1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-27 06:54:31 +02:00

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

This commit is contained in:
Awilum
2019-11-11 17:42:58 +03:00
parent a94682a172
commit ce4f167fab
6 changed files with 48 additions and 17 deletions

View File

@@ -162,12 +162,12 @@ class Parser
return $data_from_cache;
}
$data = Markdown::parse($input);
$data = Markdown::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return Markdown::parse($input);
return Markdown::decode($input);
}
break;

View File

@@ -21,11 +21,11 @@ use function trim;
class Frontmatter
{
/**
* Returns the Frontmatter representation of a value
* Returns the FRONTMATTER representation of a value
*
* @param mixed $input The PHP value
*
* @return string A Frontmatter string representing the original PHP value
* @return string A FRONTMATTER string representing the original PHP value
*/
public static function encode($input) : string
{
@@ -47,11 +47,11 @@ class Frontmatter
}
/**
* Takes a Frontmatter encoded string and converts it into a PHP variable.
* Takes a FRONTMATTER encoded string and converts it into a PHP variable.
*
* @param string $input A string containing Frontmatter
* @param string $input A string containing FRONTMATTER
*
* @return mixed The Frontmatter converted to a PHP value
* @return mixed The FRONTMATTER converted to a PHP value
*/
public static function decode(string $input)
{

View File

@@ -25,9 +25,13 @@ class Markdown
private static $markdown = null;
/**
* parse
* 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 parse($input) : string
public static function decode($input) : string
{
!isset(self::$markdown) and self::$markdown = new ParsedownExtra();

View File

@@ -14,6 +14,19 @@ use Twig_SimpleFunction;
class JsonTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
@@ -32,14 +45,14 @@ class JsonTwigExtension extends Twig_Extension
*/
public function encode($input) : string
{
return Json::encode($input);
return $this->flextype['parser']->encode($input, 'json');
}
/**
* Decode JSON
*/
public function decode(string $input)
public function decode(string $input, bool $cache = true)
{
return Json::decode($input);
return $this->flextype['parser']->decode($input, 'json', $cache);
}
}

View File

@@ -42,8 +42,8 @@ class MarkdownTwigExtension extends Twig_Extension
/**
* Markdown process
*/
public function markdown($value) : string
public function markdown($input, bool $cache = true) : string
{
return Markdown::parse($value);
return $this->flextype['parser']->decode($input, 'markdown', $cache);
}
}

View File

@@ -14,6 +14,20 @@ use Twig_SimpleFunction;
class YamlTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
@@ -32,14 +46,14 @@ class YamlTwigExtension extends Twig_Extension
*/
public function encode($input) : string
{
return Yaml::encode($input);
return $this->flextype['parser']->encode($input, 'yaml');
}
/**
* Decode YAML
*/
public function decode(string $input)
public function decode(string $input, bool $cache = true)
{
return Yaml::decode($input);
return $this->flextype['parser']->decode($input, 'yaml', $cache);
}
}