From 6c002e3c47d9f69e57355f22b2910c00b59e9c67 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Mon, 24 Feb 2025 14:00:47 +0100 Subject: [PATCH] feat(laravel): use `TypeInfo`'s `Type` --- .../EloquentPropertyMetadataFactory.php | 34 ++++++++++++------- src/Laravel/composer.json | 4 +-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php index f29a4556416..70d0aaebb25 100644 --- a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php +++ b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php @@ -24,7 +24,8 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Support\Collection; -use Symfony\Component\PropertyInfo\Type; +use Symfony\Component\TypeInfo\Type; +use Symfony\Component\TypeInfo\TypeIdentifier; /** * Uses Eloquent metadata to populate the identifier property. @@ -75,19 +76,23 @@ public function create(string $resourceClass, string $property, array $options = // see https://laravel.com/docs/11.x/eloquent-mutators#attribute-casting $builtinType = $p['cast'] ?? $p['type']; $type = match ($builtinType) { - 'integer' => new Type(Type::BUILTIN_TYPE_INT, $p['nullable']), - 'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']), - 'boolean', 'bool' => new Type(Type::BUILTIN_TYPE_BOOL, $p['nullable']), - 'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class), - 'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class), - 'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true), - 'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']), - 'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']), - default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable'] ?? true), + 'integer' => Type::int(), + 'double', 'real' => Type::float(), + 'boolean', 'bool' => Type::bool(), + 'datetime', 'date', 'timestamp' => Type::object(\DateTime::class), + 'immutable_datetime', 'immutable_date' => Type::object(\DateTimeImmutable::class), + 'collection', 'encrypted:collection' => Type::collection(Type::object(Collection::class)), + 'encrypted:array' => Type::builtin(TypeIdentifier::ARRAY), + 'encrypted:object' => Type::object(), + default => \in_array($builtinType, TypeIdentifier::values(), true) ? Type::builtin($builtinType) : Type::string(), }; + if ($p['nullable']) { + $type = Type::nullable($type); + } + return $propertyMetadata - ->withBuiltinTypes([$type]) + ->withNativeType($type) ->withWritable($propertyMetadata->isWritable() ?? true === $p['fillable']) ->withReadable($propertyMetadata->isReadable() ?? false === $p['hidden']); } @@ -106,10 +111,13 @@ public function create(string $resourceClass, string $property, array $options = default => false, }; - $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'])); + $type = Type::object($relation['related']); + if ($collection) { + $type = Type::iterable($type); + } return $propertyMetadata - ->withBuiltinTypes([$type]) + ->withNativeType($type) ->withWritable($propertyMetadata->isWritable() ?? true) ->withReadable($propertyMetadata->isReadable() ?? true) ->withExtraProperties(['eloquent_relation' => $relation] + $propertyMetadata->getExtraProperties()); diff --git a/src/Laravel/composer.json b/src/Laravel/composer.json index 5769c797d54..76fe828eb20 100644 --- a/src/Laravel/composer.json +++ b/src/Laravel/composer.json @@ -47,6 +47,7 @@ "illuminate/routing": "^11.0 || ^12.0", "illuminate/support": "^11.0 || ^12.0", "illuminate/container": "^11.0 || ^12.0", + "symfony/type-info": "^7.3-dev", "symfony/web-link": "^6.4 || ^7.1", "willdurand/negotiation": "^3.1", "phpstan/phpdoc-parser": "^1.29 || ^2.0", @@ -58,8 +59,7 @@ "orchestra/testbench": "^9.1", "phpunit/phpunit": "^11.2", "api-platform/graphql": "^4.1", - "laravel/sanctum": "^4.0", - "symfony/type-info": "^7.3-dev" + "laravel/sanctum": "^4.0" }, "autoload": { "psr-4": {