Skip to content

Commit 6006e43

Browse files
committed
improved template error reporting a bit
1 parent 9ab6326 commit 6006e43

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/Dataplater.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ private function execute($context = null): void
176176
continue;
177177
}
178178

179+
if (!$this->isString($result)) {
180+
throw new ParseException("`$attr` expression result not a string", $elem);
181+
}
182+
179183
$elem->nodeValue = '';
180184
$doc = $this->domDocumentFromHtml("<?xml encoding=\"utf-8\" ?><dataplater>$result</dataplater>");
181185
$this->importTagChildren($doc, 'dataplater', fn ($child) => $elem->appendChild($child));
@@ -190,13 +194,22 @@ private function execute($context = null): void
190194
if ($result === null) {
191195
continue;
192196
}
197+
198+
if (!$this->isString($result)) {
199+
throw new ParseException("`$attr` expression result not a string", $elem);
200+
}
201+
193202
$elem->nodeValue = $result;
194203
}
195204

196205
// CUSTOM ATTRIBUTE
197206
$attr = "$this->attr-attr";
198207
foreach ($this->xpath->query("{$axis}[@$attr]", $context) as $elem) {
199-
[$targetAttr, $expression] = explode(';', $elem->getAttribute($attr), 2);
208+
$value = explode(';', $elem->getAttribute($attr), 2);
209+
if (!isset($value[1])) {
210+
throw new ParseException("missing expression in attribute `$attr`", $elem);
211+
}
212+
[$targetAttr, $expression] = $value;
200213
$elem->removeAttribute($attr);
201214

202215
$targetAttr = trim($targetAttr);
@@ -205,6 +218,11 @@ private function execute($context = null): void
205218
if ($result === null) {
206219
continue;
207220
}
221+
222+
if (!$this->isString($result)) {
223+
throw new ParseException("`$attr` expression result not a string", $elem);
224+
}
225+
208226
$elem->setAttribute($targetAttr, $result);
209227
}
210228

@@ -215,6 +233,11 @@ private function execute($context = null): void
215233
foreach ($this->xpath->query("{$axis}[@$attr]", $context) as $elem) {
216234
$result = $this->eval($elem->getAttribute($attr), $elem);
217235
$elem->removeAttribute($attr);
236+
237+
if (!$this->isString($result)) {
238+
throw new ParseException("`$attr` expression result not a string", $elem);
239+
}
240+
218241
if ($result !== null) {
219242
$elem->setAttribute($targetAttr, $result);
220243
}
@@ -225,7 +248,7 @@ private function execute($context = null): void
225248
/**
226249
* @throws ParseException
227250
*/
228-
private function eval(string $expression, DOMElement $node)
251+
private function eval(string $expression, DOMElement $node): mixed
229252
{
230253
try {
231254
$smpl = new SMPLang($this->vars);
@@ -254,4 +277,9 @@ private function importTagChildren(DOMDocument $doc, string $tag, callable $forE
254277
$forEach($this->doc->importNode($child, true));
255278
}
256279
}
280+
281+
private function isString(mixed $value): bool
282+
{
283+
return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
284+
}
257285
}

0 commit comments

Comments
 (0)