1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-12 08:04:05 +02:00

feat(support): Simplify parsers and serializers #438

This commit is contained in:
Awilum
2020-07-22 09:31:01 +03:00
parent a6510f7c79
commit 7c6d8cfa93
6 changed files with 0 additions and 505 deletions

View File

@@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use ParsedownExtra;
class Markdown
{
/**
* Markdown Object
*
* @var object
* @access private
*/
private static $markdown = null;
/**
* Takes a MARKDOWN encoded string and converts it into a PHP variable.
*
* @param string $input A string containing MARKDOWN
*
* @return mixed The MARKDOWN converted to a PHP value
*/
public static function decode(string $input) : string
{
! isset(self::$markdown) and self::$markdown = new ParsedownExtra();
return self::$markdown->text($input);
}
}

View File

@@ -1,111 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use function md5;
class Parser
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Parsers
*
* @var array
*/
private $parsers = [
'markdown' => [
'name' => 'markdown',
'ext' => 'md',
],
'shortcodes' => [
'name' => 'shortcodes',
'ext' => 'php',
],
];
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Get Parser Information
*
* @param string $input Content to parse
* @param string $parser Parser type [frontmatter, json, yaml]
*
* @return array
*/
public function getParserInfo(string $parser) : array
{
return $this->parsers[$parser];
}
/**
* Parse INPUT content.
*
* @param string $input Content to parse
* @param string $parser Parser type [frontmatter, json, yaml, markdown]
* @param bool $cache Cache result data or no. Default is true
*
* @return mixed The Content converted to a PHP value
*/
public function parse(string $input, string $parser, bool $cache = true)
{
switch ($parser) {
case 'markdown':
if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = Markdown::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return Markdown::decode($input);
}
break;
case 'shortcodes':
if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = $this->flextype['shortcodes']->process($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return $this->flextype['shortcodes']->process($input);
}
break;
default:
// code...
break;
}
}
}

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Arr\Arr;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
// Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"]
$flextype['shortcodes']->addHandler('entries_fetch', static function (ShortcodeInterface $s) use ($flextype) {
return Arr::get($flextype['entries']->fetch($s->getParameter('id')), $s->getParameter('field'), $s->getParameter('default'));
});

View File

@@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Arr\Arr;
use function array_slice;
use function count;
use function implode;
use function ltrim;
use function preg_split;
use function trim;
use const PHP_EOL;
class Frontmatter
{
/**
* Returns the FRONTMATTER representation of a value
*
* @param mixed $input The PHP value
*
* @return string A FRONTMATTER string representing the original PHP value
*/
public static function encode($input) : string
{
if (isset($input['content'])) {
$content = $input['content'];
Arr::delete($input, 'content');
$matter = Yaml::encode($input);
} else {
$content = '';
$matter = Yaml::encode($input);
}
$encoded = '---' . "\n" .
$matter .
'---' . "\n" .
$content;
return $encoded;
}
/**
* Takes a FRONTMATTER encoded string and converts it into a PHP variable.
*
* @param string $input A string containing FRONTMATTER
*
* @return mixed The FRONTMATTER converted to a PHP value
*/
public static function decode(string $input)
{
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . ltrim($input));
if (count($parts) < 3) {
return ['content' => trim($input)];
}
return Yaml::decode(trim($parts[1])) + ['content' => trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))];
}
}

View File

@@ -1,114 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use RuntimeException;
use function json_decode;
use function json_encode;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
class Json
{
/**
* Encode options
*
* Bitmask consisting of encode options
* https://www.php.net/manual/en/function.json-encode.php
*
* @var int
*/
public static $encode_options = JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT;
/**
* Encode Depth
*
* Set the maximum depth. Must be greater than zero.
*
* @var int
*/
public static $encode_depth = 512;
/**
* Decode assoc
*
* When TRUE, returned objects will be converted into associative arrays.
*
* @var int
*/
public static $decode_assoc = true;
/**
* Decode Depth
*
* User specified recursion depth.
*
* @var int
*/
public static $decode_depth = 512;
/**
* Decode options
*
* Bitmask consisting of decode options
* https://www.php.net/manual/en/function.json-decode.php
*
* @var int
*/
public static $decode_options = 0;
/**
* Returns the JSON representation of a value
*
* @param mixed $input The PHP value
*
* @return mixed A JSON string representing the original PHP value
*/
public static function encode($input) : string
{
$encoded = @json_encode(
$input,
self::$encode_options,
self::$encode_depth
);
if ($encoded === false) {
throw new RuntimeException('Encoding JSON failed');
}
return $encoded;
}
/**
* Takes a JSON encoded string and converts it into a PHP variable.
*
* @param string $input A string containing JSON
*
* @return mixed The JSON converted to a PHP value
*
* @throws ParseException If the JSON is not valid
*/
public static function decode(string $input)
{
$decoded = @json_decode(
$input,
self::$decode_assoc,
self::$decode_depth,
self::$decode_options
);
if ($decoded === false) {
throw new RuntimeException('Decoding JSON failed');
}
return $decoded;
}
}

View File

@@ -1,160 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use function md5;
class Serializer
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Serializer
*
* @var array
*/
private $serializer = [
'frontmatter' => [
'name' => 'frontmatter',
'ext' => 'md',
],
'json' => [
'name' => 'json',
'ext' => 'json',
],
'yaml' => [
'name' => 'yaml',
'ext' => 'yaml',
],
];
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Get Parser Information
*
* @param string $input Content to parse
* @param string $serializer Parser type [frontmatter, json, yaml]
*
* @return array
*/
public function getParserInfo(string $serializer) : array
{
return $this->serializer[$serializer];
}
/**
* Dumps a PHP value to a string CONTENT.
*
* @param mixed $input Content to parse
* @param string $serializer Parser type [frontmatter, json, yaml]
*
* @return mixed PHP value converted to a string CONTENT.
*/
public function encode($input, string $serializer) : string
{
switch ($serializer) {
case 'frontmatter':
return Frontmatter::encode($input);
break;
case 'json':
return Json::encode($input);
break;
case 'yaml':
return Yaml::encode($input);
break;
default:
break;
}
}
/**
* Parse INPUT content into a PHP value.
*
* @param string $input Content to parse
* @param string $serializer Serializer type [frontmatter, json, yaml]
* @param bool $cache Cache result data or no. Default is true
*
* @return mixed The Content converted to a PHP value
*/
public function decode(string $input, string $serializer, bool $cache = true)
{
switch ($serializer) {
case 'frontmatter':
if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = Frontmatter::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return Frontmatter::decode($input);
}
break;
case 'json':
if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = Json::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return Json::decode($input);
}
break;
case 'yaml':
if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = Yaml::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return Yaml::decode($input);
}
break;
default:
// code...
break;
}
}
}