* ``` * * @internal */ final class ArrayElementViewHelper extends AbstractViewHelper { public function initializeArguments(): void { $this->registerArgument('array', 'array', 'Array to search in', true); $this->registerArgument('key', 'string', 'Key to return its value', true); $this->registerArgument('subKey', 'string', 'If result of key access is an array, subkey can be used to fetch an element from this again', false, ''); } /** * Return array element by key. Accessed values must be scalar (string, int, float or double) * * @throws Exception */ public function render(): string { $array = $this->arguments['array']; $key = $this->arguments['key']; $subKey = $this->arguments['subKey']; $result = ''; if (isset($array[$key])) { $result = $array[$key]; if (is_array($result) && $subKey && isset($result[$subKey])) { $result = $result[$subKey]; } } if (!is_scalar($result)) { throw new Exception('Only scalar return values (string, int, float or double) are supported.', 1382284105); } return (string)$result; } }