diff --git a/src/CustomTag.php b/src/CustomTag.php index ce1327a..7910d42 100644 --- a/src/CustomTag.php +++ b/src/CustomTag.php @@ -3,6 +3,8 @@ namespace LordSimal\CustomHtmlElements; +use ReflectionObject; + abstract class CustomTag { /** @@ -23,8 +25,14 @@ public function __construct( public string $innerContent = '', ) { // Overwrite properties with what is given in the attributes + $reflection = new ReflectionObject($this); foreach ($attributes as $key => $value) { - if (isset($this->$key)) { + if ( + $reflection->hasProperty($key) + && $reflection->getProperty($key)->isPublic() + && !$reflection->getProperty($key)->isStatic() + && $reflection->getProperty($key)->getDeclaringClass()->getName() !== self::class + ) { $this->$key = $value; } } diff --git a/tests/TagEngine/CustomTagsTest.php b/tests/TagEngine/CustomTagsTest.php index ed3977b..377104a 100644 --- a/tests/TagEngine/CustomTagsTest.php +++ b/tests/TagEngine/CustomTagsTest.php @@ -4,6 +4,7 @@ namespace LordSimal\CustomHtmlElements\Test\TagEngine; use LordSimal\CustomHtmlElements\TagEngine; +use LordSimal\CustomHtmlElements\Test\Tags\ClassProperties; use PHPUnit\Framework\TestCase; /** @@ -11,6 +12,13 @@ */ class CustomTagsTest extends TestCase { + public function testHydratesNullablePublicProperty(): void + { + $tag = new ClassProperties(['nullable' => 'value']); + + $this->assertSame('value', $tag->nullable); + } + protected TagEngine $tagEngine; protected function setUp(): void diff --git a/tests/Tags/ClassProperties.php b/tests/Tags/ClassProperties.php index a4be9f0..c1b33e9 100644 --- a/tests/Tags/ClassProperties.php +++ b/tests/Tags/ClassProperties.php @@ -14,6 +14,8 @@ class ClassProperties extends CustomTag public string $test = 'default'; + public ?string $nullable = null; + public function render(): string { return <<< HTML