1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2016 |
4
|
|
|
* |
5
|
|
|
* @category Library |
6
|
|
|
* @package Dwoo\Plugins\Blocks |
7
|
|
|
* @author Jordi Boggiano <[email protected]> |
8
|
|
|
* @author David Sanchez <[email protected]> |
9
|
|
|
* @copyright 2008-2013 Jordi Boggiano |
10
|
|
|
* @copyright 2013-2016 David Sanchez |
11
|
|
|
* @license http://dwoo.org/LICENSE Modified BSD License |
12
|
|
|
* @version 1.3.0 |
13
|
|
|
* @date 2016-09-19 |
14
|
|
|
* @link http://dwoo.org/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Blocks; |
18
|
|
|
|
19
|
|
|
use Dwoo\Compiler; |
20
|
|
|
use Dwoo\IElseable; |
21
|
|
|
use Dwoo\Block\Plugin as BlockPlugin; |
22
|
|
|
use Dwoo\ICompilable\Block as ICompilableBlock; |
23
|
|
|
use Dwoo\Compilation\Exception as CompilationException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Conditional block, the syntax is very similar to the php one, allowing () || && and |
27
|
|
|
* other php operators. Additional operators and their equivalent php syntax are as follow :. |
28
|
|
|
* eq -> == |
29
|
|
|
* neq or ne -> != |
30
|
|
|
* gte or ge -> >= |
31
|
|
|
* lte or le -> <= |
32
|
|
|
* gt -> > |
33
|
|
|
* lt -> < |
34
|
|
|
* mod -> % |
35
|
|
|
* not -> ! |
36
|
|
|
* X is [not] div by Y -> (X % Y) == 0 |
37
|
|
|
* X is [not] even [by Y] -> (X % 2) == 0 or ((X/Y) % 2) == 0 |
38
|
|
|
* X is [not] odd [by Y] -> (X % 2) != 0 or ((X/Y) % 2) != 0 |
39
|
|
|
* This software is provided 'as-is', without any express or implied warranty. |
40
|
|
|
* In no event will the authors be held liable for any damages arising from the use of this software. |
41
|
|
|
*/ |
42
|
|
|
class PluginIf extends BlockPlugin implements ICompilableBlock, IElseable |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @param array $rest |
46
|
|
|
*/ |
47
|
|
|
public function init(array $rest) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $params |
53
|
|
|
* @param array $tokens |
54
|
|
|
* @param Compiler $compiler |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
* @throws CompilationException |
58
|
|
|
*/ |
59
|
|
|
public static function replaceKeywords(array $params, array $tokens, Compiler $compiler) |
60
|
|
|
{ |
61
|
|
|
$p = array(); |
62
|
|
|
|
63
|
|
|
foreach($params as $k => $v) { |
64
|
|
|
$v = (string)$v; |
65
|
|
|
if (substr($v, 0, 1) === '"' || substr($v, 0, 1) === '\'') { |
66
|
|
|
$vmod = strtolower(substr($v, 1, - 1)); |
67
|
|
|
} else { |
68
|
|
|
$vmod = strtolower($v); |
69
|
|
|
} |
70
|
|
|
switch ($vmod) { |
71
|
|
|
|
72
|
|
View Code Duplication |
case 'and': |
|
|
|
|
73
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
74
|
|
|
$p[] = '&&'; |
75
|
|
|
} else { |
76
|
|
|
$p[] = $v; |
77
|
|
|
} |
78
|
|
|
break; |
79
|
|
View Code Duplication |
case 'or': |
|
|
|
|
80
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
81
|
|
|
$p[] = '||'; |
82
|
|
|
} else { |
83
|
|
|
$p[] = $v; |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
View Code Duplication |
case 'xor': |
|
|
|
|
87
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
88
|
|
|
$p[] = '^'; |
89
|
|
|
} else { |
90
|
|
|
$p[] = $v; |
91
|
|
|
} |
92
|
|
|
break; |
93
|
|
View Code Duplication |
case 'eq': |
|
|
|
|
94
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
95
|
|
|
$p[] = '=='; |
96
|
|
|
} else { |
97
|
|
|
$p[] = $v; |
98
|
|
|
} |
99
|
|
|
break; |
100
|
|
|
case 'ne': |
101
|
|
View Code Duplication |
case 'neq': |
|
|
|
|
102
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
103
|
|
|
$p[] = '!='; |
104
|
|
|
} else { |
105
|
|
|
$p[] = $v; |
106
|
|
|
} |
107
|
|
|
break; |
108
|
|
|
case 'gte': |
109
|
|
View Code Duplication |
case 'ge': |
|
|
|
|
110
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
111
|
|
|
$p[] = '>='; |
112
|
|
|
} else { |
113
|
|
|
$p[] = $v; |
114
|
|
|
} |
115
|
|
|
break; |
116
|
|
|
case 'lte': |
117
|
|
View Code Duplication |
case 'le': |
|
|
|
|
118
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
119
|
|
|
$p[] = '<='; |
120
|
|
|
} else { |
121
|
|
|
$p[] = $v; |
122
|
|
|
} |
123
|
|
|
break; |
124
|
|
View Code Duplication |
case 'gt': |
|
|
|
|
125
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
126
|
|
|
$p[] = '>'; |
127
|
|
|
} else { |
128
|
|
|
$p[] = $v; |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
View Code Duplication |
case 'lt': |
|
|
|
|
132
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
133
|
|
|
$p[] = '<'; |
134
|
|
|
} else { |
135
|
|
|
$p[] = $v; |
136
|
|
|
} |
137
|
|
|
break; |
138
|
|
View Code Duplication |
case 'mod': |
|
|
|
|
139
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
140
|
|
|
$p[] = '%'; |
141
|
|
|
} else { |
142
|
|
|
$p[] = $v; |
143
|
|
|
} |
144
|
|
|
break; |
145
|
|
View Code Duplication |
case 'not': |
|
|
|
|
146
|
|
|
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
147
|
|
|
$p[] = '!'; |
148
|
|
|
} else { |
149
|
|
|
$p[] = $v; |
150
|
|
|
} |
151
|
|
|
break; |
152
|
|
|
case '<>': |
153
|
|
|
$p[] = '!='; |
154
|
|
|
break; |
155
|
|
|
case '==': |
156
|
|
|
case '!=': |
157
|
|
|
case '>=': |
158
|
|
|
case '<=': |
159
|
|
|
case '>': |
160
|
|
|
case '<': |
161
|
|
|
case '===': |
162
|
|
|
case '!==': |
163
|
|
|
case '%': |
164
|
|
|
case '!': |
165
|
|
|
case '^': |
166
|
|
|
$p[] = $vmod; |
167
|
|
|
break; |
168
|
|
|
case 'is': |
169
|
|
|
if ($tokens[$k] !== Compiler::T_UNQUOTED_STRING) { |
170
|
|
|
$p[] = $v; |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
if (isset($params[$k + 1]) && strtolower(trim($params[$k + 1], '"\'')) === 'not' && $tokens[$k + 1] === Compiler::T_UNQUOTED_STRING) { |
174
|
|
|
$negate = true; |
175
|
|
|
next($params); |
176
|
|
|
} else { |
177
|
|
|
$negate = false; |
178
|
|
|
} |
179
|
|
|
$ptr = 1 + (int)$negate; |
180
|
|
|
if ($tokens[$k + $ptr] !== Compiler::T_UNQUOTED_STRING) { |
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
if (!isset($params[$k + $ptr])) { |
184
|
|
|
$params[$k + $ptr] = ''; |
185
|
|
|
} else { |
186
|
|
|
$params[$k + $ptr] = trim($params[$k + $ptr], '"\''); |
187
|
|
|
} |
188
|
|
|
switch ($params[$k + $ptr]) { |
189
|
|
|
|
190
|
|
|
case 'div': |
191
|
|
|
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
192
|
|
|
$p[] = ' % ' . $params[$k + $ptr + 2] . ' ' . ($negate ? '!' : '=') . '== 0'; |
193
|
|
|
next($params); |
194
|
|
|
next($params); |
195
|
|
|
next($params); |
196
|
|
|
} else { |
197
|
|
|
throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] div by $b", found ' . $params[$k - 1] . ' is ' . ($negate ? 'not ' : '') . 'div ' . $params[$k + $ptr + 1] . ' ' . $params[$k + $ptr + 2]); |
198
|
|
|
} |
199
|
|
|
break; |
200
|
|
View Code Duplication |
case 'even': |
|
|
|
|
201
|
|
|
$a = array_pop($p); |
202
|
|
|
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
203
|
|
|
$b = $params[$k + $ptr + 2]; |
204
|
|
|
$p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '!' : '=') . '== 0'; |
205
|
|
|
next($params); |
206
|
|
|
next($params); |
207
|
|
|
} else { |
208
|
|
|
$p[] = $a . ' % 2 ' . ($negate ? '!' : '=') . '== 0'; |
209
|
|
|
} |
210
|
|
|
next($params); |
211
|
|
|
break; |
212
|
|
View Code Duplication |
case 'odd': |
|
|
|
|
213
|
|
|
$a = array_pop($p); |
214
|
|
|
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
215
|
|
|
$b = $params[$k + $ptr + 2]; |
216
|
|
|
$p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '=' : '!') . '== 0'; |
217
|
|
|
next($params); |
218
|
|
|
next($params); |
219
|
|
|
} else { |
220
|
|
|
$p[] = $a . ' % 2 ' . ($negate ? '=' : '!') . '== 0'; |
221
|
|
|
} |
222
|
|
|
next($params); |
223
|
|
|
break; |
224
|
|
|
default: |
225
|
|
|
throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found ' . $params[$k - 1] . ' is ' . $params[$k + $ptr + 1]); |
226
|
|
|
} |
227
|
|
|
break; |
228
|
|
|
default: |
229
|
|
|
$p[] = $v; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $p; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param Compiler $compiler |
238
|
|
|
* @param array $params |
239
|
|
|
* @param string $prepend |
240
|
|
|
* @param string $append |
241
|
|
|
* @param string $type |
242
|
|
|
* |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type) |
246
|
|
|
{ |
247
|
|
|
return ''; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param Compiler $compiler |
252
|
|
|
* @param array $params |
253
|
|
|
* @param string $prepend |
254
|
|
|
* @param string $append |
255
|
|
|
* @param string $content |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) |
260
|
|
|
{ |
261
|
|
|
$tokens = $compiler->getParamTokens($params); |
262
|
|
|
$params = $compiler->getCompiledParams($params); |
263
|
|
|
$pre = Compiler::PHP_OPEN . 'if (' . implode(' ', self::replaceKeywords($params['*'], $tokens['*'], $compiler)) . ") {\n" . Compiler::PHP_CLOSE; |
264
|
|
|
|
265
|
|
|
$post = Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE; |
266
|
|
|
|
267
|
|
|
if (isset($params['hasElse'])) { |
268
|
|
|
$post .= $params['hasElse']; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $pre . $content . $post; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.