1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-26 06:24:32 +02:00

refactor(core): Parser API's refactoring #288

This commit is contained in:
Awilum
2019-11-11 17:04:30 +03:00
parent 64264c8061
commit a94682a172
4 changed files with 19 additions and 19 deletions

View File

@@ -21,24 +21,12 @@ use function trim;
class Frontmatter
{
/**
* Front matter parser
* Returns the Frontmatter representation of a value
*
* @param string $content Content to parse
* @param mixed $input The PHP value
*
* @return array
*
* @access public
* @return string A Frontmatter string representing the original PHP value
*/
public static function parser(string $content) : array
{
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . ltrim($content));
if (count($parts) < 3) {
return ['content' => trim($content)];
}
return Yaml::decode(trim($parts[1])) + ['content' => trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))];
}
public static function encode($input) : string
{
if (isset($input['content'])) {
@@ -58,8 +46,20 @@ class Frontmatter
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)
{
return self::parser($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

@@ -68,9 +68,9 @@ class Json
/**
* Returns the JSON representation of a value
*
* @param mixed $input A string containing JSON
* @param mixed $input The PHP value
*
* @return mixed The JSON converted to a PHP value
* @return mixed A JSON string representing the original PHP value
*/
public static function encode($input) : string
{

View File

@@ -75,7 +75,7 @@ class Yaml
* The dump method, when supplied with an array, will do its best
* to convert the array into friendly YAML.
*
* @param mixed $input The PHP value
* @param mixed $input The PHP value
*
* @return string A YAML string representing the original PHP value
*/