first version
This commit is contained in:
124
kirby/src/Data/Data.php
Executable file
124
kirby/src/Data/Data.php
Executable file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
* Universal Data writer and reader class.
|
||||
*
|
||||
* The read and write methods automatically
|
||||
* detect, which data handler to use in order
|
||||
* to correctly encode and decode passed data.
|
||||
*
|
||||
* Data Handlers for the class can be
|
||||
* extended and customized.
|
||||
*
|
||||
* @package Kirby
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright 2012 Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
|
||||
/**
|
||||
* Handler Type Aliases
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $aliases = [
|
||||
'yml' => 'yaml',
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt'
|
||||
];
|
||||
|
||||
/**
|
||||
* All registered handlers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $handlers = [
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'yaml' => 'Kirby\Data\Yaml',
|
||||
'txt' => 'Kirby\Data\Txt'
|
||||
];
|
||||
|
||||
/**
|
||||
* Handler getter
|
||||
*
|
||||
* @param string $type
|
||||
* @return Handler|null
|
||||
*/
|
||||
public static function handler(string $type)
|
||||
{
|
||||
// normalize the type
|
||||
$type = strtolower($type);
|
||||
$handler = static::$handlers[$type] ?? null;
|
||||
|
||||
if ($handler === null && isset(static::$aliases[$type]) === true) {
|
||||
$handler = static::$handlers[static::$aliases[$type]] ?? null;
|
||||
}
|
||||
|
||||
if ($handler === null) {
|
||||
throw new Exception('Missing Handler for type: "' . $type . '"');
|
||||
}
|
||||
|
||||
return new $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode data with the specified handler
|
||||
*
|
||||
* @param string $data
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode(string $data = null, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data with the specified handler
|
||||
*
|
||||
* @param array $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(array $data = null, 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 array $data
|
||||
* @param string $type
|
||||
* @return boolean
|
||||
*/
|
||||
public static function write(string $file = null, array $data = [], string $type = null): bool
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->write($file, $data);
|
||||
}
|
||||
}
|
67
kirby/src/Data/Handler.php
Executable file
67
kirby/src/Data/Handler.php
Executable file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
* Base handler abstract,
|
||||
* which needs to be extended to
|
||||
* create valid data handlers
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license 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 string $string
|
||||
* @return array
|
||||
*/
|
||||
abstract public static function decode($string): array;
|
||||
|
||||
/**
|
||||
* Converts an array to an encoded string
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
abstract public static function encode(array $data): string;
|
||||
|
||||
/**
|
||||
* 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 static::decode(F::read($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to a file.
|
||||
* The data handler is automatically chosen by
|
||||
* the extension if not specified.
|
||||
*
|
||||
* @param array $data
|
||||
* @return boolean
|
||||
*/
|
||||
public static function write(string $file = null, array $data = []): bool
|
||||
{
|
||||
return F::write($file, static::encode($data));
|
||||
}
|
||||
}
|
45
kirby/src/Data/Json.php
Executable file
45
kirby/src/Data/Json.php
Executable file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around json_encode and json_decode
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Json extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to an encoded JSON string
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(array $data): string
|
||||
{
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded JSON string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($json): array
|
||||
{
|
||||
$result = json_decode($json, true);
|
||||
|
||||
if (is_array($result) === true) {
|
||||
return $result;
|
||||
} else {
|
||||
throw new Exception('JSON string is invalid');
|
||||
}
|
||||
}
|
||||
}
|
112
kirby/src/Data/Txt.php
Executable file
112
kirby/src/Data/Txt.php
Executable file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Kirby Txt Data Handler
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Txt extends Handler
|
||||
{
|
||||
|
||||
/**
|
||||
* Converts an array to an encoded Kirby txt string
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(array $data): string
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($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 value
|
||||
*
|
||||
* @param array|string $value
|
||||
* @return string
|
||||
*/
|
||||
protected static function encodeValue($value): string
|
||||
{
|
||||
// avoid problems with arrays
|
||||
if (is_array($value) === true) {
|
||||
$value = Yaml::encode($value);
|
||||
}
|
||||
|
||||
// escape accidental dividers within a field
|
||||
$value = preg_replace('!(\n|^)----(.*?\R*)!', '$1\\----$2', $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for converting key and value to 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";
|
||||
}
|
||||
|
||||
$result .= trim($value);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a Kirby txt string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
$data[$key] = trim(substr($field, $pos + 1));
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
56
kirby/src/Data/Yaml.php
Executable file
56
kirby/src/Data/Yaml.php
Executable file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Data;
|
||||
|
||||
use Exception;
|
||||
use Spyc;
|
||||
|
||||
/**
|
||||
* Simple Wrapper around Symfony's Yaml Component
|
||||
*
|
||||
* @package Kirby Data
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Yaml extends Handler
|
||||
{
|
||||
|
||||
/**
|
||||
* Converts an array to an encoded YAML string
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(array $data): string
|
||||
{
|
||||
// $data, $indent, $wordwrap, $no_opening_dashes
|
||||
return Spyc::YAMLDump($data, false, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an encoded YAML string and returns a multi-dimensional array
|
||||
*
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($yaml): array
|
||||
{
|
||||
if ($yaml === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_array($yaml) === true) {
|
||||
return $yaml;
|
||||
}
|
||||
|
||||
$result = Spyc::YAMLLoadString($yaml);
|
||||
|
||||
if (is_array($result)) {
|
||||
return $result;
|
||||
} else {
|
||||
throw new Exception('YAML string is invalid');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user