Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow casters to parse null values and provide default or Injected values #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Fixtures/ClassWithDefaultValueProvidingCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace EventSauce\ObjectHydrator\Fixtures;

class ClassWithDefaultValueProvidingCaster
{
public function __construct(
#[DefaultValueProvidingCaster]
public string $valueProvidedFromCaster,
) {
}
}
16 changes: 16 additions & 0 deletions src/Fixtures/DefaultValueProvidingCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EventSauce\ObjectHydrator\Fixtures;

use Attribute;
use EventSauce\ObjectHydrator\ObjectMapper;
use EventSauce\ObjectHydrator\PropertyCaster;

#[Attribute(Attribute::TARGET_PARAMETER)]
final class DefaultValueProvidingCaster implements PropertyCaster
{
public function cast(mixed $value, ObjectMapper $hydrator): mixed
{
return 'some_default_value';
}
}
12 changes: 12 additions & 0 deletions src/ObjectHydrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use EventSauce\ObjectHydrator\Fixtures\ClassThatUsesMutipleCastersWithoutOptions;
use EventSauce\ObjectHydrator\Fixtures\ClassWithCamelCaseProperty;
use EventSauce\ObjectHydrator\Fixtures\ClassWithComplexTypeThatIsMapped;
use EventSauce\ObjectHydrator\Fixtures\ClassWithDefaultValueProvidingCaster;
use EventSauce\ObjectHydrator\Fixtures\ClassWithDocblockAndArrayFollowingScalar;
use EventSauce\ObjectHydrator\Fixtures\ClassWithDefaultValue;
use EventSauce\ObjectHydrator\Fixtures\ClassWithDocblockArrayVariants;
Expand Down Expand Up @@ -466,6 +467,17 @@ public function hydrating_a_class_with_a_nullable_property_defaults_to_null(): v
self::assertNull($object->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
*/
Expand Down
14 changes: 7 additions & 7 deletions src/ObjectMapperCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,6 @@ private function dumpClassHydrator(string $className, ClassHydrationDefinition $
$body .= <<<CODE

\$value = \$payload['$from'] ?? null;

if (\$value === null) {
$isNullBody
}

CODE;
} else {
$collectKeys = '';
Expand Down Expand Up @@ -295,15 +290,20 @@ private function dumpClassHydrator(string $className, ClassHydrationDefinition $
}

\$value = \${$casterName}->cast(\$value, \$this);
CODE;
}
}

if(isset($isNullBody)){
$body .= <<<CODE
if (\$value === null) {
$isNullBody
}

CODE;
}
}



if ($definition->isBackedEnum()) {
$body .= <<<CODE

Expand Down
13 changes: 0 additions & 13 deletions src/ObjectMapperUsingReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,13 @@ public function hydrateObject(string $className, array $payload): object
$property = $definition->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 */
$propertyCaster = $this->casterInstances[$key] ??= new $caster(...$options);
$value = $propertyCaster->cast($value, $this);
}

// same code as two sections above
if ($value === null) {
if ($definition->hasDefaultValue) {
continue;
Expand Down
4 changes: 4 additions & 0 deletions src/PropertyCasters/CastToDateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading