Skip to content

Commit 96c46b5

Browse files
committed
simplified bool expressions, added ternary functions, made dom and xpath props protected
1 parent b6029ab commit 96c46b5

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Dataplater.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Le\Dataplater;
44

5-
use DOMDocument, DOMElement, DOMXPath;
65
use InvalidArgumentException, ArgumentCountError, Exception;
6+
use DOMDocument, DOMElement, DOMXPath;
77

88
class Dataplater
99
{
1010

11-
private DOMDocument $doc;
12-
private DOMXPath $xpath;
11+
protected DOMDocument $doc;
12+
protected DOMXPath $xpath;
1313

1414
/**
1515
* @throws Exception
@@ -62,6 +62,9 @@ public function render(?array $vars = null): string
6262

6363
private function defineBuiltInFunctions(): void
6464
{
65+
$this->vars['logic']['ternary'] = fn($a, $b, $c) => $a ? $b : $c;
66+
$this->vars['logic']['shortTernary'] = fn($a, $b) => $a ?: $b;
67+
6568
$this->vars['logic']['and'] = fn($a, $b) => $a && $b;
6669
$this->vars['logic']['or'] = fn($a, $b) => $a || $b;
6770
$this->vars['logic']['not'] = fn($a) => !$a;
@@ -189,7 +192,7 @@ private function execute($context = null): void
189192

190193
$selector = "data-var-if";
191194
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))
193196
$elem->parentNode->removeChild($elem);
194197
else
195198
$elem->removeAttribute($selector);
@@ -203,7 +206,7 @@ private function execute($context = null): void
203206
if(empty($params[1])) throw new ParseException('missing value expression if true', $elem);
204207

205208
unset($value);
206-
if ($this->eval($params[0], $elem) == true)
209+
if ($this->eval($params[0], $elem))
207210
$value = $params[1];
208211
else if (isset($params[2]))
209212
$value = $params[2];
@@ -228,7 +231,7 @@ private function execute($context = null): void
228231
if(empty($params[0])) throw new ParseException('missing condition expression', $elem);
229232
if(empty($params[1])) throw new ParseException('missing value expression if true', $elem);
230233

231-
if ($this->eval($params[0], $elem) == true)
234+
if ($this->eval($params[0], $elem))
232235
$elem->nodeValue = $this->eval($params[1], $elem);
233236
else if (isset($params[2]))
234237
$elem->nodeValue = $this->eval($params[2], $elem);
@@ -244,7 +247,7 @@ private function execute($context = null): void
244247
if(empty($params[0])) throw new ParseException('missing condition expression', $elem);
245248
if(empty($params[1])) throw new ParseException('missing value expression if true', $elem);
246249

247-
if ($this->eval($params[0], $elem) == true)
250+
if ($this->eval($params[0], $elem))
248251
$elem->setAttribute($attribute, $this->eval($params[1], $elem));
249252
else if (isset($params[2]))
250253
$elem->setAttribute($attribute, $this->eval($params[2], $elem));
@@ -261,7 +264,7 @@ private function execute($context = null): void
261264
if(empty($params[1])) throw new ParseException('missing condition expression', $elem);
262265
if(empty($params[2])) throw new ParseException('missing value expression if true', $elem);
263266

264-
if($this->eval($params[1], $elem) == true)
267+
if($this->eval($params[1], $elem))
265268
$elem->setAttribute($params[0], $this->eval($params[3], $elem));
266269
else if (isset($params[3]))
267270
$elem->setAttribute($params[0], $this->eval($params[3], $elem));

0 commit comments

Comments
 (0)