Skip to content

Commit 687a6df

Browse files
authored
Merge pull request #32 from nutgram/fix-arraytypes-of-abstract
Fix hydration for array of abstract types
2 parents 7ef4ef2 + 9c218bf commit 687a6df

File tree

10 files changed

+59
-12
lines changed

10 files changed

+59
-12
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
operating-system: [ ubuntu-latest ]
15-
php-versions: [ "8.0", "8.1", "8.2" ]
15+
php-versions: [ "8.1", "8.2", "8.3" ]
1616

1717
steps:
1818
- uses: actions/checkout@v2

src/Annotation/Alias.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
/**
88
* @Annotation
9-
*
109
* @Target({"PROPERTY"})
11-
*
1210
* @NamedArgumentConstructor
1311
*
1412
* @Attributes({

src/Annotation/ArrayType.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
/**
99
* @Annotation
10-
*
1110
* @Target({"PROPERTY"})
12-
*
1311
* @NamedArgumentConstructor
1412
*
1513
* @Attributes({

src/Annotation/ConcreteResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @Annotation
9-
*
109
* @Target({"CLASS"})
1110
*/
1211
#[Attribute(Attribute::TARGET_CLASS)]

src/Annotation/Mutate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
/**
1010
* @Annotation
11-
*
1211
* @Target({"PROPERTY"})
1312
*/
1413
#[Attribute(Attribute::TARGET_PROPERTY)]

src/Annotation/SkipConstructor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @Annotation
9-
*
109
* @Target({"CLASS"})
1110
*/
1211
#[Attribute(Attribute::TARGET_CLASS)]

src/Annotation/UnionResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
/**
1010
* @Annotation
11-
*
1211
* @Target({"PROPERTY"})
1312
*/
1413
#[Attribute(Attribute::TARGET_PROPERTY)]

src/Hydrator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ public function hydrate(string|object $object, array|object $data): object
9090
if ($property->isStatic()) {
9191
continue;
9292
}
93-
94-
$property->setAccessible(true);
9593
$propertyType = $property->getType();
9694

9795
if ($propertyType === null) {
@@ -242,6 +240,10 @@ private function initializeObject(string|object $object, array|object $data): ob
242240
));
243241
}
244242

243+
if (is_object($data)) {
244+
$data = get_object_vars($data);
245+
}
246+
245247
return $this->initializeObject($attribute->concreteFor($data), $data);
246248
}
247249

@@ -613,7 +615,7 @@ private function hydrateObjectsInArray(array $array, ArrayType $arrayType, int $
613615
return $arrayType->class::tryFrom($object);
614616
}
615617

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

618620
return $this->hydrate($newInstance, $object);
619621
}, $array);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace SergiX44\Hydrator\Tests\Fixtures;
4+
5+
use SergiX44\Hydrator\Annotation\ArrayType;
6+
use SergiX44\Hydrator\Tests\Fixtures\Store\Apple;
7+
8+
final class ObjectWithArrayOfAbstracts
9+
{
10+
#[ArrayType(Apple::class)]
11+
public array $value;
12+
}

tests/HydratorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use SergiX44\Hydrator\Tests\Fixtures\DI\Tree;
1616
use SergiX44\Hydrator\Tests\Fixtures\DI\Wood;
1717
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithAbstract;
18+
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithArrayOfAbstracts;
1819
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithInvalidAbstract;
1920
use SergiX44\Hydrator\Tests\Fixtures\Resolver\AppleResolver;
2021
use SergiX44\Hydrator\Tests\Fixtures\Store\Apple;
@@ -751,6 +752,46 @@ public function testHydrateAbstractProperty(): void
751752
$this->assertSame('brandy', $o->value->category);
752753
}
753754

755+
public function testHydrateArrayAbstractProperty(): void
756+
{
757+
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
758+
'value' => [[
759+
'type' => 'jack',
760+
'sweetness' => null,
761+
'category' => 'brandy',
762+
]],
763+
]);
764+
765+
$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
766+
$this->assertIsArray($o->value);
767+
768+
$value = $o->value[0];
769+
770+
$this->assertInstanceOf(AppleJack::class, $value);
771+
$this->assertSame('jack', $value->type);
772+
$this->assertSame('brandy', $value->category);
773+
}
774+
775+
public function testHydrateArrayAbstractPropertyWithObject(): void
776+
{
777+
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
778+
'value' => [(object) [
779+
'type' => 'jack',
780+
'sweetness' => null,
781+
'category' => 'brandy',
782+
]],
783+
]);
784+
785+
$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
786+
$this->assertIsArray($o->value);
787+
788+
$value = $o->value[0];
789+
790+
$this->assertInstanceOf(AppleJack::class, $value);
791+
$this->assertSame('jack', $value->type);
792+
$this->assertSame('brandy', $value->category);
793+
}
794+
754795
public function testHydrateInvalidAbstractObject(): void
755796
{
756797
$this->expectException(InvalidObjectException::class);

0 commit comments

Comments
 (0)