[BUGFIX] Add missing format.case capitalWords mode
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CaseViewHelper.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\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
21 /**
22 * Modifies the case of an input string to upper- or lowercase or capitalization.
23 * The default transformation will be uppercase as in ``mb_convert_case`` [1].
24 *
25 * Possible modes are:
26 *
27 * ``lower``
28 * Transforms the input string to its lowercase representation
29 *
30 * ``upper``
31 * Transforms the input string to its uppercase representation
32 *
33 * ``capital``
34 * Transforms the input string to its first letter upper-cased, i.e. capitalization
35 *
36 * ``uncapital``
37 * Transforms the input string to its first letter lower-cased, i.e. uncapitalization
38 *
39 * ``capitalWords``
40 * Not supported yet: Transforms the input string to each containing word being capitalized
41 *
42 * Note that the behavior will be the same as in the appropriate PHP function ``mb_convert_case`` [1];
43 * especially regarding locale and multibyte behavior.
44 *
45 * @see http://php.net/manual/function.mb-convert-case.php [1]
46 *
47 * = Examples =
48 *
49 * <code title="Example">
50 * <f:format.case>Some Text with miXed case</f:format.case>
51 * </code>
52 * <output>
53 * SOME TEXT WITH MIXED CASE
54 * </output>
55 *
56 * <code title="Example with given mode">
57 * <f:format.case mode="capital">someString</f:format.case>
58 * </code>
59 * <output>
60 * SomeString
61 * </output>
62 *
63 * @api
64 */
65 class CaseViewHelper extends AbstractViewHelper
66 {
67 /**
68 * Directs the input string being converted to "lowercase"
69 */
70 const CASE_LOWER = 'lower';
71
72 /**
73 * Directs the input string being converted to "UPPERCASE"
74 */
75 const CASE_UPPER = 'upper';
76
77 /**
78 * Directs the input string being converted to "Capital case"
79 */
80 const CASE_CAPITAL = 'capital';
81
82 /**
83 * Directs the input string being converted to "unCapital case"
84 */
85 const CASE_UNCAPITAL = 'uncapital';
86
87 /**
88 * Directs the input string being converted to "Capital Case For Each Word"
89 */
90 const CASE_CAPITAL_WORDS = 'capitalWords';
91
92 /**
93 * Output is escaped already. We must not escape children, to avoid double encoding.
94 *
95 * @var bool
96 */
97 protected $escapeChildren = false;
98
99 /**
100 * Initialize ViewHelper arguments
101 */
102 public function initializeArguments()
103 {
104 $this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used.', false, null);
105 $this->registerArgument('mode', 'string', 'The case to apply, must be one of this\' CASE_* constants. Defaults to uppercase application.', false, self::CASE_UPPER);
106 }
107
108 /**
109 * Changes the case of the input string
110 *
111 * @return string the altered string.
112 * @api
113 */
114 public function render()
115 {
116 return static::renderStatic(
117 [
118 'value' => $this->arguments['value'],
119 'mode' => $this->arguments['mode']
120 ],
121 $this->buildRenderChildrenClosure(),
122 $this->renderingContext
123 );
124 }
125
126 /**
127 * Changes the case of the input string
128 *
129 * @param array $arguments
130 * @param \Closure $renderChildrenClosure
131 * @param RenderingContextInterface $renderingContext
132 * @return string
133 * @throws InvalidVariableException
134 */
135 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
136 {
137 $value = $arguments['value'];
138 $mode = $arguments['mode'];
139
140 if ($value === null) {
141 $value = $renderChildrenClosure();
142 }
143
144 switch ($mode) {
145 case self::CASE_LOWER:
146 $output = mb_strtolower($value, 'utf-8');
147 break;
148 case self::CASE_UPPER:
149 $output = mb_strtoupper($value, 'utf-8');
150 break;
151 case self::CASE_CAPITAL:
152 $firstChar = mb_substr($value, 0, 1, 'utf-8');
153 $firstChar = mb_strtoupper($firstChar, 'utf-8');
154 $remainder = mb_substr($value, 1, null, 'utf-8');
155 $output = $firstChar . $remainder;
156 break;
157 case self::CASE_UNCAPITAL:
158 $firstChar = mb_substr($value, 0, 1, 'utf-8');
159 $firstChar = mb_strtolower($firstChar, 'utf-8');
160 $remainder = mb_substr($value, 1, null, 'utf-8');
161 $output = $firstChar . $remainder;
162 break;
163 case self::CASE_CAPITAL_WORDS:
164 $output = mb_convert_case($value, MB_CASE_TITLE, 'utf-8');
165 break;
166 default:
167 throw new InvalidVariableException('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
168 }
169
170 return $output;
171 }
172 }