diff --git a/src/Hydrator.php b/src/Hydrator.php index d57f439..bc6d020 100644 --- a/src/Hydrator.php +++ b/src/Hydrator.php @@ -118,7 +118,7 @@ public function hydrate(string|object $object, array|object $data): object $propertyType = $resolver->resolve( $key, $propertyType->getTypes(), - is_array($data[$key]) ? $data[$key] : $data + isset($data[$key]) && is_array($data[$key]) ? $data[$key] : $data ); } else { throw new Exception\UnsupportedPropertyTypeException(sprintf( diff --git a/src/Resolver/EnumOrScalar.php b/src/Resolver/EnumOrScalar.php index 7bb2129..fad2170 100644 --- a/src/Resolver/EnumOrScalar.php +++ b/src/Resolver/EnumOrScalar.php @@ -39,6 +39,7 @@ public function resolve(string $propertyName, array $propertyTypes, array $data) 'integer' => 'int', 'double' => 'float', 'boolean' => 'bool', + 'NULL' => 'null', default => $valueType, }; diff --git a/tests/Fixtures/ObjectWithStringableEnumNullableUnion.php b/tests/Fixtures/ObjectWithStringableEnumNullableUnion.php new file mode 100644 index 0000000..1c0cc05 --- /dev/null +++ b/tests/Fixtures/ObjectWithStringableEnumNullableUnion.php @@ -0,0 +1,11 @@ +assertSame(.23, $object->value); } + public function testHydrateStringableEnumUnionPropertyNull(): void + { + $object = (new Hydrator())->hydrate(Fixtures\ObjectWithStringableEnumNullableUnion::class, ['value' => null]); + + $this->assertNull($object->value); + } + + public function testHydrateStringableEnumUnionPropertyNullNonSet(): void + { + $object = (new Hydrator())->hydrate(Fixtures\ObjectWithStringableEnumNullableUnion::class, []); + + $this->assertNull($object->value); + } + public function testHydrateStringableEnumUnionPropertyBool(): void { $this->expectException(Exception\UnsupportedPropertyTypeException::class);