From 76e657961826e65bd1c5d9c86c082b6c6a104898 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 11 Aug 2019 01:06:57 +0300 Subject: [PATCH] Flextype Core: Add ability to work with different types of content #212 #186 - FrontmatterParser implementation --- flextype/parsers/FrontmatterParser.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/flextype/parsers/FrontmatterParser.php b/flextype/parsers/FrontmatterParser.php index d38208e8..2178d253 100644 --- a/flextype/parsers/FrontmatterParser.php +++ b/flextype/parsers/FrontmatterParser.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace Flextype; +use Flextype\Component\Arr\Arr; use const PHP_EOL; use function array_slice; use function count; @@ -28,7 +29,7 @@ class FrontmatterParser * * @access public */ - public static function frontMatterParser(string $content) : array + 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) { @@ -40,11 +41,25 @@ class FrontmatterParser public static function encode($input) : string { - return ''; + if ($input['content']) { + $content = $input['content']; + Arr::delete($input, 'content'); + $matter = YamlParser::encode($input); + } else { + $content = ''; + $matter = YamlParser::encode($input); + } + + $encoded = '---' . "\n" . + $matter . + '---' . "\n" . + $content; + + return $encoded; } public static function decode(string $input) { - return self::frontMatterParser($input); + return self::parser($input); } }