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