@@ -176,6 +176,10 @@ private function execute($context = null): void
176
176
continue ;
177
177
}
178
178
179
+ if (!$ this ->isString ($ result )) {
180
+ throw new ParseException ("` $ attr` expression result not a string " , $ elem );
181
+ }
182
+
179
183
$ elem ->nodeValue = '' ;
180
184
$ doc = $ this ->domDocumentFromHtml ("<?xml encoding= \"utf-8 \" ?><dataplater> $ result</dataplater> " );
181
185
$ this ->importTagChildren ($ doc , 'dataplater ' , fn ($ child ) => $ elem ->appendChild ($ child ));
@@ -190,13 +194,22 @@ private function execute($context = null): void
190
194
if ($ result === null ) {
191
195
continue ;
192
196
}
197
+
198
+ if (!$ this ->isString ($ result )) {
199
+ throw new ParseException ("` $ attr` expression result not a string " , $ elem );
200
+ }
201
+
193
202
$ elem ->nodeValue = $ result ;
194
203
}
195
204
196
205
// CUSTOM ATTRIBUTE
197
206
$ attr = "$ this ->attr -attr " ;
198
207
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 ;
200
213
$ elem ->removeAttribute ($ attr );
201
214
202
215
$ targetAttr = trim ($ targetAttr );
@@ -205,6 +218,11 @@ private function execute($context = null): void
205
218
if ($ result === null ) {
206
219
continue ;
207
220
}
221
+
222
+ if (!$ this ->isString ($ result )) {
223
+ throw new ParseException ("` $ attr` expression result not a string " , $ elem );
224
+ }
225
+
208
226
$ elem ->setAttribute ($ targetAttr , $ result );
209
227
}
210
228
@@ -215,6 +233,11 @@ private function execute($context = null): void
215
233
foreach ($ this ->xpath ->query ("{$ axis }[@ $ attr] " , $ context ) as $ elem ) {
216
234
$ result = $ this ->eval ($ elem ->getAttribute ($ attr ), $ elem );
217
235
$ elem ->removeAttribute ($ attr );
236
+
237
+ if (!$ this ->isString ($ result )) {
238
+ throw new ParseException ("` $ attr` expression result not a string " , $ elem );
239
+ }
240
+
218
241
if ($ result !== null ) {
219
242
$ elem ->setAttribute ($ targetAttr , $ result );
220
243
}
@@ -225,7 +248,7 @@ private function execute($context = null): void
225
248
/**
226
249
* @throws ParseException
227
250
*/
228
- private function eval (string $ expression , DOMElement $ node )
251
+ private function eval (string $ expression , DOMElement $ node ): mixed
229
252
{
230
253
try {
231
254
$ smpl = new SMPLang ($ this ->vars );
@@ -254,4 +277,9 @@ private function importTagChildren(DOMDocument $doc, string $tag, callable $forE
254
277
$ forEach ($ this ->doc ->importNode ($ child , true ));
255
278
}
256
279
}
280
+
281
+ private function isString (mixed $ value ): bool
282
+ {
283
+ return is_string ($ value ) || (is_object ($ value ) && method_exists ($ value , '__toString ' ));
284
+ }
257
285
}
0 commit comments