[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / StripTagsViewHelper.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\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
20
21 /**
22 * Removes tags from the given string (applying PHPs strip_tags() function)
23 *
24 * @see http://www.php.net/manual/function.strip-tags.php
25 *
26 * = Examples =
27 *
28 * <code title="default notation">
29 * <f:format.stripTags>Some Text with <b>Tags</b> and an &Uuml;mlaut.</f:format.stripTags>
30 * </code>
31 * <output>
32 * Some Text with Tags and an &Uuml;mlaut. (strip_tags() applied. Note: encoded entities are not decoded)
33 * </output>
34 *
35 * <code title="default notation with allowedTags">
36 * <f:format.stripTags allowedTags="<p><span><div><script>"><p>paragraph</p><span>span</span><div>divider</div><iframe>iframe</iframe><script>script</script></f:format.stripTags>
37 * </code>
38 * <output>
39 * <p>paragraph</p><span>span</span><div>divider</div>iframe<script>script</script>
40 * </output>
41 *
42 * <code title="inline notation">
43 * {text -> f:format.stripTags()}
44 * </code>
45 * <output>
46 * Text without tags (strip_tags() applied)
47 * </output>
48 *
49 * <code title="inline notation with allowedTags">
50 * {text -> f:format.stripTags(allowedTags: "<p><span><div><script>")}
51 * </code>
52 * <output>
53 * Text with p, span, div and script Tags inside, all other tags are removed
54 * </output>
55 */
56 class StripTagsViewHelper extends AbstractViewHelper
57 {
58 use CompileWithContentArgumentAndRenderStatic;
59
60 /**
61 * No output escaping as some tags may be allowed
62 *
63 * @var bool
64 */
65 protected $escapeOutput = false;
66
67 /**
68 * Initialize ViewHelper arguments
69 *
70 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
71 */
72 public function initializeArguments()
73 {
74 $this->registerArgument('value', 'string', 'string to format');
75 $this->registerArgument('allowedTags', 'string', 'Optional string of allowed tags as required by PHPs strip_tags() f
76 unction');
77 }
78
79 /**
80 * To ensure all tags are removed, child node's output must not be escaped
81 *
82 * @var bool
83 */
84 protected $escapeChildren = false;
85
86 /**
87 * Applies strip_tags() on the specified value.
88 *
89 * @param array $arguments
90 * @param \Closure $renderChildrenClosure
91 * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
92 * @see http://www.php.net/manual/function.strip-tags.php
93 * @return string
94 */
95 public static function renderStatic(
96 array $arguments,
97 \Closure $renderChildrenClosure,
98 RenderingContextInterface $renderingContext
99 ) {
100 $value = $renderChildrenClosure();
101 $allowedTags = $arguments['allowedTags'];
102 if (!is_string($value)) {
103 return $value;
104 }
105 return strip_tags($value, $allowedTags);
106 }
107 }