-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
CaseViewHelper.php
168 lines (153 loc) · 5.13 KB
/
CaseViewHelper.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?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 Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
use Neos\FluidAdaptor\Core\ViewHelper\Exception\InvalidVariableException;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* Modifies the case of an input string to upper- or lowercase or capitalization.
* The default transformation will be uppercase as in ``mb_convert_case`` [1].
*
* Possible modes are:
*
* ``lower``
* Transforms the input string to its lowercase representation
*
* ``upper``
* Transforms the input string to its uppercase representation
*
* ``capital``
* Transforms the input string to its first letter upper-cased, i.e. capitalization
*
* ``uncapital``
* Transforms the input string to its first letter lower-cased, i.e. uncapitalization
*
* ``capitalWords``
* Transforms the input string to each containing word being capitalized
*
* Note that the behavior will be the same as in the appropriate PHP function ``mb_convert_case`` [1];
* especially regarding locale and multibyte behavior.
*
* @see http://php.net/manual/function.mb-convert-case.php [1]
*
* = Examples =
*
* <code title="Example">
* <f:format.case>Some Text with miXed case</f:format.case>
* </code>
* <output>
* SOME TEXT WITH MIXED CASE
* </output>
*
* <code title="Example with given mode">
* <f:format.case mode="capital">someString</f:format.case>
* </code>
* <output>
* SomeString
* </output>
*
* <code title="Inline notation">
* {article.title -> f:format.case(mode: 'capitalWords')}
* </code>
* <output>
* Dolphins Vanish After A Surprisingly Sophisticated Attempt To Do A Double Backward Somersault
* </output>
*
* @api
*/
class CaseViewHelper extends AbstractViewHelper
{
/**
* @var boolean
*/
protected $escapeChildren = false;
/**
* Directs the input string being converted to "lowercase"
*/
const CASE_LOWER = 'lower';
/**
* Directs the input string being converted to "UPPERCASE"
*/
const CASE_UPPER = 'upper';
/**
* Directs the input string being converted to "Capital case"
*/
const CASE_CAPITAL = 'capital';
/**
* Directs the input string being converted to "unCapital case"
*/
const CASE_UNCAPITAL = 'uncapital';
/**
* Directs the input string being converted to "Capital Case For Each Word"
*/
const CASE_CAPITAL_WORDS = 'capitalWords';
/**
* Initialize the arguments.
*
* @return void
* @api
*/
public function initializeArguments()
{
$this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used', false, null);
$this->registerArgument('mode', 'string', 'The case to apply, must be one of this\' CASE_* constants. Defaults to uppercase application', false, self::CASE_UPPER);
}
/**
* Changes the case of the input string
*
* @return string the altered string.
* @throws InvalidVariableException
* @api
*/
public function render()
{
return self::renderStatic(['value' => $this->arguments['value'], 'mode' => $this->arguments['mode']], $this->buildRenderChildrenClosure(), $this->renderingContext);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws InvalidVariableException
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$value = $arguments['value'];
$mode = $arguments['mode'];
if ($value === null) {
$value = $renderChildrenClosure();
}
$originalEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
switch ($mode) {
case self::CASE_LOWER:
$output = mb_convert_case($value, \MB_CASE_LOWER);
break;
case self::CASE_UPPER:
$output = mb_convert_case($value, \MB_CASE_UPPER);
break;
case self::CASE_CAPITAL:
$output = mb_substr(mb_convert_case($value, \MB_CASE_UPPER), 0, 1) . mb_substr($value, 1);
break;
case self::CASE_UNCAPITAL:
$output = mb_substr(mb_convert_case($value, \MB_CASE_LOWER), 0, 1) . mb_substr($value, 1);
break;
case self::CASE_CAPITAL_WORDS:
$output = mb_convert_case($value, \MB_CASE_TITLE);
break;
default:
mb_internal_encoding($originalEncoding);
throw new InvalidVariableException('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
}
mb_internal_encoding($originalEncoding);
return $output;
}
}