Skip to content

Commit 947678a

Browse files
committed
Signals should get processed before typeof requirements
1 parent 16eb4cf commit 947678a

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/viewmodel/ViewModelRenderer.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ private function processProperty(DOMElement $context, object $model): object {
351351
);
352352
}
353353

354+
if ($result instanceof Remove || $result === false) {
355+
$context->remove();
356+
357+
return $model;
358+
}
359+
360+
if ($result instanceof Ignore || $result === true || $result === null) {
361+
return $model;
362+
}
363+
354364
if ($context->hasAttribute('typeof')) {
355365
if (!is_iterable($result) && !is_object($result)) {
356366
throw new ViewModelRendererException(
@@ -382,16 +392,6 @@ private function processProperty(DOMElement $context, object $model): object {
382392
return $model;
383393
}
384394

385-
if ($result instanceof Remove || $result === false) {
386-
$context->remove();
387-
388-
return $model;
389-
}
390-
391-
if ($result instanceof Ignore || $result === true || $result === null) {
392-
return $model;
393-
}
394-
395395
if (is_object($result)) {
396396
$this->objectApply($context, $result);
397397

tests/viewmodel/ViewModelRendererTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,55 @@ public function asString(): Document {
14711471
$exp->loadXML('<root property="test"><div> sub document </div></root>');
14721472

14731473
$this->assertResultMatches($exp->documentElement, $dom->documentElement);
1474+
}
1475+
1476+
public function testRemoveSignalIsProcessedBeforeTypeOfAttribute(): void {
1477+
$dom = new DOMDocument();
1478+
$dom->loadXML('<root><node property="test" typeof="foo" /></root>');
1479+
1480+
$model = new class() {
1481+
public function test(): Signal {
1482+
return Signal::remove();
1483+
}
1484+
};
1485+
1486+
$renderer = new ViewModelRenderer();
1487+
$renderer->render($dom->documentElement, $model);
14741488

1489+
$renderer = new ViewModelRenderer();
1490+
$renderer->render($dom->documentElement, $model);
1491+
1492+
$exp = new DOMDocument();
1493+
$exp->loadXML('<root />');
1494+
1495+
$this->assertResultMatches($exp->documentElement, $dom->documentElement);
1496+
}
1497+
1498+
public function testIgnoreSignalIsProcessedBeforeTypeOfAttribute(): void {
1499+
$dom = new DOMDocument();
1500+
$dom->loadXML('<root><node property="test" typeof="foo"><child property="text" /></node></root>');
1501+
1502+
$model = new class() {
1503+
public function test(): Signal {
1504+
return Signal::ignore();
1505+
}
1506+
1507+
public function text():string {
1508+
return 'works';
1509+
}
1510+
1511+
};
1512+
1513+
$renderer = new ViewModelRenderer();
1514+
$renderer->render($dom->documentElement, $model);
1515+
1516+
$renderer = new ViewModelRenderer();
1517+
$renderer->render($dom->documentElement, $model);
1518+
1519+
$exp = new DOMDocument();
1520+
$exp->loadXML('<root><node property="test" typeof="foo"><child property="text">works</child></node></root>');
1521+
1522+
$this->assertResultMatches($exp->documentElement, $dom->documentElement);
14751523
}
14761524

14771525
}

0 commit comments

Comments
 (0)