‪TYPO3CMS  10.4
ArrayElementViewHelper.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 
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22 
27 class ‪ArrayElementViewHelper extends AbstractViewHelper
28 {
29  use CompileWithRenderStatic;
30 
34  public function ‪initializeArguments()
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 
50  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
51  {
52  $array = $arguments['array'];
53  $key = $arguments['key'];
54  $subKey = $arguments['subKey'];
55  $result = '';
56  if (is_array($array) && isset($array[$key])) {
57  $result = $array[$key];
58  if (is_array($result) && $subKey && isset($result[$subKey])) {
59  $result = $result[$subKey];
60  }
61  }
62  if (!is_scalar($result)) {
63  throw new ‪Exception('Only scalar return values (string, int, float or double) are supported.', 1382284105);
64  }
65  return $result;
66  }
67 }
‪TYPO3\CMS\Beuser\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Beuser\ViewHelpers
Definition: ArrayElementViewHelper.php:16
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ArrayElementViewHelper.php:49
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper\initializeArguments
‪initializeArguments()
Definition: ArrayElementViewHelper.php:33
‪TYPO3\CMS\Beuser\ViewHelpers\ArrayElementViewHelper
Definition: ArrayElementViewHelper.php:28