2
2
3
3
namespace Le \Dataplater ;
4
4
5
- use DOMDocument , DOMElement , DOMXPath ;
6
5
use InvalidArgumentException , ArgumentCountError , Exception ;
6
+ use DOMDocument , DOMElement , DOMXPath ;
7
7
8
8
class Dataplater
9
9
{
10
10
11
- private DOMDocument $ doc ;
12
- private DOMXPath $ xpath ;
11
+ protected DOMDocument $ doc ;
12
+ protected DOMXPath $ xpath ;
13
13
14
14
/**
15
15
* @throws Exception
@@ -62,6 +62,9 @@ public function render(?array $vars = null): string
62
62
63
63
private function defineBuiltInFunctions (): void
64
64
{
65
+ $ this ->vars ['logic ' ]['ternary ' ] = fn ($ a , $ b , $ c ) => $ a ? $ b : $ c ;
66
+ $ this ->vars ['logic ' ]['shortTernary ' ] = fn ($ a , $ b ) => $ a ?: $ b ;
67
+
65
68
$ this ->vars ['logic ' ]['and ' ] = fn ($ a , $ b ) => $ a && $ b ;
66
69
$ this ->vars ['logic ' ]['or ' ] = fn ($ a , $ b ) => $ a || $ b ;
67
70
$ this ->vars ['logic ' ]['not ' ] = fn ($ a ) => !$ a ;
@@ -189,7 +192,7 @@ private function execute($context = null): void
189
192
190
193
$ selector = "data-var-if " ;
191
194
foreach ($ this ->xpath ->query ("descendant-or-self::*[@ $ selector] " , $ context ) as $ elem ) {
192
- if ($ this ->eval ($ elem ->getAttribute ($ selector ), $ elem ) != true )
195
+ if (! $ this ->eval ($ elem ->getAttribute ($ selector ), $ elem ))
193
196
$ elem ->parentNode ->removeChild ($ elem );
194
197
else
195
198
$ elem ->removeAttribute ($ selector );
@@ -203,7 +206,7 @@ private function execute($context = null): void
203
206
if (empty ($ params [1 ])) throw new ParseException ('missing value expression if true ' , $ elem );
204
207
205
208
unset($ value );
206
- if ($ this ->eval ($ params [0 ], $ elem ) == true )
209
+ if ($ this ->eval ($ params [0 ], $ elem ))
207
210
$ value = $ params [1 ];
208
211
else if (isset ($ params [2 ]))
209
212
$ value = $ params [2 ];
@@ -228,7 +231,7 @@ private function execute($context = null): void
228
231
if (empty ($ params [0 ])) throw new ParseException ('missing condition expression ' , $ elem );
229
232
if (empty ($ params [1 ])) throw new ParseException ('missing value expression if true ' , $ elem );
230
233
231
- if ($ this ->eval ($ params [0 ], $ elem ) == true )
234
+ if ($ this ->eval ($ params [0 ], $ elem ))
232
235
$ elem ->nodeValue = $ this ->eval ($ params [1 ], $ elem );
233
236
else if (isset ($ params [2 ]))
234
237
$ elem ->nodeValue = $ this ->eval ($ params [2 ], $ elem );
@@ -244,7 +247,7 @@ private function execute($context = null): void
244
247
if (empty ($ params [0 ])) throw new ParseException ('missing condition expression ' , $ elem );
245
248
if (empty ($ params [1 ])) throw new ParseException ('missing value expression if true ' , $ elem );
246
249
247
- if ($ this ->eval ($ params [0 ], $ elem ) == true )
250
+ if ($ this ->eval ($ params [0 ], $ elem ))
248
251
$ elem ->setAttribute ($ attribute , $ this ->eval ($ params [1 ], $ elem ));
249
252
else if (isset ($ params [2 ]))
250
253
$ elem ->setAttribute ($ attribute , $ this ->eval ($ params [2 ], $ elem ));
@@ -261,7 +264,7 @@ private function execute($context = null): void
261
264
if (empty ($ params [1 ])) throw new ParseException ('missing condition expression ' , $ elem );
262
265
if (empty ($ params [2 ])) throw new ParseException ('missing value expression if true ' , $ elem );
263
266
264
- if ($ this ->eval ($ params [1 ], $ elem ) == true )
267
+ if ($ this ->eval ($ params [1 ], $ elem ))
265
268
$ elem ->setAttribute ($ params [0 ], $ this ->eval ($ params [3 ], $ elem ));
266
269
else if (isset ($ params [3 ]))
267
270
$ elem ->setAttribute ($ params [0 ], $ this ->eval ($ params [3 ], $ elem ));
0 commit comments