Initial commit
This commit is contained in:
commit
73c6b816c0
716 changed files with 170045 additions and 0 deletions
127
kirby/src/Data/Data.php
Normal file
127
kirby/src/Data/Data.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Filesystem\F;
|
||||
|
||||
/**
|
||||
* The `Data` class provides readers and
|
||||
* writers for data. The class comes with
|
||||
* handlers for `json`, `php`, `txt`, `xml`
|
||||
* and `yaml` encoded data, but can be
|
||||
* extended and customized.
|
||||
*
|
||||
* The read and write methods automatically
|
||||
* detect which data handler to use in order
|
||||
* to correctly encode and decode passed data.
|
||||
*
|
||||
* @package Kirby
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
/**
|
||||
* Handler Type Aliases
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $aliases = [
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt',
|
||||
'rss' => 'xml',
|
||||
'yml' => 'yaml',
|
||||
];
|
||||
|
||||
/**
|
||||
* All registered handlers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $handlers = [
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'php' => 'Kirby\Data\PHP',
|
||||
'txt' => 'Kirby\Data\Txt',
|
||||
'xml' => 'Kirby\Data\Xml',
|
||||
'yaml' => 'Kirby\Data\Yaml',
|
||||
];
|
||||
|
||||
/**
|
||||
* Handler getter
|
||||
*
|
||||
* @param string $type
|
||||
* @return \Kirby\Data\Handler
|
||||
*/
|
||||
public static function handler(string $type)
|
||||
{
|
||||
// normalize the type
|
||||
$type = strtolower($type);
|
||||
|
||||
// find a handler or alias
|
||||
$handler = static::$handlers[$type] ??
|
||||
static::$handlers[static::$aliases[$type] ?? null] ??
|
||||
null;
|
||||
|
||||
if ($handler !== null && class_exists($handler)) {
|
||||
return new $handler();
|
||||
}
|
||||
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes data with the specified handler
|
||||
*
|
||||
* @param mixed $string
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes data with the specified handler
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, string $type): string
|
||||
{
|
||||
return static::handler($type)->encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data from a file;
|
||||
* the data handler is automatically chosen by
|
||||
* the extension if not specified
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function read(string $file, string $type = null): array
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->read($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to a file;
|
||||
* the data handler is automatically chosen by
|
||||
* the extension if not specified
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, $data = [], string $type = null): bool
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->write($file, $data);
|
||||
}
|
||||
}
|
66
kirby/src/Data/Handler.php
Normal file
66
kirby/src/Data/Handler.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Filesystem\F;
|
||||
|
||||
/**
|
||||
* Base handler abstract,
|
||||
* which needs to be extended to
|
||||
* create valid data handlers
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
abstract class Handler
|
||||
{
|
||||
/**
|
||||
* Parses an encoded string and returns a multi-dimensional array
|
||||
*
|
||||
* Needs to throw an Exception if the file can't be parsed.
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
abstract public static function decode($string): array;
|
||||
|
||||
/**
|
||||
* Converts an array to an encoded string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
abstract public static function encode($data): string;
|
||||
|
||||
/**
|
||||
* Reads data from a file
|
||||
*
|
||||
* @param string $file
|
||||
* @return array
|
||||
*/
|
||||
public static function read(string $file): array
|
||||
{
|
||||
$contents = F::read($file);
|
||||
if ($contents === false) {
|
||||
throw new Exception('The file "' . $file . '" does not exist');
|
||||
}
|
||||
|
||||
return static::decode($contents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to a file
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, $data = []): bool
|
||||
{
|
||||
return F::write($file, static::encode($data));
|
||||
}
|
||||
}
|
57
kirby/src/Data/Json.php
Normal file
57
kirby/src/Data/Json.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around json_encode and json_decode
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Json extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded JSON string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded JSON string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($string === null || $string === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($string) === true) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid JSON data; please pass a string');
|
||||
}
|
||||
|
||||
$result = json_decode($string, true);
|
||||
|
||||
if (is_array($result) === true) {
|
||||
return $result;
|
||||
} else {
|
||||
throw new InvalidArgumentException('JSON string is invalid');
|
||||
}
|
||||
}
|
||||
}
|
94
kirby/src/Data/PHP.php
Normal file
94
kirby/src/Data/PHP.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\BadMethodCallException;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Filesystem\F;
|
||||
|
||||
/**
|
||||
* Reader and write of PHP files with data in a returned array
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class PHP extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to PHP file content
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $indent For internal use only
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, string $indent = ''): string
|
||||
{
|
||||
switch (gettype($data)) {
|
||||
case 'array':
|
||||
$indexed = array_keys($data) === range(0, count($data) - 1);
|
||||
$array = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$array[] = "$indent " . ($indexed ? '' : static::encode($key) . ' => ') . static::encode($value, "$indent ");
|
||||
}
|
||||
|
||||
return "[\n" . implode(",\n", $array) . "\n" . $indent . ']';
|
||||
case 'boolean':
|
||||
return $data ? 'true' : 'false';
|
||||
case 'integer':
|
||||
case 'double':
|
||||
return $data;
|
||||
default:
|
||||
return var_export($data, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP strings shouldn't be decoded manually
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
throw new BadMethodCallException('The PHP::decode() method is not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data from a file
|
||||
*
|
||||
* @param string $file
|
||||
* @return array
|
||||
*/
|
||||
public static function read(string $file): array
|
||||
{
|
||||
if (is_file($file) !== true) {
|
||||
throw new Exception('The file "' . $file . '" does not exist');
|
||||
}
|
||||
|
||||
return (array)F::load($file, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PHP file with the given data
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, $data = []): bool
|
||||
{
|
||||
$php = static::encode($data);
|
||||
$php = "<?php\n\nreturn $php;";
|
||||
|
||||
if (F::write($file, $php) === true) {
|
||||
F::invalidateOpcodeCache($file);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
133
kirby/src/Data/Txt.php
Normal file
133
kirby/src/Data/Txt.php
Normal file
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Kirby Txt Data Handler
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Txt extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded Kirby txt string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach (A::wrap($data) as $key => $value) {
|
||||
if (empty($key) === true || $value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = Str::ucfirst(Str::slug($key));
|
||||
$value = static::encodeValue($value);
|
||||
$result[$key] = static::encodeResult($key, $value);
|
||||
}
|
||||
|
||||
return implode("\n\n----\n\n", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for converting the value
|
||||
*
|
||||
* @param array|string $value
|
||||
* @return string
|
||||
*/
|
||||
protected static function encodeValue($value): string
|
||||
{
|
||||
// avoid problems with arrays
|
||||
if (is_array($value) === true) {
|
||||
$value = Data::encode($value, 'yaml');
|
||||
// avoid problems with localized floats
|
||||
} elseif (is_float($value) === true) {
|
||||
$value = Str::float($value);
|
||||
}
|
||||
|
||||
// escape accidental dividers within a field
|
||||
$value = preg_replace('!(?<=\n|^)----!', '\\----', $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for converting the key and value to the result string
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected static function encodeResult(string $key, string $value): string
|
||||
{
|
||||
$result = $key . ':';
|
||||
|
||||
// multi-line content
|
||||
if (preg_match('!\R!', $value) === 1) {
|
||||
$result .= "\n\n";
|
||||
} else {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
$result .= trim($value);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a Kirby txt string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($string === null || $string === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($string) === true) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid TXT data; please pass a string');
|
||||
}
|
||||
|
||||
// remove BOM
|
||||
$string = str_replace("\xEF\xBB\xBF", '', $string);
|
||||
// explode all fields by the line separator
|
||||
$fields = preg_split('!\n----\s*\n*!', $string);
|
||||
// start the data array
|
||||
$data = [];
|
||||
|
||||
// loop through all fields and add them to the content
|
||||
foreach ($fields as $field) {
|
||||
$pos = strpos($field, ':');
|
||||
$key = str_replace(['-', ' '], '_', strtolower(trim(substr($field, 0, $pos))));
|
||||
|
||||
// Don't add fields with empty keys
|
||||
if (empty($key) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = trim(substr($field, $pos + 1));
|
||||
|
||||
// unescape escaped dividers within a field
|
||||
$data[$key] = preg_replace('!(?<=\n|^)\\\\----!', '----', $value);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
64
kirby/src/Data/Xml.php
Normal file
64
kirby/src/Data/Xml.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\Xml as XmlConverter;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around the XML parser of the Toolkit
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Lukas Bestle <lukas@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Xml extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded XML string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
return XmlConverter::create($data, 'data');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded XML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($string === null || $string === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($string) === true) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid XML data; please pass a string');
|
||||
}
|
||||
|
||||
$result = XmlConverter::parse($string);
|
||||
|
||||
if (is_array($result) === true) {
|
||||
// remove the root's name if it is the default <data> to ensure that
|
||||
// the decoded data is the same as the input to the encode() method
|
||||
if ($result['@name'] === 'data') {
|
||||
unset($result['@name']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
throw new InvalidArgumentException('XML string is invalid');
|
||||
}
|
||||
}
|
||||
}
|
77
kirby/src/Data/Yaml.php
Normal file
77
kirby/src/Data/Yaml.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Spyc;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around the Spyc YAML class
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Yaml extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded YAML string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
// TODO: The locale magic should no longer be
|
||||
// necessary when support for PHP 7.x is dropped
|
||||
|
||||
// fetch the current locale setting for numbers
|
||||
$locale = setlocale(LC_NUMERIC, 0);
|
||||
|
||||
// change to english numerics to avoid issues with floats
|
||||
setlocale(LC_NUMERIC, 'C');
|
||||
|
||||
// $data, $indent, $wordwrap, $no_opening_dashes
|
||||
$yaml = Spyc::YAMLDump($data, false, false, true);
|
||||
|
||||
// restore the previous locale settings
|
||||
setlocale(LC_NUMERIC, $locale);
|
||||
|
||||
return $yaml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded YAML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($string === null || $string === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($string) === true) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid YAML data; please pass a string');
|
||||
}
|
||||
|
||||
// remove BOM
|
||||
$string = str_replace("\xEF\xBB\xBF", '', $string);
|
||||
$result = Spyc::YAMLLoadString($string);
|
||||
|
||||
if (is_array($result)) {
|
||||
return $result;
|
||||
} else {
|
||||
// apparently Spyc always returns an array, even for invalid YAML syntax
|
||||
// so this Exception should currently never be thrown
|
||||
throw new InvalidArgumentException('The YAML data cannot be parsed'); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue