Skip to content

Commit 2df0860

Browse files
authored
fix(laravel): Eloquent date and datetime type detection (#6529)
* fix(laravel): Eloquent date and datetime type detection * fix
1 parent 2909441 commit 2df0860

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,16 @@ public function create(string $resourceClass, string $property, array $options =
6666
}
6767

6868
$builtinType = $p['cast'] ?? $p['type'];
69-
if ('datetime' === $builtinType) {
70-
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class);
71-
} else {
72-
if (\in_array($builtinType, Type::$builtinTypes, true)) {
73-
$type = new Type($builtinType, $p['nullable']);
74-
} else {
75-
$type = new Type(Type::BUILTIN_TYPE_STRING, $p['nullable']);
76-
}
77-
}
69+
$type = match ($builtinType) {
70+
'datetime', 'date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
71+
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
72+
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']),
73+
};
7874

79-
$propertyMetadata = $propertyMetadata
75+
return $propertyMetadata
8076
->withBuiltinTypes([$type])
8177
->withWritable($propertyMetadata->isWritable() ?? true)
8278
->withReadable($propertyMetadata->isReadable() ?? false === $p['hidden']);
83-
84-
return $propertyMetadata;
8579
}
8680

8781
foreach ($this->modelMetadata->getRelations($model) as $relation) {
@@ -95,13 +89,12 @@ public function create(string $resourceClass, string $property, array $options =
9589
}
9690

9791
$type = new Type($collection ? Type::BUILTIN_TYPE_ITERABLE : Type::BUILTIN_TYPE_OBJECT, false, $relation['related'], $collection, collectionValueType: new Type(Type::BUILTIN_TYPE_OBJECT, false, $relation['related']));
98-
$propertyMetadata = $propertyMetadata
92+
93+
return $propertyMetadata
9994
->withBuiltinTypes([$type])
10095
->withWritable($propertyMetadata->isWritable() ?? true)
10196
->withReadable($propertyMetadata->isReadable() ?? true)
10297
->withExtraProperties(['eloquent_relation' => $relation] + $propertyMetadata->getExtraProperties());
103-
104-
return $propertyMetadata;
10598
}
10699

107100
return $propertyMetadata;

0 commit comments

Comments
 (0)