Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
dto: ignore static properties also when serializing
Browse files Browse the repository at this point in the history
The previous fix in66618c96c88f83a9e6c19eb2a0c8868ea18974d9 didn't
consider serialization where we also need to skip it.
  • Loading branch information
mfn committed Jan 22, 2020
1 parent 2deba88 commit 89c698d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function all(): array
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);

foreach ($properties as $reflectionProperty) {
// Skip static properties
if ($reflectionProperty->isStatic()) {
continue;
}

$data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,20 @@ public function ignore_static_public_properties()

$this->assertSame('bar', $object->foo);
}

/** @test */
public function ignore_static_public_properties_when_serializing()
{
$object = new class(['foo' => 'bar']) extends DataTransferObject {
/** @var string */
public $foo;
public static $prop;
};

$expected = [
'foo' => 'bar',
];

$this->assertSame($expected, $object->all());
}
}

0 comments on commit 89c698d

Please sign in to comment.