e4df5cbc08840dbc104190622f5807b9bae2b3ce
[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 * @var bool
45 */
46 protected $escapeOutput = false;
47
48 /**
49 * @var bool
50 */
51 protected $escapeChildren = false;
52
53 /**
54 * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
55 *
56 * @param string $value string to format
57 * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
58 * @param string $encoding
59 * @param bool $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
60 * @return string the altered string
61 * @see http://www.php.net/manual/function.htmlentities.php
62 * @api
63 */
64 public function render($value = null, $keepQuotes = false, $encoding = null, $doubleEncode = true)
65 {
66 if ($value === null) {
67 $value = $this->renderChildren();
68 }
69 if (!is_string($value)) {
70 return $value;
71 }
72 if ($encoding === null) {
73 $encoding = self::resolveDefaultEncoding();
74 }
75 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
76 return htmlentities($value, $flags, $encoding, $doubleEncode);
77 }
78 }