08e92549b66ed561338e64ec6792efa224fd7d01
[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 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
14 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
17
18 /**
19 * Case view helper that is only usable within the SwitchViewHelper.
20 * @see \TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper
21 *
22 * @api
23 */
24 class CaseViewHelper extends AbstractViewHelper implements CompilableInterface {
25
26 /**
27 * @param mixed $value The switch value. If it matches, the child will be rendered
28 * @param bool $default If this is set, this child will be rendered, if none else matches
29 *
30 * @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
31 * @throws Exception
32 *
33 * @api
34 */
35 public function render($value = NULL, $default = FALSE) {
36 return self::renderStatic(
37 array(
38 'value' => $value,
39 'default' => $default
40 ),
41 $this->buildRenderChildrenClosure(),
42 $this->renderingContext
43 );
44 }
45
46 /**
47 * @param array $arguments
48 * @param callable $renderChildrenClosure
49 * @param RenderingContextInterface $renderingContext
50 *
51 * @return mixed|string
52 * @throws Exception
53 */
54 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
55 $value = $arguments['value'];
56 $default = $arguments['default'];
57 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
58 if (!$viewHelperVariableContainer->exists(SwitchViewHelper::class, 'stateStack')) {
59 throw new Exception('The case View helper can only be used within a switch View helper', 1368112037);
60 }
61 if (is_null($value) && $default === FALSE) {
62 throw new Exception('The case View helper must have either value or default argument', 1382867521);
63 }
64 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
65 $currentState = array_pop($stateStack);
66
67 if ($currentState['break'] === TRUE) {
68 return '';
69 }
70
71 // non-type-safe comparison by intention
72 if ($default === TRUE || $currentState['expression'] == $value) {
73 $currentState['break'] = TRUE;
74 $stateStack[] = $currentState;
75 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
76 return $renderChildrenClosure();
77 }
78
79 return '';
80 }
81 }