[FOLLOWUP][TASK] Move ViewHelper arguments to initializeParams
[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 TYPO3\CMS\Core\SingletonInterface;
18
19 /**
20 * Applies htmlentities() escaping to a value
21 * @see http://www.php.net/manual/function.htmlentities.php
22 *
23 * = Examples =
24 *
25 * <code title="default notation">
26 * <f:format.htmlentities>{text}</f:format.htmlentities>
27 * </code>
28 * <output>
29 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
30 * </output>
31 *
32 * <code title="inline notation">
33 * {text -> f:format.htmlentities(encoding: 'ISO-8859-1')}
34 * </code>
35 * <output>
36 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
37 * </output>
38 *
39 * @api
40 */
41 class HtmlentitiesViewHelper extends AbstractEncodingViewHelper implements SingletonInterface
42 {
43 /**
44 * Output gets encoded by this viewhelper
45 *
46 * @var bool
47 */
48 protected $escapeOutput = false;
49
50 /**
51 * This prevents double encoding as the whole output gets encoded at the end
52 *
53 * @var bool
54 */
55 protected $escapeChildren = false;
56
57 /**
58 * Initialize ViewHelper arguments
59 *
60 * @return void
61 */
62 public function initializeArguments()
63 {
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 $this->registerArgument('doubleEncode', 'bool', 'If FALSE existing html entities won\'t be encoded, the default is to convert everything.', false, true);
68 }
69
70 /**
71 * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
72 *
73 * @return string the altered string
74 * @see http://www.php.net/manual/function.htmlentities.php
75 * @api
76 */
77 public function render()
78 {
79 $value = $this->arguments['value'];
80 $encoding = $this->arguments['encoding'];
81 $keepQuotes = $this->arguments['keepQuotes'];
82 $doubleEncode = $this->arguments['doubleEncode'];
83
84 if ($value === null) {
85 $value = $this->renderChildren();
86 }
87 if (!is_string($value)) {
88 return $value;
89 }
90 if ($encoding === null) {
91 $encoding = self::resolveDefaultEncoding();
92 }
93 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
94 return htmlentities($value, $flags, $encoding, $doubleEncode);
95 }
96 }