From 614fc78a4666decf2e8852793893f60da3f96bf5 Mon Sep 17 00:00:00 2001 From: Robertbaelde Date: Fri, 1 Mar 2024 15:21:47 +0100 Subject: [PATCH] Allow casters to provide null values --- .../ClassWithDefaultValueProvidingCaster.php | 14 ++++++++++++++ src/Fixtures/DefaultValueProvidingCaster.php | 16 ++++++++++++++++ src/ObjectHydrationTestCase.php | 12 ++++++++++++ src/ObjectMapperCodeGenerator.php | 14 +++++++------- src/ObjectMapperUsingReflection.php | 13 ------------- src/PropertyCasters/CastToDateTimeImmutable.php | 4 ++++ 6 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/Fixtures/ClassWithDefaultValueProvidingCaster.php create mode 100644 src/Fixtures/DefaultValueProvidingCaster.php diff --git a/src/Fixtures/ClassWithDefaultValueProvidingCaster.php b/src/Fixtures/ClassWithDefaultValueProvidingCaster.php new file mode 100644 index 0000000..e3e4cc5 --- /dev/null +++ b/src/Fixtures/ClassWithDefaultValueProvidingCaster.php @@ -0,0 +1,14 @@ +defaultsToNull); } + /** @test */ + public function hydrating_a_class_with_a_default_value_providing_caster(): void + { + $hydrator = $this->createObjectHydrator(); + + $object = $hydrator->hydrateObject(ClassWithDefaultValueProvidingCaster::class, []); + + self::assertInstanceOf(ClassWithDefaultValueProvidingCaster::class, $object); + self::assertEquals('some_default_value', $object->valueProvidedFromCaster); + } + /** * @test */ diff --git a/src/ObjectMapperCodeGenerator.php b/src/ObjectMapperCodeGenerator.php index cab9471..3824222 100644 --- a/src/ObjectMapperCodeGenerator.php +++ b/src/ObjectMapperCodeGenerator.php @@ -245,11 +245,6 @@ private function dumpClassHydrator(string $className, ClassHydrationDefinition $ $body .= <<cast(\$value, \$this); +CODE; + } + } + if(isset($isNullBody)){ + $body .= <<isBackedEnum()) { $body .= <<accessorName; $value = $this->extractPayloadViaMap($payload, $keys); - // same code as two sections below - if ($value === null) { - if ($definition->hasDefaultValue) { - continue; - } elseif ($definition->nullable) { - $properties[$property] = null; - } else { - $missingFields[] = implode('.', end($keys)); - } - continue; - } - foreach ($definition->casters as [$caster, $options]) { $key = $className . '-' . $caster . '-' . json_encode($options); /** @var PropertyCaster $propertyCaster */ @@ -117,7 +105,6 @@ public function hydrateObject(string $className, array $payload): object $value = $propertyCaster->cast($value, $this); } - // same code as two sections above if ($value === null) { if ($definition->hasDefaultValue) { continue; diff --git a/src/PropertyCasters/CastToDateTimeImmutable.php b/src/PropertyCasters/CastToDateTimeImmutable.php index ce508e5..dcb6db1 100644 --- a/src/PropertyCasters/CastToDateTimeImmutable.php +++ b/src/PropertyCasters/CastToDateTimeImmutable.php @@ -23,6 +23,10 @@ public function __construct(private ?string $format = null, private ?string $tim public function cast(mixed $value, ObjectMapper $hydrator): mixed { + if($value === null){ + return null; + } + $timeZone = $this->timeZone ? new DateTimeZone($this->timeZone) : $this->timeZone; if ($this->format !== null) {