Upgrade to 3.8.0
This commit is contained in:
@@ -26,10 +26,8 @@ class Data
|
||||
{
|
||||
/**
|
||||
* Handler Type Aliases
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $aliases = [
|
||||
public static array $aliases = [
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt',
|
||||
'rss' => 'xml',
|
||||
@@ -38,10 +36,8 @@ class Data
|
||||
|
||||
/**
|
||||
* All registered handlers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $handlers = [
|
||||
public static array $handlers = [
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'php' => 'Kirby\Data\PHP',
|
||||
'txt' => 'Kirby\Data\Txt',
|
||||
@@ -51,33 +47,32 @@ class Data
|
||||
|
||||
/**
|
||||
* Handler getter
|
||||
*
|
||||
* @param string $type
|
||||
* @return \Kirby\Data\Handler
|
||||
*/
|
||||
public static function handler(string $type)
|
||||
public static function handler(string $type): Handler
|
||||
{
|
||||
// normalize the type
|
||||
$type = strtolower($type);
|
||||
|
||||
// find a handler or alias
|
||||
$alias = static::$aliases[$type] ?? null;
|
||||
$handler = static::$handlers[$type] ??
|
||||
static::$handlers[static::$aliases[$type] ?? null] ??
|
||||
null;
|
||||
($alias ? static::$handlers[$alias] ?? null : null);
|
||||
|
||||
if ($handler !== null && class_exists($handler)) {
|
||||
return new $handler();
|
||||
if ($handler === null || class_exists($handler) === false) {
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
}
|
||||
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
$handler = new $handler();
|
||||
|
||||
if ($handler instanceof Handler === false) {
|
||||
throw new Exception('Handler for type: "' . $type . '" needs to extend Kirby\\Data\\Handler');
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes data with the specified handler
|
||||
*
|
||||
* @param mixed $string
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string, string $type): array
|
||||
{
|
||||
@@ -86,10 +81,6 @@ class Data
|
||||
|
||||
/**
|
||||
* Encodes data with the specified handler
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, string $type): string
|
||||
{
|
||||
@@ -100,12 +91,8 @@ class 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
|
||||
public static function read(string $file, string|null $type = null): array
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->read($file);
|
||||
}
|
||||
@@ -114,14 +101,12 @@ class Data
|
||||
* 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
|
||||
{
|
||||
public static function write(
|
||||
string $file,
|
||||
$data = [],
|
||||
string|null $type = null
|
||||
): bool {
|
||||
return static::handler($type ?? F::extension($file))->write($file, $data);
|
||||
}
|
||||
}
|
||||
|
@@ -21,26 +21,17 @@ 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
|
||||
* @throws \Exception if the file can't be parsed
|
||||
*/
|
||||
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
|
||||
{
|
||||
@@ -54,12 +45,8 @@ abstract class Handler
|
||||
|
||||
/**
|
||||
* Writes data to a file
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, $data = []): bool
|
||||
public static function write(string $file, $data = []): bool
|
||||
{
|
||||
return F::write($file, static::encode($data));
|
||||
}
|
||||
|
@@ -17,20 +17,17 @@ 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);
|
||||
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
|
||||
{
|
||||
@@ -50,8 +47,8 @@ class Json extends Handler
|
||||
|
||||
if (is_array($result) === true) {
|
||||
return $result;
|
||||
} else {
|
||||
throw new InvalidArgumentException('JSON string is invalid');
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('JSON string is invalid');
|
||||
}
|
||||
}
|
||||
|
@@ -20,9 +20,7 @@ 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
|
||||
{
|
||||
@@ -40,7 +38,7 @@ class PHP extends Handler
|
||||
return $data ? 'true' : 'false';
|
||||
case 'integer':
|
||||
case 'double':
|
||||
return $data;
|
||||
return (string)$data;
|
||||
default:
|
||||
return var_export($data, true);
|
||||
}
|
||||
@@ -48,9 +46,6 @@ class PHP extends Handler
|
||||
|
||||
/**
|
||||
* PHP strings shouldn't be decoded manually
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
@@ -59,9 +54,6 @@ class PHP extends Handler
|
||||
|
||||
/**
|
||||
* Reads data from a file
|
||||
*
|
||||
* @param string $file
|
||||
* @return array
|
||||
*/
|
||||
public static function read(string $file): array
|
||||
{
|
||||
@@ -74,12 +66,8 @@ class PHP extends Handler
|
||||
|
||||
/**
|
||||
* 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
|
||||
public static function write(string $file, $data = []): bool
|
||||
{
|
||||
$php = static::encode($data);
|
||||
$php = "<?php\n\nreturn $php;";
|
||||
|
@@ -19,9 +19,6 @@ class Txt extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded Kirby txt string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
@@ -42,11 +39,8 @@ class Txt extends Handler
|
||||
|
||||
/**
|
||||
* Helper for converting the value
|
||||
*
|
||||
* @param array|string $value
|
||||
* @return string
|
||||
*/
|
||||
protected static function encodeValue($value): string
|
||||
protected static function encodeValue(array|string|float $value): string
|
||||
{
|
||||
// avoid problems with arrays
|
||||
if (is_array($value) === true) {
|
||||
@@ -64,10 +58,6 @@ class Txt extends Handler
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
@@ -88,9 +78,6 @@ class Txt extends Handler
|
||||
|
||||
/**
|
||||
* Parses a Kirby txt string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
@@ -115,18 +102,24 @@ class Txt extends Handler
|
||||
|
||||
// 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))));
|
||||
if ($pos = strpos($field, ':')) {
|
||||
$key = strtolower(trim(substr($field, 0, $pos)));
|
||||
$key = str_replace(['-', ' '], '_', $key);
|
||||
|
||||
// Don't add fields with empty keys
|
||||
if (empty($key) === true) {
|
||||
continue;
|
||||
// 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
|
||||
);
|
||||
}
|
||||
|
||||
$value = trim(substr($field, $pos + 1));
|
||||
|
||||
// unescape escaped dividers within a field
|
||||
$data[$key] = preg_replace('!(?<=\n|^)\\\\----!', '----', $value);
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
@@ -18,9 +18,6 @@ class Xml extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded XML string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
{
|
||||
@@ -29,9 +26,6 @@ class Xml extends Handler
|
||||
|
||||
/**
|
||||
* Parses an encoded XML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
@@ -57,8 +51,8 @@ class Xml extends Handler
|
||||
}
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
throw new InvalidArgumentException('XML string is invalid');
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('XML string is invalid');
|
||||
}
|
||||
}
|
||||
|
@@ -18,35 +18,15 @@ 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;
|
||||
return Spyc::YAMLDump($data, false, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded YAML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
@@ -66,12 +46,12 @@ class Yaml extends Handler
|
||||
$string = str_replace("\xEF\xBB\xBF", '', $string);
|
||||
$result = Spyc::YAMLLoadString($string);
|
||||
|
||||
if (is_array($result)) {
|
||||
if (is_array($result) === true) {
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user