Skip to content

Commit 118eeb5

Browse files
authored
fix(mapper): nullable datetime caster (tempestphp#974)
1 parent b6252dc commit 118eeb5

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

src/Tempest/Mapper/src/Casters/DateTimeCaster.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
public function __construct(
1818
private string $format = DATE_ATOM,
1919
private bool $immutable = true,
20-
) {
21-
}
20+
) {}
2221

2322
public static function fromProperty(PropertyReflector $property): DateTimeCaster
2423
{
@@ -30,8 +29,12 @@ public static function fromProperty(PropertyReflector $property): DateTimeCaster
3029
};
3130
}
3231

33-
public function cast(mixed $input): DateTimeInterface
32+
public function cast(mixed $input): ?DateTimeInterface
3433
{
34+
if (! $input) {
35+
return null;
36+
}
37+
3538
if ($input instanceof DateTimeInterface) {
3639
return $input;
3740
}

src/Tempest/Mapper/src/Mappers/ArrayToObjectMapper.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
{
2222
public function __construct(
2323
private CasterFactory $casterFactory,
24-
) {
25-
}
24+
) {}
2625

2726
public function canMap(mixed $from, mixed $to): bool
2827
{
@@ -93,7 +92,7 @@ public function map(mixed $from, mixed $to): object
9392
if ($value instanceof UnknownValue) {
9493
$caster = $this->casterFactory->forProperty($property);
9594

96-
$value = $caster?->cast($from[$propertyName]) ?? $from[$propertyName];
95+
$value = $caster ? $caster->cast($from[$propertyName]) : $from[$propertyName];
9796
}
9897

9998
$property->setValue($object, $value);
@@ -145,7 +144,8 @@ private function resolveValueFromType(
145144
mixed $data,
146145
PropertyReflector $property,
147146
object $parent,
148-
): mixed {
147+
): mixed
148+
{
149149
$type = $property->getType();
150150

151151
if ($type->isBuiltIn()) {
@@ -155,7 +155,7 @@ private function resolveValueFromType(
155155
$caster = $this->casterFactory->forProperty($property);
156156

157157
if (! is_array($data)) {
158-
return $caster?->cast($data) ?? $data;
158+
return $caster ? $caster->cast($data) : $data;
159159
}
160160

161161
$data = $this->withParentRelations(
@@ -165,7 +165,7 @@ private function resolveValueFromType(
165165
);
166166

167167
return $this->map(
168-
from: $caster?->cast($data) ?? $data,
168+
from: $caster ? $caster->cast($data) : $data,
169169
to: $type->getName(),
170170
);
171171
}
@@ -174,7 +174,8 @@ private function resolveValueFromArray(
174174
mixed $data,
175175
PropertyReflector $property,
176176
object $parent,
177-
): UnknownValue|array {
177+
): UnknownValue|array
178+
{
178179
$type = $property->getIterableType();
179180

180181
if ($type === null) {
@@ -192,7 +193,7 @@ private function resolveValueFromArray(
192193

193194
foreach ($data as $key => $item) {
194195
if (! is_array($item)) {
195-
$values[$key] = $caster?->cast($item) ?? $item;
196+
$values[$key] = $caster ? $caster->cast($item) : $item;
196197

197198
continue;
198199
}
@@ -204,7 +205,7 @@ private function resolveValueFromArray(
204205
);
205206

206207
$values[] = $this->map(
207-
from: $caster?->cast($item) ?? $item,
208+
from: $caster ? $caster->cast($item) : $item,
208209
to: $type->getName(),
209210
);
210211
}
@@ -223,7 +224,8 @@ private function withParentRelations(
223224
ClassReflector $child,
224225
object $parent,
225226
array $data,
226-
): array {
227+
): array
228+
{
227229
foreach ($child->getPublicProperties() as $property) {
228230
if ($property->getType()->getName() === $parent::class) {
229231
$data[$property->getName()] = $parent;

tests/Integration/Mapper/Fixtures/ObjectWithBuiltInCasters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
final class ObjectWithBuiltInCasters
1212
{
13+
public ?DateTimeImmutable $nullableDateTimeImmutable = null;
14+
1315
public DateTimeImmutable $dateTimeObject;
1416

1517
public DateTimeImmutable $dateTimeImmutable;

tests/Integration/Mapper/Mappers/ArrayToObjectMapperTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function test_default_values(): void
6565
public function test_built_in_casters(): void
6666
{
6767
$object = map([
68+
'nullableDateTimeImmutable' => '',
6869
'dateTimeObject' => new DateTimeImmutable('2024-01-01 10:10:10'),
6970
'dateTimeImmutable' => '2024-01-01 10:10:10',
7071
'dateTime' => '2024-01-01 10:10:10',
@@ -78,6 +79,7 @@ public function test_built_in_casters(): void
7879
$this->assertSame('2024-01-01 10:10:10', $object->dateTimeImmutable->format('Y-m-d H:i:s'));
7980
$this->assertSame('2024-01-01 10:10:10', $object->dateTime->format('Y-m-d H:i:s'));
8081
$this->assertSame('2024-12-01 10:10:10', $object->dateTimeWithFormat->format('Y-m-d H:i:s'));
82+
$this->assertNull($object->nullableDateTimeImmutable);
8183
$this->assertSame(true, $object->bool);
8284
$this->assertSame(0.1, $object->float);
8385
$this->assertSame(1, $object->int);

0 commit comments

Comments
 (0)