From a94682a17277e6f4854e694cba0019c61481edc9 Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 11 Nov 2019 17:04:30 +0300 Subject: [PATCH] refactor(core): Parser API's refactoring #288 --- flextype/{parsers => core}/Parser.php | 0 flextype/parsers/Frontmatter.php | 32 +++++++++++++-------------- flextype/parsers/Json.php | 4 ++-- flextype/parsers/Yaml.php | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) rename flextype/{parsers => core}/Parser.php (100%) diff --git a/flextype/parsers/Parser.php b/flextype/core/Parser.php similarity index 100% rename from flextype/parsers/Parser.php rename to flextype/core/Parser.php diff --git a/flextype/parsers/Frontmatter.php b/flextype/parsers/Frontmatter.php index 7481a785..eaeaa4a7 100644 --- a/flextype/parsers/Frontmatter.php +++ b/flextype/parsers/Frontmatter.php @@ -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)))]; } } diff --git a/flextype/parsers/Json.php b/flextype/parsers/Json.php index baac8f53..71c80171 100644 --- a/flextype/parsers/Json.php +++ b/flextype/parsers/Json.php @@ -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 { diff --git a/flextype/parsers/Yaml.php b/flextype/parsers/Yaml.php index 93d4c443..9c3ab75f 100644 --- a/flextype/parsers/Yaml.php +++ b/flextype/parsers/Yaml.php @@ -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 */