Skip to content

Commit d695c3f

Browse files
committed
fixed ReflectionNormalizer to include inherited properties not returned by default by ReflectionClass::getProperties
1 parent 92445df commit d695c3f

5 files changed

Lines changed: 77 additions & 6 deletions

File tree

src/Normalizer/ReflectionNormalizer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ public function __invoke($var)
1818
$ref = new \ReflectionObject($var);
1919

2020
$result = array();
21-
foreach($ref->getProperties() as $property) {
22-
if(in_array($property->getName(), $this->skipped)) {
23-
continue;
24-
}
21+
while($ref) {
22+
foreach($ref->getProperties() as $property) {
23+
if(in_array($property->getName(), $this->skipped)) {
24+
continue;
25+
}
26+
27+
$property->setAccessible(true);
2528

26-
$property->setAccessible(true);
29+
$result[$property->getName()] = $property->getValue($var);
30+
}
2731

28-
$result[$property->getName()] = $property->getValue($var);
32+
$ref = $ref->getParentClass();
2933
}
3034

3135
return $result;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Thunder\Serializard\Tests\Fake\Inheritance;
3+
4+
class FakeClass extends FakeClassParent
5+
{
6+
private $property;
7+
8+
public function __construct($parentParent, $parent, $property)
9+
{
10+
parent::__construct($parentParent, $parent);
11+
12+
$this->property = $property;
13+
}
14+
15+
public function getProperty()
16+
{
17+
return $this->property;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Thunder\Serializard\Tests\Fake\Inheritance;
3+
4+
class FakeClassParent extends FakeClassParentParent
5+
{
6+
private $parentProperty;
7+
8+
public function __construct($parent, $property)
9+
{
10+
parent::__construct($parent);
11+
12+
$this->parentProperty = $property;
13+
}
14+
15+
public function getParentProperty()
16+
{
17+
return $this->parentProperty;
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Thunder\Serializard\Tests\Fake\Inheritance;
3+
4+
class FakeClassParentParent
5+
{
6+
private $parentParentProperty;
7+
8+
public function __construct($property)
9+
{
10+
$this->parentParentProperty = $property;
11+
}
12+
13+
public function getParentParentProperty()
14+
{
15+
return $this->parentParentProperty;
16+
}
17+
}

tests/NormalizerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Thunder\Serializard\Tests;
33

44
use Thunder\Serializard\Normalizer\ReflectionNormalizer;
5+
use Thunder\Serializard\Tests\Fake\Inheritance\FakeClass;
56
use Thunder\Serializard\Tests\Fake\FakeTag;
67
use Thunder\Serializard\Tests\Fake\FakeUser;
78

@@ -17,4 +18,15 @@ public function testReflectionSkip()
1718

1819
$this->assertSame(array('id' => 12, 'name' => 'XXX'), $normalizer($object));
1920
}
21+
22+
public function testReflectionInheritance()
23+
{
24+
$normalizer = new ReflectionNormalizer();
25+
26+
$this->assertSame(array(
27+
'property' => 'property',
28+
'parentProperty' => 'parent',
29+
'parentParentProperty' => 'parentParent',
30+
), $normalizer(new FakeClass('parentParent', 'parent', 'property')));
31+
}
2032
}

0 commit comments

Comments
 (0)