‪TYPO3CMS  ‪main
ArrayElementViewHelper.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 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 
30 final class ‪ArrayElementViewHelper extends AbstractViewHelper
31 {
32  use CompileWithRenderStatic;
33 
34  public function ‪initializeArguments(): void
35  {
36  $this->registerArgument('array', 'array', 'Array to search in', true);
37  $this->registerArgument('key', 'string', 'Key to return its value', true);
38  $this->registerArgument('subKey', 'string', 'If result of key access is an array, subkey can be used to fetch an element from this again', false, '');
39  }
40 
47  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
48  {
49  $array = $arguments['array'];
50  $key = $arguments['key'];
51  $subKey = $arguments['subKey'];
52  $result = '';
53  if (isset($array[$key])) {
54  $result = $array[$key];
55  if (is_array($result) && $subKey && isset($result[$subKey])) {
56  $result = $result[$subKey];
57  }
58  }
59  if (!is_scalar($result)) {
60  throw new Exception('Only scalar return values (string, int, float or double) are supported.', 1382284105);
61  }
62  return (string)$result;
63  }
64 }
‪TYPO3\CMS\Beuser\Exception
Definition: Exception.php:24
‪TYPO3\CMS\Beuser\ViewHelpers
Definition: ArrayElementViewHelper.php:18
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper\initializeArguments
‪initializeArguments()
Definition: ArrayElementViewHelper.php:33
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper
Definition: ArrayElementViewHelper.php:31
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ArrayElementViewHelper.php:46