-
Notifications
You must be signed in to change notification settings - Fork 93
/
TernaryExpressionNode.php
150 lines (132 loc) · 5.77 KB
/
TernaryExpressionNode.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
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\BooleanParser;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* Ternary Condition Node - allows the shorthand version
* of a condition to be written as `{var ? thenvar : elsevar}`
*
* @internal
* @todo Make class final.
*/
class TernaryExpressionNode extends AbstractExpressionNode
{
/**
* Pattern which detects ternary conditions written in shorthand
* syntax, e.g. {checkvar ? thenvar : elsevar}.
*/
public static string $detectionExpression = '/
(
{ # Start of shorthand syntax
(?: # Math expression is composed of...
[\\!_a-zA-Z0-9.\(\)\!\|\&\\\'\'\"\=\<\>\%\s\{\}\:\,]+ # Check variable side
[\s]?\?[\s]?
[_a-zA-Z0-9.\s\'\"\\.]* # Then variable side, optional
[\s]?:[\s]?
[_a-zA-Z0-9.\s\'\"\\.]+ # Else variable side
)
} # End of shorthand syntax
)/x';
/**
* Filter out variable names form expression
*/
protected static string $variableDetection = '/[^\'_a-zA-Z0-9\.\\\\]{0,1}([_a-zA-Z0-9\.\\\\]*)[^\']{0,1}/';
public static function evaluateExpression(RenderingContextInterface $renderingContext, string $expression, array $matches): mixed
{
$parts = preg_split('/([\?:])/s', $expression);
$parts = array_map([__CLASS__, 'trimPart'], $parts);
list($check, $then, $else) = $parts;
if ($then === '') {
$then = $check[0] === '!' ? $else : $check;
}
$context = static::gatherContext($renderingContext, $expression);
$parser = new BooleanParser();
$checkResult = $parser->evaluate($check, $context);
if ($checkResult) {
return static::getTemplateVariableOrValueItself($renderingContext->getTemplateParser()->unquoteString($then), $renderingContext);
}
return static::getTemplateVariableOrValueItself($renderingContext->getTemplateParser()->unquoteString($else), $renderingContext);
}
public static function getTemplateVariableOrValueItself(mixed $candidate, RenderingContextInterface $renderingContext): mixed
{
$suspect = parent::getTemplateVariableOrValueItself($candidate, $renderingContext);
if ($suspect === $candidate) {
return $renderingContext->getTemplateParser()->unquoteString($suspect);
}
return $suspect;
}
/**
* Gather all context variables used in the expression
*/
public static function gatherContext(RenderingContextInterface $renderingContext, string $expression): array
{
$context = [];
if (preg_match_all(static::$variableDetection, $expression, $matches) > 0) {
foreach ($matches[1] as $variable) {
if (strtolower($variable) === 'true' || strtolower($variable) === 'false' || empty($variable) === true) {
continue;
}
$context[$variable] = static::getTemplateVariableOrValueItself($variable, $renderingContext);
}
}
return $context;
}
/**
* Compiles the ExpressionNode, returning an array with
* exactly two keys which contain strings:
*
* - "initialization" which contains variable initializations
* - "execution" which contains the execution (that uses the variables)
*
* The expression and matches can be read from the local
* instance - and the RenderingContext and other APIs
* can be accessed via the TemplateCompiler.
*
* @return array<string, string>
*/
public function compile(TemplateCompiler $templateCompiler): array
{
$parts = preg_split('/([\?:])/s', $this->getExpression());
$parts = array_map([__CLASS__, 'trimPart'], $parts);
list($check, $then, $else) = $parts;
if ($then === '') {
$then = $check[0] === '!' ? $else : $check;
}
$matchesVariable = $templateCompiler->variableName('array');
$initializationPhpCode = '// Rendering TernaryExpression node' . chr(10);
$initializationPhpCode .= sprintf('%s = %s;', $matchesVariable, var_export($this->getMatches(), true)) . chr(10);
$functionName = $templateCompiler->variableName('ternaryExpression');
$initializationPhpCode .= sprintf(
'%s = function($context, $renderingContext) {' . chr(10) .
' $check = %s;' . chr(10) .
' $parser = new \TYPO3Fluid\Fluid\Core\Parser\BooleanParser();' . chr(10) .
' $checkResult = $parser->evaluate($check, $context);' . chr(10) .
' if ($checkResult) {' . chr(10) .
' return %s::getTemplateVariableOrValueItself(%s, $renderingContext);' . chr(10) .
' }' . chr(10) .
' return %s::getTemplateVariableOrValueItself(%s, $renderingContext);' . chr(10) .
'};' . chr(10),
$functionName,
var_export($check, true),
static::class,
var_export($then, true),
static::class,
var_export($else, true),
);
return [
'initialization' => $initializationPhpCode,
'execution' => sprintf(
'%s(%s::gatherContext($renderingContext, %s[1]), $renderingContext)',
$functionName,
static::class,
$matchesVariable,
),
];
}
}