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

feat(core): add Serializer and Parsers for data encoding/decoding/parsing. #424

This commit is contained in:
Awilum
2020-04-22 23:44:11 +03:00
parent c32de2ae7a
commit c27a72c438
2 changed files with 15 additions and 30 deletions

View File

@@ -234,15 +234,20 @@ class Entries
foreach ($entry_decoded['parsers'] as $parser_name => $parser_data) {
if (in_array($parser_name, ['markdown', 'shortcodes'])) {
if (isset($entry_decoded['parsers'][$parser_name]['enabled']) && $entry_decoded['parsers'][$parser_name]['enabled'] === true) {
if (isset($entry_decoded['parsers'][$parser_name]['cache']) && $entry_decoded['parsers'][$parser_name]['cache'] === true) {
$cache = true;
} else {
$cache = false;
}
if (isset($entry_decoded['parsers'][$parser_name]['fields'])) {
if (is_array($entry_decoded['parsers'][$parser_name]['fields'])) {
foreach ($entry_decoded['parsers'][$parser_name]['fields'] as $field) {
if (! in_array($field, $this->system_fields)) {
if ($parser_name == 'markdown') {
$entry_decoded[$field] = $this->flextype['parser']->decode($entry_decoded[$field], 'markdown');
$entry_decoded[$field] = $this->flextype['parser']->parse($entry_decoded[$field], 'markdown', $cache);
}
if ($parser_name == 'shortcodes') {
$entry_decoded[$field] = $this->flextype['parser']->decode($entry_decoded[$field], 'shortcodes');
$entry_decoded[$field] = $this->flextype['parser']->parse($entry_decoded[$field], 'shortcodes', $cache);
}
}
}
@@ -250,10 +255,10 @@ class Entries
foreach ($entry_decoded as $key => $value) {
if (! in_array($key, $this->system_fields)) {
if ($parser_name == 'markdown') {
$entry_decoded[$key] = $this->flextype['parser']->decode($entry_decoded[$key], 'markdown');
$entry_decoded[$key] = $this->flextype['parser']->parse($entry_decoded[$key], 'markdown', $cache);
}
if ($parser_name == 'shortcodes') {
$entry_decoded[$key] = $this->flextype['parser']->decode($entry_decoded[$key], 'shortcodes');
$entry_decoded[$key] = $this->flextype['parser']->parse($entry_decoded[$key], 'shortcodes', $cache);
}
}
}

View File

@@ -28,6 +28,10 @@ class Parser
'name' => 'markdown',
'ext' => 'md',
],
'shortcodes' => [
'name' => 'shortcodes',
'ext' => 'php',
],
];
/**
@@ -54,31 +58,7 @@ class Parser
}
/**
* Dumps a PHP value to a string CONTENT.
*
* @param mixed $input Content to parse
* @param string $parser Parser type [markdown]
*
* @return mixed PHP value converted to a string CONTENT.
*/
public function encode($input, string $parser) : string
{
switch ($parser) {
case 'markdown':
return $input;
break;
case 'shortcodes':
return $input;
break;
default:
break;
}
}
/**
* Parse INPUT content into a PHP value.
* Parse INPUT content.
*
* @param string $input Content to parse
* @param string $parser Parser type [frontmatter, json, yaml, markdown]
@@ -86,7 +66,7 @@ class Parser
*
* @return mixed The Content converted to a PHP value
*/
public function decode(string $input, string $parser, bool $cache = true)
public function parse(string $input, string $parser, bool $cache = true)
{
switch ($parser) {
case 'markdown':