-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
UrlencodeViewHelper.php
96 lines (88 loc) · 2.59 KB
/
UrlencodeViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Neos\FluidAdaptor\ViewHelpers\Format;
/*
* This file is part of the Neos.FluidAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use Neos\FluidAdaptor\Core\ViewHelper;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
/**
* Encodes the given string according to http://www.faqs.org/rfcs/rfc3986.html (applying PHPs rawurlencode() function)
*
* @see http://www.php.net/manual/function.urlencode.php
*
* = Examples =
*
* <code title="default notation">
* <f:format.urlencode>foo @+%/</f:format.urlencode>
* </code>
* <output>
* foo%20%40%2B%25%2F (rawurlencode() applied)
* </output>
*
* <code title="inline notation">
* {text -> f:format.urlencode()}
* </code>
* <output>
* Url encoded text (rawurlencode() applied)
* </output>
*
* @api
*/
class UrlencodeViewHelper extends AbstractViewHelper
{
/**
* @var boolean
*/
protected $escapeChildren = false;
/**
* Initialize the arguments.
*
* @return void
* @api
*/
public function initializeArguments()
{
$this->registerArgument('value', 'string', 'string to format', false, null);
}
/**
* Escapes special characters with their escaped counterparts as needed using PHPs urlencode() function.
*
* @return mixed
* @see http://www.php.net/manual/function.urlencode.php
* @api
* @throws ViewHelper\Exception
*/
public function render()
{
return self::renderStatic(['value' => $this->arguments['value']], $this->buildRenderChildrenClosure(), $this->renderingContext);
}
/**
* Applies rawurlencode() on the specified value.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws \Neos\FluidAdaptor\Core\ViewHelper\Exception
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$value = $arguments['value'];
if ($value === null) {
$value = $renderChildrenClosure();
}
if (is_object($value) && method_exists($value, '__toString')) {
$value = $value->__toString();
} elseif (!is_string($value)) {
return $value;
}
return rawurlencode($value);
}
}