[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / CaseViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
18
19 /**
20 * Case view helper that is only usable within the SwitchViewHelper.
21 * @see \TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper
22 *
23 * @api
24 */
25 class CaseViewHelper extends AbstractViewHelper implements CompilableInterface {
26
27 /**
28 * @param mixed $value The switch value. If it matches, the child will be rendered
29 * @param bool $default If this is set, this child will be rendered, if none else matches
30 *
31 * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, or $default is TRUE. otherwise an empty string
32 * @throws Exception
33 *
34 * @api
35 */
36 public function render($value = NULL, $default = FALSE) {
37 return static::renderStatic(
38 array(
39 'value' => $value,
40 'default' => $default
41 ),
42 $this->buildRenderChildrenClosure(),
43 $this->renderingContext
44 );
45 }
46
47 /**
48 * @param array $arguments
49 * @param callable $renderChildrenClosure
50 * @param RenderingContextInterface $renderingContext
51 *
52 * @return mixed|string
53 * @throws Exception
54 */
55 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
56 $value = $arguments['value'];
57 $default = $arguments['default'];
58 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
59 if (!$viewHelperVariableContainer->exists(SwitchViewHelper::class, 'stateStack')) {
60 throw new Exception('The case View helper can only be used within a switch View helper', 1368112037);
61 }
62 if (is_null($value) && $default === FALSE) {
63 throw new Exception('The case View helper must have either value or default argument', 1382867521);
64 }
65 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
66 $currentState = array_pop($stateStack);
67
68 if ($currentState['break'] === TRUE) {
69 return '';
70 }
71
72 // non-type-safe comparison by intention
73 if ($default === TRUE || $currentState['expression'] == $value) {
74 $currentState['break'] = TRUE;
75 $stateStack[] = $currentState;
76 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
77 return $renderChildrenClosure();
78 }
79
80 return '';
81 }
82
83 }