Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace LordSimal\CustomHtmlElements;

use ReflectionObject;

abstract class CustomTag
{
/**
Expand All @@ -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;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/TagEngine/CustomTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
namespace LordSimal\CustomHtmlElements\Test\TagEngine;

use LordSimal\CustomHtmlElements\TagEngine;
use LordSimal\CustomHtmlElements\Test\Tags\ClassProperties;
use PHPUnit\Framework\TestCase;

/**
* @see \LordSimal\CustomHtmlElements\TagEngine
*/
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
Expand Down
2 changes: 2 additions & 0 deletions tests/Tags/ClassProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ClassProperties extends CustomTag

public string $test = 'default';

public ?string $nullable = null;

public function render(): string
{
return <<< HTML
Expand Down