63c3eb85f3ab9cc6bd4e6d81dc8d19031582e6b1
[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 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
14 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
15
16 /**
17 * Applies html_entity_decode() to a value
18 * @see http://www.php.net/html_entity_decode
19 *
20 * = Examples =
21 *
22 * <code title="default notation">
23 * <f:format.htmlentitiesDecode>{text}</f:format.htmlentitiesDecode>
24 * </code>
25 * <output>
26 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
27 * </output>
28 *
29 * <code title="inline notation">
30 * {text -> f:format.htmlentitiesDecode(encoding: 'ISO-8859-1')}
31 * </code>
32 * <output>
33 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
34 * </output>
35 *
36 * @api
37 */
38 class HtmlentitiesDecodeViewHelper extends AbstractEncodingViewHelper implements CompilableInterface {
39
40 /**
41 * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
42 * can decode the text's entities.
43 *
44 * @var bool
45 */
46 protected $escapingInterceptorEnabled = FALSE;
47
48 /**
49 * Converts all HTML entities to their applicable characters as needed using PHPs html_entity_decode() function.
50 *
51 * @param string $value string to format
52 * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
53 * @param string $encoding
54 * @return string the altered string
55 * @see http://www.php.net/html_entity_decode
56 * @api
57 */
58 public function render($value = NULL, $keepQuotes = FALSE, $encoding = NULL) {
59 return self::renderStatic(
60 array(
61 'value' => $value,
62 'keepQuotes' => $keepQuotes,
63 'encoding' => $encoding
64 ),
65 $this->buildRenderChildrenClosure(),
66 $this->renderingContext
67 );
68 }
69
70 /**
71 * @param array $arguments
72 * @param callable $renderChildrenClosure
73 * @param RenderingContextInterface $renderingContext
74 *
75 * @return string
76 */
77 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
78 $value = $arguments['value'];
79 $encoding = $arguments['encoding'];
80 $keepQuotes = $arguments['keepQuotes'];
81
82 if ($value === NULL) {
83 $value = $renderChildrenClosure();
84 }
85 if (!is_string($value)) {
86 return $value;
87 }
88 if ($encoding === NULL) {
89 $encoding = self::resolveDefaultEncoding();
90 }
91 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
92 return html_entity_decode($value, $flags, $encoding);
93 }
94
95 }