Skip to content

Commit 15bc46a

Browse files
authored
Allowed interfaces as handled visitor FQCNs (#193)
1 parent a833a94 commit 15bc46a

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/contracts/Output/ValueObjectVisitorDispatcher.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function visit($data)
7575
}
7676
} while ($className = get_parent_class($className));
7777

78+
$interfaces = class_implements($data);
79+
foreach ($interfaces as $interface) {
80+
if (isset($this->visitors[$interface])) {
81+
return $this->visitors[$interface]->visit($this->outputVisitor, $this->outputGenerator, $data);
82+
}
83+
}
84+
7885
throw new Exceptions\NoVisitorFoundException($checkedClassNames);
7986
}
8087
}

tests/lib/Output/ValueObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Test dummy class.
1313
*/
14-
class ValueObject extends stdClass
14+
class ValueObject extends stdClass implements ValueObjectInterface
1515
{
1616
}
1717

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Rest\Output;
10+
11+
/**
12+
* Test dummy interface.
13+
*/
14+
interface ValueObjectInterface
15+
{
16+
}

tests/lib/Output/ValueObjectVisitorDispatcherTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ public function testVisitValueObjectParentMatch()
7979
$dispatcher->visit($data);
8080
}
8181

82+
public function testVisitInterface(): void
83+
{
84+
$data = new ValueObject();
85+
86+
$valueObjectVisitor = $this->getValueObjectVisitorMock();
87+
$valueObjectVisitor
88+
->expects(self::once())
89+
->method('visit')
90+
->with($this->getOutputVisitorMock(), $this->getOutputGeneratorMock(), $data);
91+
92+
$dispatcher = $this->getValueObjectDispatcher();
93+
$dispatcher->addVisitor(ValueObjectInterface::class, $valueObjectVisitor);
94+
95+
$dispatcher->visit($data);
96+
}
97+
8298
public function testVisitValueObjectSecondRuleParentMatch()
8399
{
84100
$data = new ValueObject();

0 commit comments

Comments
 (0)