[FEATURE] Add "allowedTags" argument on f:format.stripTags
[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 TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20 /**
21 * Removes tags from the given string (applying PHPs strip_tags() function)
22 *
23 * @see http://www.php.net/manual/function.strip-tags.php
24 *
25 * = Examples =
26 *
27 * <code title="default notation">
28 * <f:format.stripTags>Some Text with <b>Tags</b> and an &Uuml;mlaut.</f:format.stripTags>
29 * </code>
30 * <output>
31 * Some Text with Tags and an &Uuml;mlaut. (strip_tags() applied. Note: encoded entities are not decoded)
32 * </output>
33 *
34 * <code title="inline notation">
35 * {text -> f:format.stripTags()}
36 * </code>
37 * <output>
38 * Text without tags (strip_tags() applied)
39 * </output>
40 *
41 * @api
42 */
43 class StripTagsViewHelper extends AbstractViewHelper
44 {
45 /**
46 * @var bool
47 */
48 protected $escapeOutput = false;
49
50 /**
51 * @var bool
52 */
53 protected $escapeChildren = false;
54
55 /**
56 * Escapes special characters with their escaped counterparts as needed using PHPs strip_tags() function.
57 *
58 * @param string $value string to format
59 * @param string $allowedTags Optional string of allowed tags as required by PHPs strip_tags() function
60 * @return mixed
61 * @see http://www.php.net/manual/function.strip-tags.php
62 * @api
63 */
64 public function render($value = null, $allowedTags = null)
65 {
66 return static::renderStatic(
67 array(
68 'value' => $value,
69 'allowedTags' => $allowedTags
70 ),
71 $this->buildRenderChildrenClosure(),
72 $this->renderingContext
73 );
74 }
75
76 /**
77 * Applies strip_tags() on the specified value.
78 *
79 * @param array $arguments
80 * @param \Closure $renderChildrenClosure
81 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
82 * @return string
83 */
84 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
85 {
86 $value = $arguments['value'];
87 if ($value === null) {
88 $value = $renderChildrenClosure();
89 }
90 if (!is_string($value)) {
91 return $value;
92 }
93 return strip_tags($value, (string)$arguments['allowedTags']);
94 }
95 }