‪TYPO3CMS  10.4
CaseViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22 
74 class ‪CaseViewHelper extends AbstractViewHelper
75 {
76  use CompileWithRenderStatic;
77 
81  const ‪CASE_LOWER = 'lower';
82 
86  const ‪CASE_UPPER = 'upper';
87 
91  const ‪CASE_CAPITAL = 'capital';
92 
96  const ‪CASE_UNCAPITAL = 'uncapital';
97 
101  const ‪CASE_CAPITAL_WORDS = 'capitalWords';
102 
108  protected ‪$escapeChildren = false;
109 
113  public function ‪initializeArguments()
114  {
115  $this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used.', false, null);
116  $this->registerArgument('mode', 'string', 'The case to apply, must be one of this\' CASE_* constants. Defaults to uppercase application.', false, self::CASE_UPPER);
117  }
118 
128  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
129  {
130  $value = $arguments['value'];
131  $mode = $arguments['mode'];
132 
133  if ($value === null) {
134  $value = $renderChildrenClosure();
135  }
136 
137  switch ($mode) {
138  case ‪self::CASE_LOWER:
139  ‪$output = mb_strtolower($value, 'utf-8');
140  break;
141  case ‪self::CASE_UPPER:
142  ‪$output = mb_strtoupper($value, 'utf-8');
143  break;
145  $firstChar = mb_substr($value, 0, 1, 'utf-8');
146  $firstChar = mb_strtoupper($firstChar, 'utf-8');
147  $remainder = mb_substr($value, 1, null, 'utf-8');
148  ‪$output = $firstChar . $remainder;
149  break;
151  $firstChar = mb_substr($value, 0, 1, 'utf-8');
152  $firstChar = mb_strtolower($firstChar, 'utf-8');
153  $remainder = mb_substr($value, 1, null, 'utf-8');
154  ‪$output = $firstChar . $remainder;
155  break;
157  ‪$output = mb_convert_case($value, MB_CASE_TITLE, 'utf-8');
158  break;
159  default:
160  throw new Exception('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
161  }
162 
163  return ‪$output;
164  }
165 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_LOWER
‪const CASE_LOWER
Definition: CaseViewHelper.php:80
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\initializeArguments
‪initializeArguments()
Definition: CaseViewHelper.php:111
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL_WORDS
‪const CASE_CAPITAL_WORDS
Definition: CaseViewHelper.php:100
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UPPER
‪const CASE_UPPER
Definition: CaseViewHelper.php:85
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CaseViewHelper.php:126
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UNCAPITAL
‪const CASE_UNCAPITAL
Definition: CaseViewHelper.php:95
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CaseViewHelper.php:106
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper
Definition: CaseViewHelper.php:75
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL
‪const CASE_CAPITAL
Definition: CaseViewHelper.php:90
‪$output
‪$output
Definition: annotationChecker.php:119