Skip to content

Commit

Permalink
Merge pull request #32 from nutgram/fix-arraytypes-of-abstract
Browse files Browse the repository at this point in the history
Fix hydration for array of abstract types
  • Loading branch information
sergix44 authored Dec 30, 2023
2 parents 7ef4ef2 + 9c218bf commit 687a6df
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ "8.0", "8.1", "8.2" ]
php-versions: [ "8.1", "8.2", "8.3" ]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

/**
* @Annotation
*
* @Target({"PROPERTY"})
*
* @NamedArgumentConstructor
*
* @Attributes({
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

/**
* @Annotation
*
* @Target({"PROPERTY"})
*
* @NamedArgumentConstructor
*
* @Attributes({
Expand Down
1 change: 0 additions & 1 deletion src/Annotation/ConcreteResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* @Annotation
*
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
Expand Down
1 change: 0 additions & 1 deletion src/Annotation/Mutate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/**
* @Annotation
*
* @Target({"PROPERTY"})
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
Expand Down
1 change: 0 additions & 1 deletion src/Annotation/SkipConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* @Annotation
*
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
Expand Down
1 change: 0 additions & 1 deletion src/Annotation/UnionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/**
* @Annotation
*
* @Target({"PROPERTY"})
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
Expand Down
8 changes: 5 additions & 3 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,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 +240,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 +615,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 687a6df

Please sign in to comment.