diff --git a/flextype/twig/JsonParserTwigExtension.php b/flextype/twig/JsonParserTwigExtension.php new file mode 100644 index 00000000..7d221d89 --- /dev/null +++ b/flextype/twig/JsonParserTwigExtension.php @@ -0,0 +1,39 @@ + + * @link http://romanenko.digital + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +class JsonParserTwigExtension extends \Twig_Extension +{ + /** + * Callback for twig. + * + * @return array + */ + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('json_parser_decode', array($this, 'json_parser_decode')), + new \Twig_SimpleFunction('json_parser_encode', array($this, 'json_parser_encode')) + ]; + } + + public function encode($input, int $encode_options = 0, int $encode_depth = 512) : string + { + return JsonParser::encode($input, $encode_options, $encode_depth); + } + + public function decode(string $input, bool $decode_assoc = true, int $decode_depth = 512, int $decode_options = 0) + { + return JsonParser::decode($input, $decode_assoc, $decode_depth, $decode_options); + } +}