[TASK] Move ViewHelper arguments to initializeArguments() in ext:install
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / ViewHelpers / Object / ArrayValueByKeyViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Install\ViewHelpers\Object;
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 TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Install\ViewHelpers\Exception;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
21 /**
22 * View helper which allows you to access a key in an array.
23 *
24 * = Examples =
25 *
26 * <code title="Defaults">
27 * <i:object.key array="{array}" key="{key}" />
28 * </code>
29 * <output>
30 * The key in the array, if it exists, otherwise an empty string.
31 * </output>
32 *
33 * @internal
34 */
35 class ArrayValueByKeyViewHelper extends AbstractViewHelper
36 {
37 /**
38 * Initialize arguments
39 */
40 public function initializeArguments()
41 {
42 parent::initializeArguments();
43 $this->registerArgument('array', 'array', 'The array being processed', true);
44 $this->registerArgument('key', 'mixed', 'The key being accessed', true);
45 }
46
47 /**
48 * Get the value of an key in an array.
49 *
50 * @return string
51 */
52 public function render()
53 {
54 return static::renderStatic(
55 $this->arguments,
56 $this->buildRenderChildrenClosure(),
57 $this->renderingContext
58 );
59 }
60
61 /**
62 * @param array $arguments
63 * @param \Closure $renderChildrenClosure
64 * @param RenderingContextInterface $renderingContext
65 *
66 * @return string
67 * @throws Exception
68 */
69 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
70 {
71 $array = $arguments['array'];
72 $key = $arguments['key'];
73 $result = '';
74 if (isset($array[$key])) {
75 $result = $array[$key];
76 }
77 if (!is_scalar($result)) {
78 throw new Exception(
79 'Only scalar return values (string, int, float or double) are supported.',
80 1430852128
81 );
82 }
83 return (string)$result;
84 }
85 }