Upgrade to 3.2.0

This commit is contained in:
Bastian Allgeier
2019-06-25 09:56:08 +02:00
parent 9e18cf635d
commit 9c89153d35
296 changed files with 14408 additions and 2504 deletions

View File

@@ -8,18 +8,19 @@ use Kirby\Toolkit\F;
/**
* The `Data` class provides readers and
* writers for data. The class comes with
* three handlers for `json`, `yaml` and
* `txt` encoded data, but can be extended
* and customized.
* four handlers for `json`, `php`, `txt`
* and `yaml` encoded data, but can be
* extended and customized.
*
* The read and write methods automatically
* detect, which data handler to use in order
* detect which data handler to use in order
* to correctly encode and decode passed data.
*
* @package Kirby
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class Data
{
@@ -50,31 +51,31 @@ class Data
/**
* Handler getter
*
* @param string $type
* @return Handler|null
* @param string $type
* @return Kirby\Data\Handler
*/
public static function handler(string $type)
{
// normalize the type
$type = strtolower($type);
$handler = static::$handlers[$type] ?? null;
$type = strtolower($type);
if ($handler === null && isset(static::$aliases[$type]) === true) {
$handler = static::$handlers[static::$aliases[$type]] ?? null;
// find a handler or alias
$handler = static::$handlers[$type] ??
static::$handlers[static::$aliases[$type] ?? null] ??
null;
if (class_exists($handler)) {
return new $handler;
}
if ($handler === null) {
throw new Exception('Missing Handler for type: "' . $type . '"');
}
return new $handler;
throw new Exception('Missing handler for type: "' . $type . '"');
}
/**
* Decode data with the specified handler
* Decodes data with the specified handler
*
* @param string $data
* @param string $type
* @param string $data
* @param string $type
* @return array
*/
public static function decode(string $data = null, string $type): array
@@ -83,10 +84,10 @@ class Data
}
/**
* Encode data with the specified handler
* Encodes data with the specified handler
*
* @param array $data
* @param string $type
* @param array $data
* @param string $type
* @return string
*/
public static function encode(array $data = null, string $type): string
@@ -95,9 +96,9 @@ class Data
}
/**
* Reads data from a file
* The data handler is automatically chosen by
* the extension if not specified.
* Reads data from a file;
* the data handler is automatically chosen by
* the extension if not specified
*
* @param string $file
* @param string $type
@@ -109,13 +110,13 @@ class Data
}
/**
* Writes data to a file.
* The data handler is automatically chosen by
* the extension if not specified.
* Writes data to a file;
* the data handler is automatically chosen by
* the extension if not specified
*
* @param string $file
* @param array $data
* @param string $type
* @param string $file
* @param array $data
* @param string $type
* @return boolean
*/
public static function write(string $file = null, array $data = [], string $type = null): bool

View File

@@ -12,9 +12,9 @@ use Kirby\Toolkit\F;
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
abstract class Handler
{
@@ -45,7 +45,7 @@ abstract class Handler
*/
public static function read(string $file): array
{
if (file_exists($file) !== true) {
if (is_file($file) !== true) {
throw new Exception('The file "' . $file . '" does not exist');
}
@@ -53,11 +53,9 @@ abstract class Handler
}
/**
* Writes data to a file.
* The data handler is automatically chosen by
* the extension if not specified.
* Writes data to a file
*
* @param array $data
* @param array $data
* @return boolean
*/
public static function write(string $file = null, array $data = []): bool

View File

@@ -9,12 +9,13 @@ use Exception;
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class Json extends Handler
{
/**
* Converts an array to an encoded JSON string
*

View File

@@ -10,17 +10,18 @@ use Kirby\Toolkit\F;
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class PHP extends Handler
{
/**
* Converts an array to PHP file content
*
* @param mixed $data
* @param string $indent
* @param string $indent For internal use only
* @return string
*/
public static function encode($data, $indent = ''): string
@@ -64,7 +65,7 @@ class PHP extends Handler
*/
public static function read(string $file): array
{
if (file_exists($file) !== true) {
if (is_file($file) !== true) {
throw new Exception('The file "' . $file . '" does not exist');
}
@@ -74,7 +75,7 @@ class PHP extends Handler
/**
* Creates a PHP file with the given data
*
* @param array $data
* @param array $data
* @return boolean
*/
public static function write(string $file = null, array $data = []): bool
@@ -87,6 +88,6 @@ class PHP extends Handler
return true;
}
return false;
return false; // @codeCoverageIgnore
}
}

View File

@@ -9,9 +9,9 @@ use Kirby\Toolkit\Str;
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class Txt extends Handler
{
@@ -40,9 +40,9 @@ class Txt extends Handler
}
/**
* Helper for converting value
* Helper for converting the value
*
* @param array|string $value
* @param array|string $value
* @return string
*/
protected static function encodeValue($value): string
@@ -56,13 +56,13 @@ class Txt extends Handler
}
// escape accidental dividers within a field
$value = preg_replace('!(\n|^)----(.*?\R*)!', '$1\\----$2', $value);
$value = preg_replace('!(?<=\n|^)----!', '\\----', $value);
return $value;
}
/**
* Helper for converting key and value to result string
* Helper for converting the key and value to the result string
*
* @param string $key
* @param string $value
@@ -70,11 +70,13 @@ class Txt extends Handler
*/
protected static function encodeResult(string $key, string $value): string
{
$result = $key . ': ';
$result = $key . ':';
// multi-line content
if (preg_match('!\R!', $value) === 1) {
$result .= "\n\n";
} else {
$result .= ' ';
}
$result .= trim($value);
@@ -107,7 +109,10 @@ class Txt extends Handler
continue;
}
$data[$key] = trim(substr($field, $pos + 1));
$value = trim(substr($field, $pos + 1));
// unescape escaped dividers within a field
$data[$key] = preg_replace('!(?<=\n|^)\\\\----!', '----', $value);
}
return $data;

View File

@@ -10,9 +10,9 @@ use Spyc;
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class Yaml extends Handler
{
@@ -56,14 +56,16 @@ class Yaml extends Handler
return $yaml;
}
// remove bom
// remove BOM
$yaml = str_replace("\xEF\xBB\xBF", '', $yaml);
$result = Spyc::YAMLLoadString($yaml);
if (is_array($result)) {
return $result;
} else {
throw new Exception('YAML string is invalid');
// 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
}
}
}