Skip to content

Commit

Permalink
Fix hydration for array of abstract types
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Dec 30, 2023
1 parent 7ef4ef2 commit bc2a371
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use SergiX44\Hydrator\Annotation\SkipConstructor;
use SergiX44\Hydrator\Annotation\UnionResolver;
use SergiX44\Hydrator\Exception\InvalidObjectException;

use function array_key_exists;
use function class_exists;
use function ctype_digit;
Expand All @@ -38,7 +37,6 @@
use function is_subclass_of;
use function sprintf;
use function strtotime;

use const FILTER_NULL_ON_FAILURE;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_FLOAT;
Expand Down Expand Up @@ -90,8 +88,6 @@ public function hydrate(string|object $object, array|object $data): object
if ($property->isStatic()) {
continue;
}

$property->setAccessible(true);
$propertyType = $property->getType();

if ($propertyType === null) {
Expand Down Expand Up @@ -242,6 +238,10 @@ private function initializeObject(string|object $object, array|object $data): ob
));
}

if (is_object($data)) {
$data = get_object_vars($data);
}

return $this->initializeObject($attribute->concreteFor($data), $data);
}

Expand Down Expand Up @@ -613,7 +613,7 @@ private function hydrateObjectsInArray(array $array, ArrayType $arrayType, int $
return $arrayType->class::tryFrom($object);
}

$newInstance = $this->initializeObject($arrayType->class, []);
$newInstance = $this->initializeObject($arrayType->class, $object);

return $this->hydrate($newInstance, $object);
}, $array);
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/ObjectWithArrayOfAbstracts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace SergiX44\Hydrator\Tests\Fixtures;

use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Hydrator\Tests\Fixtures\Store\Apple;

final class ObjectWithArrayOfAbstracts
{
#[ArrayType(Apple::class)]
public array $value;
}
41 changes: 41 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SergiX44\Hydrator\Tests\Fixtures\DI\Tree;
use SergiX44\Hydrator\Tests\Fixtures\DI\Wood;
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithAbstract;
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithArrayOfAbstracts;
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithInvalidAbstract;
use SergiX44\Hydrator\Tests\Fixtures\Resolver\AppleResolver;
use SergiX44\Hydrator\Tests\Fixtures\Store\Apple;
Expand Down Expand Up @@ -751,6 +752,46 @@ public function testHydrateAbstractProperty(): void
$this->assertSame('brandy', $o->value->category);
}

public function testHydrateArrayAbstractProperty(): void
{
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
'value' => [[
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
]],
]);

$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
$this->assertIsArray($o->value);

$value = $o->value[0];

$this->assertInstanceOf(AppleJack::class, $value);
$this->assertSame('jack', $value->type);
$this->assertSame('brandy', $value->category);
}

public function testHydrateArrayAbstractPropertyWithObject(): void
{
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
'value' => [(object)[
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
]],
]);

$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
$this->assertIsArray($o->value);

$value = $o->value[0];

$this->assertInstanceOf(AppleJack::class, $value);
$this->assertSame('jack', $value->type);
$this->assertSame('brandy', $value->category);
}

public function testHydrateInvalidAbstractObject(): void
{
$this->expectException(InvalidObjectException::class);
Expand Down

0 comments on commit bc2a371

Please sign in to comment.