Upgrade to 3.7.1
This commit is contained in:
@@ -24,104 +24,104 @@ use Kirby\Filesystem\F;
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
/**
|
||||
* Handler Type Aliases
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $aliases = [
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt',
|
||||
'rss' => 'xml',
|
||||
'yml' => 'yaml',
|
||||
];
|
||||
/**
|
||||
* Handler Type Aliases
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $aliases = [
|
||||
'md' => 'txt',
|
||||
'mdown' => 'txt',
|
||||
'rss' => 'xml',
|
||||
'yml' => 'yaml',
|
||||
];
|
||||
|
||||
/**
|
||||
* All registered handlers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $handlers = [
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'php' => 'Kirby\Data\PHP',
|
||||
'txt' => 'Kirby\Data\Txt',
|
||||
'xml' => 'Kirby\Data\Xml',
|
||||
'yaml' => 'Kirby\Data\Yaml',
|
||||
];
|
||||
/**
|
||||
* All registered handlers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $handlers = [
|
||||
'json' => 'Kirby\Data\Json',
|
||||
'php' => 'Kirby\Data\PHP',
|
||||
'txt' => 'Kirby\Data\Txt',
|
||||
'xml' => 'Kirby\Data\Xml',
|
||||
'yaml' => 'Kirby\Data\Yaml',
|
||||
];
|
||||
|
||||
/**
|
||||
* Handler getter
|
||||
*
|
||||
* @param string $type
|
||||
* @return \Kirby\Data\Handler
|
||||
*/
|
||||
public static function handler(string $type)
|
||||
{
|
||||
// normalize the type
|
||||
$type = strtolower($type);
|
||||
/**
|
||||
* Handler getter
|
||||
*
|
||||
* @param string $type
|
||||
* @return \Kirby\Data\Handler
|
||||
*/
|
||||
public static function handler(string $type)
|
||||
{
|
||||
// normalize the type
|
||||
$type = strtolower($type);
|
||||
|
||||
// find a handler or alias
|
||||
$handler = static::$handlers[$type] ??
|
||||
static::$handlers[static::$aliases[$type] ?? null] ??
|
||||
null;
|
||||
// find a handler or alias
|
||||
$handler = static::$handlers[$type] ??
|
||||
static::$handlers[static::$aliases[$type] ?? null] ??
|
||||
null;
|
||||
|
||||
if ($handler !== null && class_exists($handler)) {
|
||||
return new $handler();
|
||||
}
|
||||
if ($handler !== null && class_exists($handler)) {
|
||||
return new $handler();
|
||||
}
|
||||
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
}
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes data with the specified handler
|
||||
*
|
||||
* @param mixed $string
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($string);
|
||||
}
|
||||
/**
|
||||
* Decodes data with the specified handler
|
||||
*
|
||||
* @param mixed $string
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes data with the specified handler
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, string $type): string
|
||||
{
|
||||
return static::handler($type)->encode($data);
|
||||
}
|
||||
/**
|
||||
* Encodes data with the specified handler
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($data, 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);
|
||||
}
|
||||
/**
|
||||
* 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 mixed $data
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public static function write(string $file = null, $data = [], string $type = null): bool
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->write($file, $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
|
||||
{
|
||||
return static::handler($type ?? F::extension($file))->write($file, $data);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user