|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace TYPO3Fluid\Fluid\Core\ViewHelper; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
|
7
|
|
|
* See LICENSE.txt that was shipped with this package. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use TYPO3Fluid\Fluid\Component\ComponentInterface; |
|
11
|
|
|
use TYPO3Fluid\Fluid\ViewHelpers\ElseViewHelper; |
|
12
|
|
|
use TYPO3Fluid\Fluid\ViewHelpers\ThenViewHelper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This view helper is an abstract ViewHelper which implements an if/else condition. |
|
16
|
|
|
* |
|
17
|
|
|
* = Usage = |
|
18
|
|
|
* |
|
19
|
|
|
* To create a custom Condition ViewHelper, you need to subclass this class, and |
|
20
|
|
|
* implement your own render() method. Inside there, you should call $this->renderThenChild() |
|
21
|
|
|
* if the condition evaluated to TRUE, and $this->renderElseChild() if the condition evaluated |
|
22
|
|
|
* to FALSE. |
|
23
|
|
|
* |
|
24
|
|
|
* Every Condition ViewHelper has a "then" and "else" argument, so it can be used like: |
|
25
|
|
|
* <[aConditionViewHelperName] .... then="condition true" else="condition false" />, |
|
26
|
|
|
* or as well use the "then" and "else" child nodes. |
|
27
|
|
|
* |
|
28
|
|
|
* @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper for a more detailed explanation and a simple usage example. |
|
29
|
|
|
* Make sure to NOT OVERRIDE the constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractConditionViewHelper extends AbstractViewHelper |
|
32
|
|
|
{ |
|
33
|
|
|
protected $escapeOutput = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function initializeArguments() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.'); |
|
41
|
|
|
$this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Renders <f:then> child if $condition is true, otherwise renders <f:else> child. |
|
46
|
|
|
* Method which only gets called if the template is not compiled. For static calling, |
|
47
|
|
|
* the then/else nodes are converted to closures and condition evaluation closures. |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function render() |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->condition()) { |
|
54
|
|
|
return $this->renderThenChild(); |
|
55
|
|
|
} |
|
56
|
|
|
return $this->renderElseChild(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Override this method for a custom decision whether or not to render the ViewHelper |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function condition(): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns value of "then" attribute. |
|
71
|
|
|
* If then attribute is not set, iterates through child nodes and renders ThenViewHelper. |
|
72
|
|
|
* If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function renderThenChild() |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->hasArgument('then')) { |
|
79
|
|
|
return $this->arguments['then']; |
|
80
|
|
|
} |
|
81
|
|
|
$elseViewHelperEncountered = false; |
|
82
|
|
|
foreach ($this->getChildren() as $childNode) { |
|
83
|
|
|
if ($childNode instanceof ThenViewHelper) { |
|
84
|
|
|
$data = $childNode->evaluate($this->renderingContext); |
|
85
|
|
|
return $data; |
|
86
|
|
|
} |
|
87
|
|
|
if ($childNode instanceof ElseViewHelper) { |
|
88
|
|
|
$elseViewHelperEncountered = true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($elseViewHelperEncountered) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->evaluateChildren($this->renderingContext); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns value of "else" attribute. |
|
101
|
|
|
* If else attribute is not set, iterates through child nodes and renders ElseViewHelper. |
|
102
|
|
|
* If else attribute is not set and no ElseViewHelper is found, an empty string will be returned. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string rendered ElseViewHelper or an empty string if no ThenViewHelper was found |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function renderElseChild() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->hasArgument('else')) { |
|
109
|
|
|
return $this->arguments['else']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** @var ComponentInterface|null $elseNode */ |
|
113
|
|
|
$elseNode = null; |
|
|
|
|
|
|
114
|
|
|
foreach ($this->getChildren() as $childNode) { |
|
115
|
|
|
if ($childNode instanceof ElseViewHelper) { |
|
116
|
|
|
$conditionArgument = $childNode->getArguments()->setRenderingContext($this->renderingContext)['if']; |
|
117
|
|
|
if ($conditionArgument) { |
|
118
|
|
|
return $childNode->evaluate($this->renderingContext); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.