1832d29b94bad9542c99c157dd1cef6e761e16bb
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
14 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
16
17 /**
18 * Encodes the given string according to http://www.faqs.org/rfcs/rfc3986.html (applying PHPs rawurlencode() function)
19 * @see http://www.php.net/manual/function.rawurlencode.php
20 *
21 * = Examples =
22 *
23 * <code title="default notation">
24 * <f:format.rawurlencode>foo @+%/</f:format.rawurlencode>
25 * </code>
26 * <output>
27 * foo%20%40%2B%25%2F (rawurlencode() applied)
28 * </output>
29 *
30 * <code title="inline notation">
31 * {text -> f:format.urlencode()}
32 * </code>
33 * <output>
34 * Url encoded text (rawurlencode() applied)
35 * </output>
36 *
37 * @api
38 */
39 class UrlencodeViewHelper extends AbstractViewHelper implements CompilableInterface {
40
41 /**
42 * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
43 * can decode the text's entities.
44 *
45 * @var bool
46 */
47 protected $escapingInterceptorEnabled = FALSE;
48
49 /**
50 * Escapes special characters with their escaped counterparts as needed using PHPs rawurlencode() function.
51 *
52 * @param string $value string to format
53 * @return mixed
54 * @see http://www.php.net/manual/function.rawurlencode.php
55 * @api
56 */
57 public function render($value = NULL) {
58 return self::renderStatic(
59 array(
60 'value' => $value
61 ),
62 $this->buildRenderChildrenClosure(),
63 $this->renderingContext
64 );
65 }
66
67 /**
68 * @param array $arguments
69 * @param callable $renderChildrenClosure
70 * @param RenderingContextInterface $renderingContext
71 *
72 * @return string
73 */
74 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
75 $value = $arguments['value'];
76
77 if ($value === NULL) {
78 $value = $renderChildrenClosure();
79 }
80 if (!is_string($value)) {
81 return $value;
82 }
83 return rawurlencode($value);
84 }
85 }