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