forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTernaryExpressionNode.php
More file actions
160 lines (144 loc) · 5.88 KB
/
TernaryExpressionNode.php
File metadata and controls
160 lines (144 loc) · 5.88 KB
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
<?php
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\BooleanParser;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\BooleanNode;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* Ternary Condition Node - allows the shorthand version
* of a condition to be written as `{var ? thenvar : elsevar}`
*/
class TernaryExpressionNode extends AbstractExpressionNode
{
/**
* Pattern which detects ternary conditions written in shorthand
* syntax, e.g. {checkvar ? thenvar : elsevar}.
*/
public static $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 $variableDetection = '/[^\'_a-zA-Z0-9\.\\\\]{0,1}([_a-zA-Z0-9\.\\\\]*)[^\']{0,1}/';
/**
* @param RenderingContextInterface $renderingContext
* @param string $expression
* @param array $matches
* @return mixed
*/
public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches)
{
$parts = preg_split('/([\?:])/s', $expression);
$parts = array_map([__CLASS__, 'trimPart'], $parts);
$negated = false;
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);
} else {
return static::getTemplateVariableOrValueItself($renderingContext->getTemplateParser()->unquoteString($else), $renderingContext);
}
}
/**
* @param mixed $candidate
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function getTemplateVariableOrValueItself($candidate, RenderingContextInterface $renderingContext)
{
$suspect = parent::getTemplateVariableOrValueItself($candidate, $renderingContext);
if ($suspect === $candidate) {
return $renderingContext->getTemplateParser()->unquoteString($suspect);
}
return $suspect;
}
/**
* Gather all context variables used in the expression
*
* @param RenderingContextInterface $renderingContext
* @param string $expression
* @return array
*/
public static function gatherContext($renderingContext, $expression)
{
$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.
*
* @param TemplateCompiler $templateCompiler
* @return string
*/
public function compile(TemplateCompiler $templateCompiler)
{
$parts = preg_split('/([\?:])/s', $this->getExpression());
$parts = array_map([__CLASS__, 'trimPart'], $parts);
list ($check, $then, $else) = $parts;
$matchesVariable = $templateCompiler->variableName('array');
$initializationPhpCode = '// Rendering TernaryExpression node' . chr(10);
$initializationPhpCode .= sprintf('%s = %s;', $matchesVariable, var_export($this->getMatches(), true)) . chr(10);
$parser = new BooleanParser();
$compiledExpression = $parser->compile($check);
$functionName = $templateCompiler->variableName('ternaryExpression');
$initializationPhpCode .= sprintf(
'%s = function($context, $renderingContext) {
if (%s::convertToBoolean(' . $compiledExpression . ', $renderingContext) === TRUE) {
return %s::getTemplateVariableOrValueItself(%s, $renderingContext);
} else {
return %s::getTemplateVariableOrValueItself(%s, $renderingContext);
}
};' . chr(10),
$functionName,
BooleanNode::class,
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
)
];
}
}