Skip to content

Commit f44914f

Browse files
committed
feat(laravel): use TypeInfo's Type
1 parent 6e47055 commit f44914f

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
use Illuminate\Database\Eloquent\Relations\MorphMany;
2525
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2626
use Illuminate\Support\Collection;
27-
use Symfony\Component\PropertyInfo\Type;
27+
use Symfony\Component\TypeInfo\Type;
28+
use Symfony\Component\TypeInfo\TypeIdentifier;
2829

2930
/**
3031
* Uses Eloquent metadata to populate the identifier property.
@@ -75,19 +76,23 @@ public function create(string $resourceClass, string $property, array $options =
7576
// see https://laravel.com/docs/11.x/eloquent-mutators#attribute-casting
7677
$builtinType = $p['cast'] ?? $p['type'];
7778
$type = match ($builtinType) {
78-
'integer' => new Type(Type::BUILTIN_TYPE_INT, $p['nullable']),
79-
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']),
80-
'boolean', 'bool' => new Type(Type::BUILTIN_TYPE_BOOL, $p['nullable']),
81-
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
82-
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
83-
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),
84-
'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']),
85-
'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']),
86-
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable'] ?? true),
79+
'integer' => Type::int(),
80+
'double', 'real' => Type::float(),
81+
'boolean', 'bool' => Type::bool(),
82+
'datetime', 'date', 'timestamp' => Type::object(\DateTime::class),
83+
'immutable_datetime', 'immutable_date' => Type::object(\DateTimeImmutable::class),
84+
'collection', 'encrypted:collection' => Type::collection(Type::object(Collection::class)),
85+
'encrypted:array' => Type::builtin(TypeIdentifier::ARRAY),
86+
'encrypted:object' => Type::object(),
87+
default => \in_array($builtinType, TypeIdentifier::values(), true) ? Type::builtin($builtinType) : Type::string(),
8788
};
8889

90+
if ($p['nullable']) {
91+
$type = Type::nullable($type);
92+
}
93+
8994
return $propertyMetadata
90-
->withBuiltinTypes([$type])
95+
->withPhpType($type)
9196
->withWritable($propertyMetadata->isWritable() ?? true === $p['fillable'])
9297
->withReadable($propertyMetadata->isReadable() ?? false === $p['hidden']);
9398
}
@@ -106,10 +111,13 @@ public function create(string $resourceClass, string $property, array $options =
106111
default => false,
107112
};
108113

109-
$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']));
114+
$type = Type::object($relation['related']);
115+
if ($collection) {
116+
$type = Type::iterable($type);
117+
}
110118

111119
return $propertyMetadata
112-
->withBuiltinTypes([$type])
120+
->withPhpType($type)
113121
->withWritable($propertyMetadata->isWritable() ?? true)
114122
->withReadable($propertyMetadata->isReadable() ?? true)
115123
->withExtraProperties(['eloquent_relation' => $relation] + $propertyMetadata->getExtraProperties());

src/Laravel/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"illuminate/routing": "^11.0",
4848
"illuminate/support": "^11.0",
4949
"illuminate/container": "^11.0",
50+
"symfony/type-info": "^7.2",
5051
"symfony/web-link": "^6.4 || ^7.1",
5152
"willdurand/negotiation": "^3.1",
5253
"phpstan/phpdoc-parser": "^1.29 || ^2.0",

0 commit comments

Comments
 (0)