[BUGFIX] Use late static binding for compilable viewhelpers
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
16 use TYPO3\CMS\Core\Charset\CharsetConverter;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
19 use TYPO3\CMS\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 implements CompilableInterface {
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 * @var NULL|CharsetConverter
94 */
95 static protected $charsetConverter = NULL;
96
97 /**
98 * Changes the case of the input string
99 *
100 * @param string $value The input value. If not given, the evaluated child nodes will be used
101 * @param string $mode The case to apply, must be one of this' CASE_* constants. Defaults to uppercase application
102 * @return string the altered string.
103 * @api
104 */
105 public function render($value = NULL, $mode = self::CASE_UPPER) {
106 return static::renderStatic(
107 array(
108 'value' => $value,
109 'mode' => $mode,
110 ),
111 $this->buildRenderChildrenClosure(),
112 $this->renderingContext
113 );
114 }
115
116 /**
117 * Changes the case of the input string
118 *
119 * @param array $arguments
120 * @param \Closure $renderChildrenClosure
121 * @param RenderingContextInterface $renderingContext
122 * @return string
123 * @throws InvalidVariableException
124 */
125 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
126 $value = $arguments['value'];
127 $mode = $arguments['mode'];
128
129 if ($value === NULL) {
130 $value = $renderChildrenClosure();
131 }
132
133 if (is_null(static::$charsetConverter)) {
134 static::$charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
135 }
136 $charsetConverter = static::$charsetConverter;
137
138 switch ($mode) {
139 case self::CASE_LOWER:
140 $output = $charsetConverter->conv_case('utf-8', $value, 'toLower');
141 break;
142 case self::CASE_UPPER:
143 $output = $charsetConverter->conv_case('utf-8', $value, 'toUpper');
144 break;
145 case self::CASE_CAPITAL:
146 $output = $charsetConverter->utf8_substr($charsetConverter->convCaseFirst('utf-8', $value, 'toUpper'), 0, 1) . $charsetConverter->utf8_substr($value, 1);
147 break;
148 case self::CASE_UNCAPITAL:
149 $output = $charsetConverter->utf8_substr($charsetConverter->convCaseFirst('utf-8', $value, 'toLower'), 0, 1) . $charsetConverter->utf8_substr($value, 1);
150 break;
151 case self::CASE_CAPITAL_WORDS:
152 // @todo: Implement method once there is a proper solution with using the CharsetConverter
153 default:
154 throw new InvalidVariableException('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
155 }
156
157 return $output;
158 }
159
160 }