Let PHP convert Twig_Markup to JSON instead of pre-filtering it

This commit is contained in:
Christophe Coevoet
2016-01-07 22:29:55 +01:00
parent 1a817fc8ee
commit 8bd96c12d3
2 changed files with 7 additions and 25 deletions
+1 -24
View File
@@ -149,7 +149,7 @@ class Twig_Extension_Core extends Twig_Extension
// encoding
new Twig_Filter('url_encode', 'twig_urlencode_filter'),
new Twig_Filter('json_encode', 'twig_jsonencode_filter'),
new Twig_Filter('json_encode', 'json_encode'),
new Twig_Filter('convert_encoding', 'twig_convert_encoding'),
// string filters
@@ -582,29 +582,6 @@ function twig_urlencode_filter($url)
return rawurlencode($url);
}
/**
* JSON encodes a variable.
*
* @param mixed $value The value to encode.
* @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
*
* @return mixed The JSON encoded value
*/
function twig_jsonencode_filter($value, $options = 0)
{
if ($value instanceof Twig_Markup) {
$value = (string) $value;
} elseif (is_array($value)) {
array_walk_recursive($value, function (&$value) {
if ($value instanceof Twig_Markup) {
$value = (string) $value;
}
});
}
return json_encode($value, $options);
}
/**
* Merges an array with another one.
*
+6 -1
View File
@@ -14,7 +14,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Markup implements Countable
class Twig_Markup implements Countable, JsonSerializable
{
private $content;
private $charset;
@@ -34,4 +34,9 @@ class Twig_Markup implements Countable
{
return mb_strlen($this->content, $this->charset);
}
public function jsonSerialize()
{
return $this->content;
}
}