‪TYPO3CMS  9.5
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 
17 use TYPO3Fluid\Fluid\Core\Exception;
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
21 
73 class ‪CaseViewHelper extends AbstractViewHelper
74 {
75  use CompileWithRenderStatic;
76 
80  const ‪CASE_LOWER = 'lower';
81 
85  const ‪CASE_UPPER = 'upper';
86 
90  const ‪CASE_CAPITAL = 'capital';
91 
95  const ‪CASE_UNCAPITAL = 'uncapital';
96 
100  const ‪CASE_CAPITAL_WORDS = 'capitalWords';
101 
107  protected ‪$escapeChildren = false;
108 
112  public function ‪initializeArguments()
113  {
114  $this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used.', false, null);
115  $this->registerArgument('mode', 'string', 'The case to apply, must be one of this\' CASE_* constants. Defaults to uppercase application.', false, self::CASE_UPPER);
116  }
117 
127  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
128  {
129  $value = $arguments['value'];
130  $mode = $arguments['mode'];
131 
132  if ($value === null) {
133  $value = $renderChildrenClosure();
134  }
135 
136  switch ($mode) {
137  case ‪self::CASE_LOWER:
138  ‪$output = mb_strtolower($value, 'utf-8');
139  break;
140  case ‪self::CASE_UPPER:
141  ‪$output = mb_strtoupper($value, 'utf-8');
142  break;
144  $firstChar = mb_substr($value, 0, 1, 'utf-8');
145  $firstChar = mb_strtoupper($firstChar, 'utf-8');
146  $remainder = mb_substr($value, 1, null, 'utf-8');
147  ‪$output = $firstChar . $remainder;
148  break;
150  $firstChar = mb_substr($value, 0, 1, 'utf-8');
151  $firstChar = mb_strtolower($firstChar, 'utf-8');
152  $remainder = mb_substr($value, 1, null, 'utf-8');
153  ‪$output = $firstChar . $remainder;
154  break;
156  ‪$output = mb_convert_case($value, MB_CASE_TITLE, 'utf-8');
157  break;
158  default:
159  throw new Exception('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
160  }
161 
162  return ‪$output;
163  }
164 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_LOWER
‪const CASE_LOWER
Definition: CaseViewHelper.php:79
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\initializeArguments
‪initializeArguments()
Definition: CaseViewHelper.php:110
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL_WORDS
‪const CASE_CAPITAL_WORDS
Definition: CaseViewHelper.php:99
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UPPER
‪const CASE_UPPER
Definition: CaseViewHelper.php:84
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CaseViewHelper.php:125
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UNCAPITAL
‪const CASE_UNCAPITAL
Definition: CaseViewHelper.php:94
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CaseViewHelper.php:105
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper
Definition: CaseViewHelper.php:74
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL
‪const CASE_CAPITAL
Definition: CaseViewHelper.php:89
‪$output
‪$output
Definition: annotationChecker.php:113