Upgrade to 3.1.2

This commit is contained in:
Bastian Allgeier
2019-04-09 14:34:12 +02:00
parent 852a14595e
commit eb29ef6d6c
58 changed files with 535 additions and 258 deletions

View File

@@ -30,9 +30,9 @@ class Data
* @var array
*/
public static $aliases = [
'yml' => 'yaml',
'md' => 'txt',
'mdown' => 'txt'
'mdown' => 'txt',
'yml' => 'yaml',
];
/**
@@ -42,8 +42,9 @@ class Data
*/
public static $handlers = [
'json' => 'Kirby\Data\Json',
'php' => 'Kirby\Data\PHP',
'txt' => 'Kirby\Data\Txt',
'yaml' => 'Kirby\Data\Yaml',
'txt' => 'Kirby\Data\Txt'
];
/**

View File

@@ -32,10 +32,10 @@ abstract class Handler
/**
* Converts an array to an encoded string
*
* @param array $data
* @param mixed $data
* @return string
*/
abstract public static function encode(array $data): string;
abstract public static function encode($data): string;
/**
* Reads data from a file

View File

@@ -18,10 +18,10 @@ class Json extends Handler
/**
* Converts an array to an encoded JSON string
*
* @param array $data
* @param mixed $data
* @return string
*/
public static function encode(array $data): string
public static function encode($data): string
{
return json_encode($data);
}

87
kirby/src/Data/PHP.php Executable file
View File

@@ -0,0 +1,87 @@
<?php
namespace Kirby\Data;
use Exception;
use Kirby\Toolkit\F;
/**
* Reader and write of PHP files with data in a returned array
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class PHP extends Handler
{
/**
* Converts an array to PHP file content
*
* @param mixed $data
* @param string $indent
* @return string
*/
public static function encode($data, $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 'int':
case 'double':
return $data;
default:
return var_export($data, true);
}
}
/**
* PHP arrays don't have to be decoded
*
* @param array $array
* @return array
*/
public static function decode($array): array
{
return $array;
}
/**
* Reads data from a file
*
* @param string $file
* @return array
*/
public static function read(string $file): array
{
if (file_exists($file) !== true) {
throw new Exception('The file "' . $file . '" does not exist');
}
return (array)(include $file);
}
/**
* Creates a PHP file with the given data
*
* @param array $data
* @return boolean
*/
public static function write(string $file = null, array $data = []): bool
{
$php = static::encode($data);
$php = "<?php\n\nreturn $php;";
return F::write($file, $php);
}
}

View File

@@ -19,14 +19,14 @@ class Txt extends Handler
/**
* Converts an array to an encoded Kirby txt string
*
* @param array $data
* @param mixed $data
* @return string
*/
public static function encode(array $data): string
public static function encode($data): string
{
$result = [];
foreach ($data as $key => $value) {
foreach ((array)$data as $key => $value) {
if (empty($key) === true || $value === null) {
continue;
}

View File

@@ -20,10 +20,10 @@ class Yaml extends Handler
/**
* Converts an array to an encoded YAML string
*
* @param array $data
* @param mixed $data
* @return string
*/
public static function encode(array $data): string
public static function encode($data): string
{
// fetch the current locale setting for numbers
$locale = setlocale(LC_NUMERIC, 0);