Skip to content

Commit

Permalink
Fix for RFC laminas#114 - Recursively add parent properties if specif…
Browse files Browse the repository at this point in the history
…ied.

Signed-off-by: Ian Foulds <[email protected]>
  • Loading branch information
ianef committed Oct 10, 2023
1 parent a4c7c73 commit 29850d9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/ReflectionHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class ReflectionHydrator extends AbstractHydrator
*
* {@inheritDoc}
*/
public function extract(object $object): array
public function extract(object $object, bool $includeParentProperties = false): array
{
$result = [];
foreach (self::getReflProperties($object) as $property) {
foreach (self::getReflProperties($object, $includeParentProperties) as $property) {
$propertyName = $this->extractName($property->getName(), $object);
if (! $this->getCompositeFilter()->filter($propertyName)) {
continue;
Expand All @@ -42,9 +42,9 @@ public function extract(object $object): array
*
* {@inheritDoc}
*/
public function hydrate(array $data, object $object)
public function hydrate(array $data, object $object, bool $includeParentProperties = false)
{
$reflProperties = self::getReflProperties($object);
$reflProperties = self::getReflProperties($object, $includeParentProperties);
foreach ($data as $key => $value) {
$name = $this->hydrateName($key, $data);
if (isset($reflProperties[$name])) {
Expand All @@ -60,21 +60,23 @@ public function hydrate(array $data, object $object)
*
* @return ReflectionProperty[]
*/
protected static function getReflProperties(object $input): array
protected static function getReflProperties(object $input, bool $includeParentProperties): array
{
$class = $input::class;
$class = get_class($input);

if (isset(static::$reflProperties[$class])) {
return static::$reflProperties[$class];
}

static::$reflProperties[$class] = [];
$reflClass = new ReflectionClass($class);
$reflProperties = $reflClass->getProperties();
$reflClass = new ReflectionClass($class);

foreach ($reflProperties as $property) {
static::$reflProperties[$class][$property->getName()] = $property;
}
do {
foreach ($reflClass->getProperties() as $property) {
$property->setAccessible(true);
static::$reflProperties[$class][$property->getName()] = $property;
}
} while ($includeParentProperties === true && ($reflClass = $reflClass->getParentClass()) !== false);

return static::$reflProperties[$class];
}
Expand Down

0 comments on commit 29850d9

Please sign in to comment.