|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Template |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Template is a template engine that has taken a lot of inspiration from Django. |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine Template |
|
11
|
|
|
* Copyright (c) 2014 Guz Alexander, http://guzalexander.com |
|
12
|
|
|
* Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com |
|
13
|
|
|
* Copyright (c) 2006 Mateo Murphy |
|
14
|
|
|
* |
|
15
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
16
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
17
|
|
|
* in the Software without restriction, including without limitation the rights |
|
18
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
19
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
20
|
|
|
* furnished to do so, subject to the following conditions: |
|
21
|
|
|
* |
|
22
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
23
|
|
|
* copies or substantial portions of the Software. |
|
24
|
|
|
* |
|
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
31
|
|
|
* SOFTWARE. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @file IfTag.php |
|
36
|
|
|
* |
|
37
|
|
|
* The "if" Template tag class |
|
38
|
|
|
* |
|
39
|
|
|
* @package Platine\Template\Tag |
|
40
|
|
|
* @author Platine Developers Team |
|
41
|
|
|
* @copyright Copyright (c) 2020 |
|
42
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
43
|
|
|
* @link http://www.iacademy.cf |
|
44
|
|
|
* @version 1.0.0 |
|
45
|
|
|
* @filesource |
|
46
|
|
|
*/ |
|
47
|
|
|
|
|
48
|
|
|
declare(strict_types=1); |
|
49
|
|
|
|
|
50
|
|
|
namespace Platine\Template\Tag; |
|
51
|
|
|
|
|
52
|
|
|
use Platine\Template\Exception\ParseException; |
|
53
|
|
|
use Platine\Template\Parser\AbstractCondition; |
|
54
|
|
|
use Platine\Template\Parser\Context; |
|
55
|
|
|
use Platine\Template\Parser\Lexer; |
|
56
|
|
|
use Platine\Template\Parser\Parser; |
|
57
|
|
|
use Platine\Template\Parser\Token; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Class IfTag |
|
61
|
|
|
* @package Platine\Template\Tag |
|
62
|
|
|
*/ |
|
63
|
|
|
class IfTag extends AbstractCondition |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* holding the nodes to render for each logical block |
|
68
|
|
|
* @var array<int, mixed> |
|
69
|
|
|
*/ |
|
70
|
|
|
protected array $nodeListHolders = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* holding the block type, block markup (conditions) |
|
74
|
|
|
* and block node list |
|
75
|
|
|
* @var array<int, mixed> |
|
76
|
|
|
*/ |
|
77
|
|
|
protected array $blocks = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(string $markup, &$tokens, Parser $parser) |
|
83
|
|
|
{ |
|
84
|
|
|
//Initialize |
|
85
|
|
|
$this->nodeListHolders[count($this->blocks)] = []; |
|
86
|
|
|
$this->nodeList = & $this->nodeListHolders[count($this->blocks)]; |
|
87
|
|
|
array_push($this->blocks, ['if', $markup, & $this->nodeList]); |
|
88
|
|
|
|
|
89
|
|
|
parent::__construct($markup, $tokens, $parser); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function render(Context $context): string |
|
96
|
|
|
{ |
|
97
|
|
|
$context->push(); |
|
98
|
|
|
$lexerLogical = new Lexer('/\s+(and|or)\s+/'); |
|
99
|
|
|
$lexerConditional = new Lexer( |
|
100
|
|
|
'/(' |
|
101
|
|
|
. Token::QUOTED_FRAGMENT |
|
102
|
|
|
. ')\s*([=!<>a-z_]+)?\s*(' |
|
103
|
|
|
. Token::QUOTED_FRAGMENT |
|
104
|
|
|
. ')?/' |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
$result = ''; |
|
108
|
|
|
foreach ($this->blocks as $block) { |
|
109
|
|
|
if ($block[0] === 'else') { |
|
110
|
|
|
$result = $this->renderAll($block[2], $context); |
|
111
|
|
|
|
|
112
|
|
|
break; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($block[0] === 'if' || $block[0] === 'elseif') { |
|
116
|
|
|
// Extract logical operators |
|
117
|
|
|
$lexerLogical->matchAll($block[1]); |
|
118
|
|
|
$operators = $lexerLogical->getArrayMatch(1); |
|
119
|
|
|
// Extract individual conditions |
|
120
|
|
|
$individualConditions = $lexerLogical->split($block[1]); |
|
121
|
|
|
|
|
122
|
|
|
$conditions = []; |
|
123
|
|
|
foreach ($individualConditions as $condition) { |
|
124
|
|
|
if ($lexerConditional->match($condition)) { |
|
125
|
|
|
$left = $lexerConditional->isMatchNotNull(1) |
|
126
|
|
|
? $lexerConditional->getStringMatch(1) |
|
127
|
|
|
: null; |
|
128
|
|
|
$operator = $lexerConditional->isMatchNotNull(2) |
|
129
|
|
|
? $lexerConditional->getStringMatch(2) |
|
130
|
|
|
: null; |
|
131
|
|
|
$right = $lexerConditional->isMatchNotNull(3) |
|
132
|
|
|
? $lexerConditional->getStringMatch(3) |
|
133
|
|
|
: null; |
|
134
|
|
|
|
|
135
|
|
|
array_push($conditions, [ |
|
136
|
|
|
'left' => $left, |
|
137
|
|
|
'operator' => $operator, |
|
138
|
|
|
'right' => $right, |
|
139
|
|
|
]); |
|
140
|
|
|
} else { |
|
141
|
|
|
throw new ParseException(sprintf( |
|
142
|
|
|
'Syntax Error in "%s" - Valid syntax: if [conditions]', |
|
143
|
|
|
'if' |
|
144
|
|
|
)); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (count($operators) > 0) { |
|
149
|
|
|
// If statement contains and/or |
|
150
|
|
|
$display = $this->evaluateCondition( |
|
151
|
|
|
$conditions[0]['left'], |
|
152
|
|
|
$conditions[0]['right'], |
|
153
|
|
|
$conditions[0]['operator'], |
|
154
|
|
|
$context |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
foreach ($operators as $key => $operator) { |
|
158
|
|
|
if ($operator === 'and') { |
|
159
|
|
|
$display = ( |
|
160
|
|
|
$display |
|
161
|
|
|
&& $this->evaluateCondition( |
|
162
|
|
|
$conditions[$key + 1]['left'], |
|
163
|
|
|
$conditions[$key + 1]['right'], |
|
164
|
|
|
$conditions[$key + 1]['operator'], |
|
165
|
|
|
$context |
|
166
|
|
|
) |
|
167
|
|
|
); |
|
168
|
|
|
} else { |
|
169
|
|
|
$display = ( |
|
170
|
|
|
$display |
|
171
|
|
|
|| $this->evaluateCondition( |
|
172
|
|
|
$conditions[$key + 1]['left'], |
|
173
|
|
|
$conditions[$key + 1]['right'], |
|
174
|
|
|
$conditions[$key + 1]['operator'], |
|
175
|
|
|
$context |
|
176
|
|
|
) |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} else { |
|
181
|
|
|
// If statement is a single condition |
|
182
|
|
|
$display = $this->evaluateCondition( |
|
183
|
|
|
$conditions[0]['left'], |
|
184
|
|
|
$conditions[0]['right'], |
|
185
|
|
|
$conditions[0]['operator'], |
|
186
|
|
|
$context |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// hook for if not tag |
|
191
|
|
|
$display = $this->negateCondition($display); |
|
192
|
|
|
|
|
193
|
|
|
if ($display) { |
|
194
|
|
|
$result = $this->renderAll($block[2], $context); |
|
195
|
|
|
|
|
196
|
|
|
break; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$context->pop(); |
|
202
|
|
|
|
|
203
|
|
|
return $result; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Handler negate condition |
|
208
|
|
|
* @param mixed $value |
|
209
|
|
|
* @return mixed |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function negateCondition($value) |
|
212
|
|
|
{ |
|
213
|
|
|
// no need to negate a condition in a regular `if` |
|
214
|
|
|
// tag (will do that in `ifnot` tag) |
|
215
|
|
|
|
|
216
|
|
|
return $value; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* {@inheritdoc} |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function unknownTag(string $tag, string $param, array $tokens): void |
|
223
|
|
|
{ |
|
224
|
|
|
if ($tag === 'else' || $tag === 'elseif') { |
|
225
|
|
|
// Update reference to node list holder for this block |
|
226
|
|
|
$this->nodeListHolders[count($this->blocks) + 1] = []; |
|
227
|
|
|
$this->nodeList = & $this->nodeListHolders[count($this->blocks) + 1]; |
|
228
|
|
|
array_push($this->blocks, [$tag, $param, & $this->nodeList]); |
|
229
|
|
|
} else { |
|
230
|
|
|
parent::unknownTag($tag, $param, $tokens); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|