[TASK] Use csConv API methods
[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\Core\Charset\CharsetConverter;
15 use TYPO3\CMS\Core\Utility\GeneralUtility;
16 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
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 protected static $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 {
107 return static::renderStatic(
108 array(
109 'value' => $value,
110 'mode' => $mode,
111 ),
112 $this->buildRenderChildrenClosure(),
113 $this->renderingContext
114 );
115 }
116
117 /**
118 * Changes the case of the input string
119 *
120 * @param array $arguments
121 * @param \Closure $renderChildrenClosure
122 * @param RenderingContextInterface $renderingContext
123 * @return string
124 * @throws InvalidVariableException
125 */
126 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
127 {
128 $value = $arguments['value'];
129 $mode = $arguments['mode'];
130
131 if ($value === null) {
132 $value = $renderChildrenClosure();
133 }
134
135 if (is_null(static::$charsetConverter)) {
136 static::$charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
137 }
138 $charsetConverter = static::$charsetConverter;
139
140 switch ($mode) {
141 case self::CASE_LOWER:
142 $output = $charsetConverter->conv_case('utf-8', $value, 'toLower');
143 break;
144 case self::CASE_UPPER:
145 $output = $charsetConverter->conv_case('utf-8', $value, 'toUpper');
146 break;
147 case self::CASE_CAPITAL:
148 $output = $charsetConverter->substr('utf-8', $charsetConverter->convCaseFirst('utf-8', $value, 'toUpper'), 0, 1) . $charsetConverter->substr('utf-8', $value, 1);
149 break;
150 case self::CASE_UNCAPITAL:
151 $output = $charsetConverter->substr('utf-8', $charsetConverter->convCaseFirst('utf-8', $value, 'toLower'), 0, 1) . $charsetConverter->substr('utf-8', $value, 1);
152 break;
153 case self::CASE_CAPITAL_WORDS:
154 // @todo: Implement method once there is a proper solution with using the CharsetConverter
155 default:
156 throw new InvalidVariableException('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
157 }
158
159 return $output;
160 }
161 }