[TASK] Streamline phpdoc annotations 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 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
19
20 /**
21 * Applies html_entity_decode() to a value
22 * @see http://www.php.net/html_entity_decode
23 *
24 * = Examples =
25 *
26 * <code title="default notation">
27 * <f:format.htmlentitiesDecode>{text}</f:format.htmlentitiesDecode>
28 * </code>
29 * <output>
30 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
31 * </output>
32 *
33 * <code title="inline notation">
34 * {text -> f:format.htmlentitiesDecode(encoding: 'ISO-8859-1')}
35 * </code>
36 * <output>
37 * Text with &amp; &quot; &lt; &gt; replaced by unescaped entities (html_entity_decode applied).
38 * </output>
39 */
40 class HtmlentitiesDecodeViewHelper extends AbstractEncodingViewHelper
41 {
42 use CompileWithContentArgumentAndRenderStatic;
43
44 /**
45 * We accept value and children interchangeably, thus we must disable children escaping.
46 *
47 * @var bool
48 */
49 protected $escapeChildren = false;
50
51 /**
52 * If we decode, we must not encode again after that.
53 *
54 * @var bool
55 */
56 protected $escapeOutput = false;
57
58 /**
59 * Initialize ViewHelper arguments
60 */
61 public function initializeArguments()
62 {
63 parent::initializeArguments();
64 $this->registerArgument('value', 'string', 'string to format');
65 $this->registerArgument('keepQuotes', 'bool', 'If TRUE, single and double quotes won\'t be replaced (sets ENT_NOQUOTES flag).', false, false);
66 $this->registerArgument('encoding', 'string', '');
67 }
68
69 /**
70 * Converts all HTML entities to their applicable characters as needed using PHPs html_entity_decode() function.
71 *
72 * @see http://www.php.net/html_entity_decode
73 * @param array $arguments
74 * @param \Closure $renderChildrenClosure
75 * @param RenderingContextInterface $renderingContext
76 *
77 * @return string
78 */
79 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
80 {
81 $value = $renderChildrenClosure();
82 $encoding = $arguments['encoding'];
83 $keepQuotes = $arguments['keepQuotes'];
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 }