d850c9f44318db2114e346954060664972fb4fe6
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / UrlencodeViewHelper.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 * Encodes the given string according to http://www.faqs.org/rfcs/rfc3986.html (applying PHPs rawurlencode() function)
22 * @see http://www.php.net/manual/function.rawurlencode.php
23 *
24 * = Examples =
25 *
26 * <code title="default notation">
27 * <f:format.rawurlencode>foo @+%/</f:format.rawurlencode>
28 * </code>
29 * <output>
30 * foo%20%40%2B%25%2F (rawurlencode() applied)
31 * </output>
32 *
33 * <code title="inline notation">
34 * {text -> f:format.urlencode()}
35 * </code>
36 * <output>
37 * Url encoded text (rawurlencode() applied)
38 * </output>
39 *
40 * @api
41 */
42 class UrlencodeViewHelper extends AbstractViewHelper
43 {
44 /**
45 * @var bool
46 */
47 protected $escapeOutput = false;
48
49 /**
50 * @var bool
51 */
52 protected $escapeChildren = false;
53
54 /**
55 * Escapes special characters with their escaped counterparts as needed using PHPs rawurlencode() function.
56 *
57 * @param string $value string to format
58 * @return mixed
59 * @see http://www.php.net/manual/function.rawurlencode.php
60 * @api
61 */
62 public function render($value = null)
63 {
64 return static::renderStatic(
65 array(
66 'value' => $value
67 ),
68 $this->buildRenderChildrenClosure(),
69 $this->renderingContext
70 );
71 }
72
73 /**
74 * @param array $arguments
75 * @param callable $renderChildrenClosure
76 * @param RenderingContextInterface $renderingContext
77 *
78 * @return string
79 */
80 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
81 {
82 $value = $arguments['value'];
83
84 if ($value === null) {
85 $value = $renderChildrenClosure();
86 }
87 if (!is_string($value)) {
88 return $value;
89 }
90 return rawurlencode($value);
91 }
92 }