[BUGFIX] Serialize expression in switch viewhelper
[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 /**
15 * Case view helper that is only usable within the SwitchViewHelper.
16 * @see \TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper
17 *
18 * @api
19 */
20 class CaseViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
21
22 /**
23 * @param mixed $value The switch value. If it matches, the child will be rendered
24 * @param bool $default If this is set, this child will be rendered, if none else matches
25 *
26 * @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
27 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
28 *
29 * @api
30 */
31 public function render($value = NULL, $default = FALSE) {
32 $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
33 if (!$viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper::class, 'switchExpression')) {
34 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('The case View helper can only be used within a switch View helper', 1368112037);
35 }
36 if (is_null($value) && $default === FALSE) {
37 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('The case View helper must have either value or default argument', 1382867521);
38 }
39 $switchExpression = unserialize($viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper::class, 'switchExpression'));
40
41 // non-type-safe comparison by intention
42 if ($default === TRUE || $switchExpression == $value) {
43 $viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper::class, 'break', TRUE);
44 return $this->renderChildren();
45 }
46 return '';
47 }
48 }