diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 3449b44..b33b8ad 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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 diff --git a/src/Annotation/Alias.php b/src/Annotation/Alias.php index dce0b40..012db93 100644 --- a/src/Annotation/Alias.php +++ b/src/Annotation/Alias.php @@ -6,9 +6,7 @@ /** * @Annotation - * * @Target({"PROPERTY"}) - * * @NamedArgumentConstructor * * @Attributes({ diff --git a/src/Annotation/ArrayType.php b/src/Annotation/ArrayType.php index 84668aa..f9d342d 100644 --- a/src/Annotation/ArrayType.php +++ b/src/Annotation/ArrayType.php @@ -7,9 +7,7 @@ /** * @Annotation - * * @Target({"PROPERTY"}) - * * @NamedArgumentConstructor * * @Attributes({ diff --git a/src/Annotation/ConcreteResolver.php b/src/Annotation/ConcreteResolver.php index 80e66ec..4d20031 100644 --- a/src/Annotation/ConcreteResolver.php +++ b/src/Annotation/ConcreteResolver.php @@ -6,7 +6,6 @@ /** * @Annotation - * * @Target({"CLASS"}) */ #[Attribute(Attribute::TARGET_CLASS)] diff --git a/src/Annotation/Mutate.php b/src/Annotation/Mutate.php index 07ad104..f85dbf7 100644 --- a/src/Annotation/Mutate.php +++ b/src/Annotation/Mutate.php @@ -8,7 +8,6 @@ /** * @Annotation - * * @Target({"PROPERTY"}) */ #[Attribute(Attribute::TARGET_PROPERTY)] diff --git a/src/Annotation/SkipConstructor.php b/src/Annotation/SkipConstructor.php index 25792bc..e994f30 100644 --- a/src/Annotation/SkipConstructor.php +++ b/src/Annotation/SkipConstructor.php @@ -6,7 +6,6 @@ /** * @Annotation - * * @Target({"CLASS"}) */ #[Attribute(Attribute::TARGET_CLASS)] diff --git a/src/Annotation/UnionResolver.php b/src/Annotation/UnionResolver.php index e828846..46a20fa 100644 --- a/src/Annotation/UnionResolver.php +++ b/src/Annotation/UnionResolver.php @@ -8,7 +8,6 @@ /** * @Annotation - * * @Target({"PROPERTY"}) */ #[Attribute(Attribute::TARGET_PROPERTY)] diff --git a/src/Hydrator.php b/src/Hydrator.php index c933821..750b5fd 100644 --- a/src/Hydrator.php +++ b/src/Hydrator.php @@ -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) { @@ -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); } @@ -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); diff --git a/tests/Fixtures/ObjectWithArrayOfAbstracts.php b/tests/Fixtures/ObjectWithArrayOfAbstracts.php new file mode 100644 index 0000000..9ca83b3 --- /dev/null +++ b/tests/Fixtures/ObjectWithArrayOfAbstracts.php @@ -0,0 +1,12 @@ +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);