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