[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlentitiesViewHelper.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 htmlentities() escaping to a value
22 * @see http://www.php.net/manual/function.htmlentities.php
23 *
24 * = Examples =
25 *
26 * <code title="default notation">
27 * <f:format.htmlentities>{text}</f:format.htmlentities>
28 * </code>
29 * <output>
30 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
31 * </output>
32 *
33 * <code title="inline notation">
34 * {text -> f:format.htmlentities(encoding: 'ISO-8859-1')}
35 * </code>
36 * <output>
37 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
38 * </output>
39 */
40 class HtmlentitiesViewHelper extends AbstractEncodingViewHelper
41 {
42 use CompileWithContentArgumentAndRenderStatic;
43
44 /**
45 * Output gets encoded by this viewhelper
46 *
47 * @var bool
48 */
49 protected $escapeOutput = false;
50
51 /**
52 * This prevents double encoding as the whole output gets encoded at the end
53 *
54 * @var bool
55 */
56 protected $escapeChildren = false;
57
58 /**
59 * Initialize ViewHelper arguments
60 */
61 public function initializeArguments()
62 {
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 $this->registerArgument('doubleEncode', 'bool', 'If FALSE existing html entities won\'t be encoded, the default is to convert everything.', false, true);
67 }
68
69 /**
70 * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
71 *
72 * @see http://www.php.net/manual/function.htmlentities.php
73 * @param array $arguments
74 * @param \Closure $renderChildrenClosure
75 * @param RenderingContextInterface $renderingContext
76 * @return mixed
77 */
78 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
79 {
80 $value = $renderChildrenClosure();
81 $encoding = $arguments['encoding'];
82 $keepQuotes = $arguments['keepQuotes'];
83 $doubleEncode = $arguments['doubleEncode'];
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 htmlentities($value, $flags, $encoding, $doubleEncode);
93 }
94 }