24
24
use Illuminate \Database \Eloquent \Relations \MorphMany ;
25
25
use Illuminate \Database \Eloquent \Relations \MorphToMany ;
26
26
use Illuminate \Support \Collection ;
27
- use Symfony \Component \PropertyInfo \Type ;
27
+ use Symfony \Component \TypeInfo \Type ;
28
+ use Symfony \Component \TypeInfo \TypeIdentifier ;
28
29
29
30
/**
30
31
* Uses Eloquent metadata to populate the identifier property.
@@ -75,19 +76,23 @@ public function create(string $resourceClass, string $property, array $options =
75
76
// see https://laravel.com/docs/11.x/eloquent-mutators#attribute-casting
76
77
$ builtinType = $ p ['cast ' ] ?? $ p ['type ' ];
77
78
$ 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 ( ),
87
88
};
88
89
90
+ if ($ p ['nullable ' ]) {
91
+ $ type = Type::nullable ($ type );
92
+ }
93
+
89
94
return $ propertyMetadata
90
- ->withBuiltinTypes ([ $ type] )
95
+ ->withPhpType ( $ type )
91
96
->withWritable ($ propertyMetadata ->isWritable () ?? true === $ p ['fillable ' ])
92
97
->withReadable ($ propertyMetadata ->isReadable () ?? false === $ p ['hidden ' ]);
93
98
}
@@ -106,10 +111,13 @@ public function create(string $resourceClass, string $property, array $options =
106
111
default => false ,
107
112
};
108
113
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
+ }
110
118
111
119
return $ propertyMetadata
112
- ->withBuiltinTypes ([ $ type] )
120
+ ->withPhpType ( $ type )
113
121
->withWritable ($ propertyMetadata ->isWritable () ?? true )
114
122
->withReadable ($ propertyMetadata ->isReadable () ?? true )
115
123
->withExtraProperties (['eloquent_relation ' => $ relation ] + $ propertyMetadata ->getExtraProperties ());
0 commit comments