[BUGFIX] Switch ViewHelper restored for safe deprecation
[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\Core\Utility\GeneralUtility;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper as OriginalSwitchViewHelper;
19
20 /**
21 * Case view helper that is only usable within the SwitchViewHelper.
22 * @see \TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper
23 *
24 * @api
25 */
26 class CaseViewHelper extends AbstractViewHelper
27 {
28 /**
29 * @var bool
30 */
31 protected $escapeOutput = false;
32
33 /**
34 * @var bool
35 */
36 protected $escapeChildren = false;
37
38 /**
39 * @param mixed $value The switch value. If it matches, the child will be rendered
40 * @param bool $default If this is set, this child will be rendered, if none else matches
41 *
42 * @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
43 * @throws Exception
44 *
45 * @api
46 */
47 public function render($value = null, $default = false)
48 {
49 return static::renderStatic(
50 array(
51 'value' => $value,
52 'default' => $default
53 ),
54 $this->buildRenderChildrenClosure(),
55 $this->renderingContext
56 );
57 }
58
59 /**
60 * @param array $arguments
61 * @param callable $renderChildrenClosure
62 * @param RenderingContextInterface $renderingContext
63 *
64 * @return mixed|string
65 * @throws Exception
66 */
67 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
68 {
69 $value = $arguments['value'];
70 $default = $arguments['default'];
71 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
72 if ($default !== false) {
73 GeneralUtility::deprecationLog('Argument "default" on f:case is deprecated - use f:defaultCase instead');
74 }
75 if ($value === null && $default === false) {
76 throw new Exception('The case View helper must have either value or default argument', 1382867521);
77 }
78 $expression = $viewHelperVariableContainer->get(OriginalSwitchViewHelper::class, 'switchExpression');
79
80 // non-type-safe comparison by intention
81 if ($default === true || $expression == $value) {
82 $viewHelperVariableContainer->addOrUpdate(OriginalSwitchViewHelper::class, 'break', true);
83 return $renderChildrenClosure();
84 }
85
86 return '';
87 }
88 }