TYPO3 CMS  TYPO3_8-7
CaseViewHelper.php
Go to the documentation of this file.
1 <?php
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 
18 use TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper as OriginalSwitchViewHelper;
19 
27 {
31  protected $escapeOutput = false;
32 
36  public function initializeArguments()
37  {
38  $this->registerArgument('value', 'mixed', 'The switch value. If it matches, the child will be rendered.', false, null);
39  $this->registerArgument('default', 'bool', 'If this is set, this child will be rendered, if none else matches.', false, false);
40  }
41 
42  /***
43  * @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
44  * @throws Exception
45  *
46  * @api
47  */
48  public function render()
49  {
50  return static::renderStatic(
51  [
52  'value' => $this->arguments['value'],
53  'default' => $this->arguments['default']
54  ],
55  $this->buildRenderChildrenClosure(),
56  $this->renderingContext
57  );
58  }
59 
68  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
69  {
70  $value = $arguments['value'];
71  $default = (bool)$arguments['default'];
72  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
73  if ($default !== false) {
75  'default',
76  $renderingContext,
77  'Argument "default" on f:case is deprecated - use f:defaultCase instead'
78  );
79  }
80  if ($value === null && $default === false) {
81  throw new Exception('The case View helper must have either value or default argument', 1382867521);
82  }
83  $expression = $viewHelperVariableContainer->get(OriginalSwitchViewHelper::class, 'switchExpression');
84 
85  // non-type-safe comparison by intention
86  if ($default === true || $expression == $value) {
87  $viewHelperVariableContainer->addOrUpdate(OriginalSwitchViewHelper::class, 'break', true);
88  return $renderChildrenClosure();
89  }
90 
91  return '';
92  }
93 }
static logDeprecatedViewHelperAttribute(string $property, RenderingContextInterface $renderingContext, string $additionalMessage='')
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)