3.4.0
This commit is contained in:
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
* The `Data` class provides readers and
|
||||
* writers for data. The class comes with
|
||||
* four handlers for `json`, `php`, `txt`
|
||||
* handlers for `json`, `php`, `txt`, `xml`
|
||||
* and `yaml` encoded data, but can be
|
||||
* extended and customized.
|
||||
*
|
||||
@@ -32,6 +32,7 @@ class Data
|
||||
public static $aliases = [
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt',
|
||||
'rss' => 'xml',
|
||||
'yml' => 'yaml',
|
||||
];
|
||||
|
||||
@@ -44,6 +45,7 @@ class Data
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'php' => 'Kirby\Data\PHP',
|
||||
'txt' => 'Kirby\Data\Txt',
|
||||
'xml' => 'Kirby\Data\Xml',
|
||||
'yaml' => 'Kirby\Data\Yaml',
|
||||
];
|
||||
|
||||
@@ -73,23 +75,23 @@ class Data
|
||||
/**
|
||||
* Decodes data with the specified handler
|
||||
*
|
||||
* @param string $data
|
||||
* @param mixed $string
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode(string $data = null, string $type): array
|
||||
public static function decode($string = null, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($data);
|
||||
return static::handler($type)->decode($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes data with the specified handler
|
||||
*
|
||||
* @param array $data
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(array $data = null, string $type): string
|
||||
public static function encode($data = null, string $type): string
|
||||
{
|
||||
return static::handler($type)->encode($data);
|
||||
}
|
||||
@@ -114,11 +116,11 @@ class Data
|
||||
* the extension if not specified
|
||||
*
|
||||
* @param string $file
|
||||
* @param array $data
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, array $data = [], string $type = null): bool
|
||||
public static function write(string $file = null, $data = [], string $type = null): bool
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->write($file, $data);
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@ abstract class Handler
|
||||
*
|
||||
* Needs to throw an Exception if the file can't be parsed.
|
||||
*
|
||||
* @param string $string
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
abstract public static function decode($string): array;
|
||||
@@ -55,10 +55,10 @@ abstract class Handler
|
||||
* Writes data to a file
|
||||
*
|
||||
* @param string $file
|
||||
* @param array $data
|
||||
* @param mixed $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, array $data = []): bool
|
||||
public static function write(string $file = null, $data = []): bool
|
||||
{
|
||||
return F::write($file, static::encode($data));
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around json_encode and json_decode
|
||||
@@ -29,17 +29,29 @@ class Json extends Handler
|
||||
/**
|
||||
* Parses an encoded JSON string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $json
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($json): array
|
||||
public static function decode($string): array
|
||||
{
|
||||
$result = json_decode($json, true);
|
||||
if ($string === null) {
|
||||
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 Exception('JSON string is invalid');
|
||||
throw new InvalidArgumentException('JSON string is invalid');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\BadMethodCallException;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
@@ -23,7 +24,7 @@ class PHP extends Handler
|
||||
* @param string $indent For internal use only
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, $indent = ''): string
|
||||
public static function encode($data, string $indent = ''): string
|
||||
{
|
||||
switch (gettype($data)) {
|
||||
case 'array':
|
||||
@@ -46,14 +47,14 @@ class PHP extends Handler
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP arrays don't have to be decoded
|
||||
* PHP strings shouldn't be decoded manually
|
||||
*
|
||||
* @param array $array
|
||||
* @param mixed $array
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($array): array
|
||||
{
|
||||
return $array;
|
||||
throw new BadMethodCallException('The PHP::decode() method is not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,17 +69,17 @@ class PHP extends Handler
|
||||
throw new Exception('The file "' . $file . '" does not exist');
|
||||
}
|
||||
|
||||
return (array)(include $file);
|
||||
return (array)F::load($file, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PHP file with the given data
|
||||
*
|
||||
* @param string $file
|
||||
* @param array $data
|
||||
* @param mixed $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, array $data = []): bool
|
||||
public static function write(string $file = null, $data = []): bool
|
||||
{
|
||||
$php = static::encode($data);
|
||||
$php = "<?php\n\nreturn $php;";
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
@@ -25,7 +27,7 @@ class Txt extends Handler
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ((array)$data as $key => $value) {
|
||||
foreach (A::wrap($data) as $key => $value) {
|
||||
if (empty($key) === true || $value === null) {
|
||||
continue;
|
||||
}
|
||||
@@ -48,7 +50,7 @@ class Txt extends Handler
|
||||
{
|
||||
// avoid problems with arrays
|
||||
if (is_array($value) === true) {
|
||||
$value = Yaml::encode($value);
|
||||
$value = Data::encode($value, 'yaml');
|
||||
// avoid problems with localized floats
|
||||
} elseif (is_float($value) === true) {
|
||||
$value = Str::float($value);
|
||||
@@ -86,11 +88,23 @@ class Txt extends Handler
|
||||
/**
|
||||
* Parses a Kirby txt string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $string
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($string === null) {
|
||||
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
|
||||
|
64
kirby/src/Data/Xml.php
Executable file
64
kirby/src/Data/Xml.php
Executable 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 GmbH
|
||||
* @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) {
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Spyc;
|
||||
|
||||
/**
|
||||
@@ -42,29 +42,33 @@ class Yaml extends Handler
|
||||
/**
|
||||
* Parses an encoded YAML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $yaml
|
||||
* @param mixed $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($yaml): array
|
||||
public static function decode($string): array
|
||||
{
|
||||
if ($yaml === null) {
|
||||
if ($string === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($yaml) === true) {
|
||||
return $yaml;
|
||||
if (is_array($string) === true) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid YAML data; please pass a string');
|
||||
}
|
||||
|
||||
// remove BOM
|
||||
$yaml = str_replace("\xEF\xBB\xBF", '', $yaml);
|
||||
$result = Spyc::YAMLLoadString($yaml);
|
||||
$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 Exception('YAML string is invalid'); // @codeCoverageIgnore
|
||||
throw new InvalidArgumentException('The YAML data cannot be parsed'); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user