[TASK] Remove closing PHP tags
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / SwitchViewHelper.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 * Switch view helper which can be used to render content depending on a value or expression.
16 * Implements what a basic switch()-PHP-method does.
17 *
18 * = Examples =
19 *
20 * <code title="Simple Switch statement">
21 * <f:switch expression="{person.gender}">
22 * <f:case value="male">Mr.</f:case>
23 * <f:case value="female">Mrs.</f:case>
24 * </f:switch>
25 * </code>
26 * <output>
27 * Mr. / Mrs. (depending on the value of {person.gender})
28 * </output>
29 *
30 * Note: Using this view helper can be a sign of weak architecture. If you end up using it extensively
31 * you might want to consider restructuring your controllers/actions and/or use partials and sections.
32 * E.g. the above example could be achieved with <f:render partial="title.{person.gender}" /> and the partials
33 * "title.male.html", "title.female.html", ...
34 * Depending on the scenario this can be easier to extend and possibly contains less duplication.
35 *
36 * @api
37 */
38 class SwitchViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper implements \TYPO3\CMS\Fluid\Core\ViewHelper\Facets\ChildNodeAccessInterface {
39
40 /**
41 * An array of \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode
42 * @var array
43 */
44 private $childNodes = array();
45
46 /**
47 * @var mixed
48 */
49 protected $backupSwitchExpression = NULL;
50
51 /**
52 * @var boolean
53 */
54 protected $backupBreakState = FALSE;
55
56 /**
57 * Setter for ChildNodes - as defined in ChildNodeAccessInterface
58 *
59 * @param array $childNodes Child nodes of this syntax tree node
60 * @return void
61 */
62 public function setChildNodes(array $childNodes) {
63 $this->childNodes = $childNodes;
64 }
65
66 /**
67 * @param mixed $expression
68 * @return string the rendered string
69 * @api
70 */
71 public function render($expression) {
72 $content = '';
73 $this->backupSwitchState();
74 $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
75
76 $templateVariableContainer->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression', $expression);
77 $templateVariableContainer->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', FALSE);
78
79 foreach ($this->childNodes as $childNode) {
80 if (
81 !$childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
82 || $childNode->getViewHelperClassName() !== 'TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper'
83 ) {
84 continue;
85 }
86 $content = $childNode->evaluate($this->renderingContext);
87 if ($templateVariableContainer->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break') === TRUE) {
88 break;
89 }
90 }
91
92 $templateVariableContainer->remove('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression');
93 $templateVariableContainer->remove('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break');
94
95 $this->restoreSwitchState();
96 return $content;
97 }
98
99 /**
100 * Backups "switch expression" and "break" state of a possible parent switch ViewHelper to support nesting
101 *
102 * @return void
103 */
104 protected function backupSwitchState() {
105 if ($this->renderingContext->getViewHelperVariableContainer()->exists('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')) {
106 $this->backupSwitchExpression = $this->renderingContext->getViewHelperVariableContainer()->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression');
107 }
108 if ($this->renderingContext->getViewHelperVariableContainer()->exists('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break')) {
109 $this->backupBreakState = $this->renderingContext->getViewHelperVariableContainer()->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break');
110 }
111 }
112
113 /**
114 * Restores "switch expression" and "break" states that might have been backed up in backupSwitchState() before
115 *
116 * @return void
117 */
118 protected function restoreSwitchState() {
119 if ($this->backupSwitchExpression !== NULL) {
120 $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(
121 'TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper',
122 'switchExpression',
123 $this->backupSwitchExpression
124 );
125 }
126 if ($this->backupBreakState !== FALSE) {
127 $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
128 }
129 }
130 }