‪TYPO3CMS  ‪main
CaseViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 
76 final class ‪CaseViewHelper extends AbstractViewHelper
77 {
78  use CompileWithRenderStatic;
79 
83  public const ‪CASE_LOWER = 'lower';
84 
88  public const ‪CASE_UPPER = 'upper';
89 
93  public const ‪CASE_CAPITAL = 'capital';
94 
98  public const ‪CASE_UNCAPITAL = 'uncapital';
99 
103  public const ‪CASE_CAPITAL_WORDS = 'capitalWords';
104 
110  protected ‪$escapeChildren = false;
111 
112  public function ‪initializeArguments(): void
113  {
114  $this->registerArgument('value', 'string', 'The input value. If not given, the evaluated child nodes will be used.', false);
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 
122  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
123  {
124  $value = $arguments['value'];
125  $mode = $arguments['mode'];
126 
127  if ($value === null) {
128  $value = (string)$renderChildrenClosure();
129  }
130 
131  switch ($mode) {
132  case ‪self::CASE_LOWER:
133  ‪$output = mb_strtolower($value, 'utf-8');
134  break;
135  case ‪self::CASE_UPPER:
136  ‪$output = mb_strtoupper($value, 'utf-8');
137  break;
139  $firstChar = mb_substr($value, 0, 1, 'utf-8');
140  $firstChar = mb_strtoupper($firstChar, 'utf-8');
141  $remainder = mb_substr($value, 1, null, 'utf-8');
142  ‪$output = $firstChar . $remainder;
143  break;
145  $firstChar = mb_substr($value, 0, 1, 'utf-8');
146  $firstChar = mb_strtolower($firstChar, 'utf-8');
147  $remainder = mb_substr($value, 1, null, 'utf-8');
148  ‪$output = $firstChar . $remainder;
149  break;
151  ‪$output = mb_convert_case($value, MB_CASE_TITLE, 'utf-8');
152  break;
153  default:
154  throw new Exception('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
155  }
156 
157  return ‪$output;
158  }
159 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_LOWER
‪const CASE_LOWER
Definition: CaseViewHelper.php:82
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\initializeArguments
‪initializeArguments()
Definition: CaseViewHelper.php:110
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CaseViewHelper.php:120
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL_WORDS
‪const CASE_CAPITAL_WORDS
Definition: CaseViewHelper.php:102
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UPPER
‪const CASE_UPPER
Definition: CaseViewHelper.php:87
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_UNCAPITAL
‪const CASE_UNCAPITAL
Definition: CaseViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CaseViewHelper.php:108
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper
Definition: CaseViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CaseViewHelper\CASE_CAPITAL
‪const CASE_CAPITAL
Definition: CaseViewHelper.php:92
‪$output
‪$output
Definition: annotationChecker.php:119