[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlentitiesDecodeViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
15 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
16
17 /**
18 * Applies html_entity_decode() to a value
19 * @see http://www.php.net/html_entity_decode
20 *
21 * = Examples =
22 *
23 * <code title="default notation">
24 * <f:format.htmlentitiesDecode>{text}</f:format.htmlentitiesDecode>
25 * </code>
26 * <output>
27 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
28 * </output>
29 *
30 * <code title="inline notation">
31 * {text -> f:format.htmlentitiesDecode(encoding: 'ISO-8859-1')}
32 * </code>
33 * <output>
34 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
35 * </output>
36 *
37 * @api
38 */
39 class HtmlentitiesDecodeViewHelper extends AbstractEncodingViewHelper implements CompilableInterface {
40
41 /**
42 * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
43 * can decode the text's entities.
44 *
45 * @var bool
46 */
47 protected $escapingInterceptorEnabled = FALSE;
48
49 /**
50 * Converts all HTML entities to their applicable characters as needed using PHPs html_entity_decode() function.
51 *
52 * @param string $value string to format
53 * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
54 * @param string $encoding
55 * @return string the altered string
56 * @see http://www.php.net/html_entity_decode
57 * @api
58 */
59 public function render($value = NULL, $keepQuotes = FALSE, $encoding = NULL) {
60 return static::renderStatic(
61 array(
62 'value' => $value,
63 'keepQuotes' => $keepQuotes,
64 'encoding' => $encoding
65 ),
66 $this->buildRenderChildrenClosure(),
67 $this->renderingContext
68 );
69 }
70
71 /**
72 * @param array $arguments
73 * @param callable $renderChildrenClosure
74 * @param RenderingContextInterface $renderingContext
75 *
76 * @return string
77 */
78 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
79 $value = $arguments['value'];
80 $encoding = $arguments['encoding'];
81 $keepQuotes = $arguments['keepQuotes'];
82
83 if ($value === NULL) {
84 $value = $renderChildrenClosure();
85 }
86 if (!is_string($value)) {
87 return $value;
88 }
89 if ($encoding === NULL) {
90 $encoding = self::resolveDefaultEncoding();
91 }
92 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
93 return html_entity_decode($value, $flags, $encoding);
94 }
95
96 }