[TASK] Move ViewHelper arguments to initializeArguments() in ext:fluid
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18
19 /**
20 * Applies html_entity_decode() to a value
21 * @see http://www.php.net/html_entity_decode
22 *
23 * = Examples =
24 *
25 * <code title="default notation">
26 * <f:format.htmlentitiesDecode>{text}</f:format.htmlentitiesDecode>
27 * </code>
28 * <output>
29 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
30 * </output>
31 *
32 * <code title="inline notation">
33 * {text -> f:format.htmlentitiesDecode(encoding: 'ISO-8859-1')}
34 * </code>
35 * <output>
36 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
37 * </output>
38 *
39 * @api
40 */
41 class HtmlentitiesDecodeViewHelper extends AbstractEncodingViewHelper
42 {
43 /**
44 * We accept value and children interchangeably, thus we must disable children escaping.
45 *
46 * @var bool
47 */
48 protected $escapeChildren = false;
49
50 /**
51 * If we decode, we must not encode again after that.
52 *
53 * @var bool
54 */
55 protected $escapeOutput = false;
56
57 /**
58 * Initialize ViewHelper arguments
59 */
60 public function initializeArguments()
61 {
62 parent::initializeArguments();
63 $this->registerArgument('value', 'string', 'string to format');
64 $this->registerArgument('keepQuotes', 'bool', 'If TRUE, single and double quotes won\'t be replaced (sets ENT_NOQUOTES flag).', false, false);
65 $this->registerArgument('encoding', 'string', '');
66 }
67
68 /**
69 * Converts all HTML entities to their applicable characters as needed using PHPs html_entity_decode() function.
70 *
71 * @return string the altered string
72 * @see http://www.php.net/html_entity_decode
73 */
74 public function render()
75 {
76 return static::renderStatic(
77 $this->arguments,
78 $this->buildRenderChildrenClosure(),
79 $this->renderingContext
80 );
81 }
82
83 /**
84 * @param array $arguments
85 * @param \Closure $renderChildrenClosure
86 * @param RenderingContextInterface $renderingContext
87 *
88 * @return string
89 */
90 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
91 {
92 $value = $arguments['value'];
93 $encoding = $arguments['encoding'];
94 $keepQuotes = $arguments['keepQuotes'];
95
96 if ($value === null) {
97 $value = $renderChildrenClosure();
98 }
99 if (!is_string($value)) {
100 return $value;
101 }
102 if ($encoding === null) {
103 $encoding = self::resolveDefaultEncoding();
104 }
105 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
106 return html_entity_decode($value, $flags, $encoding);
107 }
108 }