-
Notifications
You must be signed in to change notification settings - Fork 91
/
BooleanNode.php
183 lines (166 loc) · 6.35 KB
/
BooleanNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\BooleanParser;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* A node which is used inside boolean arguments
*
* @internal
* @todo Make class final.
*/
class BooleanNode extends AbstractNode
{
/**
* Stack of expression nodes to be evaluated
*
* @var NodeInterface[]
*/
protected $childNodes = [];
/**
* @var array
*/
protected array $stack = [];
/**
* @param mixed $input NodeInterface, array (of nodes or expression parts) or a simple type that can be evaluated to boolean
*/
public function __construct($input)
{
// First, evaluate everything that is not an ObjectAccessorNode, ArrayNode
// or ViewHelperNode so we get all text, numbers, comparators and
// groupers from the text parts of the expression. All other nodes
// we leave intact for later processing
if ($input instanceof RootNode) {
$this->stack = $input->getChildNodes();
} elseif (is_array($input)) {
$this->stack = $input;
} else {
$this->stack = [is_string($input) ? trim($input) : $input];
}
}
public function getStack(): array
{
return $this->stack;
}
public function evaluate(RenderingContextInterface $renderingContext): bool
{
return self::evaluateStack($renderingContext, $this->stack);
}
/**
* @param NodeInterface $node
* @param RenderingContextInterface $renderingContext
* @return bool
*/
public static function createFromNodeAndEvaluate(NodeInterface $node, RenderingContextInterface $renderingContext)
{
$booleanNode = new BooleanNode($node);
return $booleanNode->evaluate($renderingContext);
}
/**
* Takes a stack of nodes evaluates it with the end result
* being a single boolean value. Creates new BooleanNodes
* recursively to process braced expressions as single units.
*
* @param RenderingContextInterface $renderingContext
* @param array $expressionParts
* @return bool the boolean value
*/
public static function evaluateStack(RenderingContextInterface $renderingContext, array $expressionParts): bool
{
$expression = static::reconcatenateExpression($expressionParts);
$context = static::gatherContext($renderingContext, $expressionParts);
$parser = new BooleanParser();
return static::convertToBoolean($parser->evaluate($expression, $context), $renderingContext);
}
/**
* Walk all expressionParts and concatenate an expression string
*/
public static function reconcatenateExpression(array $expressionParts): string
{
$merged = [];
foreach ($expressionParts as $key => $expressionPart) {
if ($expressionPart instanceof TextNode || is_string($expressionPart)) {
$merged[] = $expressionPart instanceof TextNode ? $expressionPart->getText() : $expressionPart;
} elseif ($expressionPart instanceof NodeInterface) {
$merged[] = '{node' . $key . '}';
} else {
$merged[] = '{node' . $key . '}';
}
}
return implode('', $merged);
}
/**
* Walk all expressionParts and gather a context array of all non textNode parts
*/
public static function gatherContext(RenderingContextInterface $renderingContext, array $expressionParts): array
{
$context = [];
foreach ($expressionParts as $key => $expressionPart) {
if ($expressionPart instanceof NodeInterface) {
$context['node' . $key] = $expressionPart->evaluate($renderingContext);
} else {
$context['node' . $key] = $expressionPart;
}
}
return $context;
}
/**
* Convert argument strings to their equivalents. Needed to handle strings with a boolean meaning.
*
* Must be public and static as it is used from inside cached templates.
*/
public static function convertToBoolean(mixed $value, RenderingContextInterface $renderingContext): bool
{
if (is_bool($value)) {
return $value;
}
if (is_numeric($value)) {
return (bool)((float)$value);
}
if (is_string($value)) {
if (strlen($value) === 0) {
return false;
}
$value = $renderingContext->getTemplateParser()->unquoteString($value);
return strtolower($value) !== 'false' && !empty($value);
}
if (is_array($value) || $value instanceof \Countable) {
return count($value) > 0;
}
if (is_object($value)) {
return true;
}
return false;
}
public function convert(TemplateCompiler $templateCompiler): array
{
$stack = (new ArrayNode($this->getStack()))->convert($templateCompiler);
$initializationPhpCode = $stack['initialization'] . chr(10);
$parser = new BooleanParser();
$compiledExpression = $parser->compile(BooleanNode::reconcatenateExpression($this->getStack()));
$functionName = $templateCompiler->variableName('expression');
$initializationPhpCode .= $functionName . ' = function($context) {return ' . $compiledExpression . ';};' . chr(10);
// @todo: There are possible paths to short-circuit this code in combination
// with EscapingNode, to hint EscapingNode no further sanitation
// is needed. See PR #440 for options on this. The PR however introduces
// multiple different values for 'execution', which needs to be decided
// if we really want this.
return [
'initialization' => $initializationPhpCode,
'execution' => sprintf(
'%s::convertToBoolean(' . chr(10) .
' %s(%s::gatherContext($renderingContext, %s)),' . chr(10) .
' $renderingContext' . chr(10) .
')',
BooleanNode::class,
$functionName,
BooleanNode::class,
$stack['execution'],
),
];
}
}