TYPO3 CMS  TYPO3_8-7
CaseViewHelper.php
Go to the documentation of this file.
1 <?php
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 
20 
66 {
70  const CASE_LOWER = 'lower';
71 
75  const CASE_UPPER = 'upper';
76 
80  const CASE_CAPITAL = 'capital';
81 
85  const CASE_UNCAPITAL = 'uncapital';
86 
90  const CASE_CAPITAL_WORDS = 'capitalWords';
91 
97  protected $escapeChildren = false;
98 
102  public function initializeArguments()
103  {
104  $this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used.', false, null);
105  $this->registerArgument('mode', 'string', 'The case to apply, must be one of this\' CASE_* constants. Defaults to uppercase application.', false, self::CASE_UPPER);
106  }
107 
114  public function render()
115  {
116  return static::renderStatic(
117  [
118  'value' => $this->arguments['value'],
119  'mode' => $this->arguments['mode']
120  ],
121  $this->buildRenderChildrenClosure(),
122  $this->renderingContext
123  );
124  }
125 
135  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
136  {
137  $value = $arguments['value'];
138  $mode = $arguments['mode'];
139 
140  if ($value === null) {
141  $value = $renderChildrenClosure();
142  }
143 
144  switch ($mode) {
145  case self::CASE_LOWER:
146  $output = mb_strtolower($value, 'utf-8');
147  break;
148  case self::CASE_UPPER:
149  $output = mb_strtoupper($value, 'utf-8');
150  break;
151  case self::CASE_CAPITAL:
152  $firstChar = mb_substr($value, 0, 1, 'utf-8');
153  $firstChar = mb_strtoupper($firstChar, 'utf-8');
154  $remainder = mb_substr($value, 1, null, 'utf-8');
155  $output = $firstChar . $remainder;
156  break;
157  case self::CASE_UNCAPITAL:
158  $firstChar = mb_substr($value, 0, 1, 'utf-8');
159  $firstChar = mb_strtolower($firstChar, 'utf-8');
160  $remainder = mb_substr($value, 1, null, 'utf-8');
161  $output = $firstChar . $remainder;
162  break;
163  case self::CASE_CAPITAL_WORDS:
164  $output = mb_convert_case($value, MB_CASE_TITLE, 'utf-8');
165  break;
166  default:
167  throw new InvalidVariableException('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
168  }
169 
170  return $output;
171  }
172 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)