13
13
14
14
namespace ApiPlatform \Doctrine \Odm \PropertyInfo ;
15
15
16
+ use ApiPlatform \Metadata \Util \PropertyInfoToTypeInfoHelper ;
16
17
use Doctrine \Common \Collections \Collection ;
17
18
use Doctrine \ODM \MongoDB \Mapping \ClassMetadata as MongoDbClassMetadata ;
18
19
use Doctrine \ODM \MongoDB \Types \Type as MongoDbType ;
22
23
use Symfony \Component \PropertyInfo \PropertyAccessExtractorInterface ;
23
24
use Symfony \Component \PropertyInfo \PropertyListExtractorInterface ;
24
25
use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
25
- use Symfony \Component \PropertyInfo \Type ;
26
+ use Symfony \Component \PropertyInfo \Type as LegacyType ;
27
+ use Symfony \Component \TypeInfo \Type ;
26
28
27
29
/**
28
30
* Extracts data using Doctrine MongoDB ODM metadata.
@@ -53,9 +55,9 @@ public function getProperties($class, array $context = []): ?array
53
55
/**
54
56
* {@inheritdoc}
55
57
*
56
- * @return Type []|null
58
+ * @return LegacyType []|null
57
59
*/
58
- public function getTypes ($ class , $ property , array $ context = []): ?array
60
+ public function getTypes (string $ class , string $ property , array $ context = []): ?array
59
61
{
60
62
if (null === $ metadata = $ this ->getMetadata ($ class )) {
61
63
return null ;
@@ -72,19 +74,19 @@ public function getTypes($class, $property, array $context = []): ?array
72
74
if ($ metadata ->isSingleValuedAssociation ($ property )) {
73
75
$ nullable = $ metadata instanceof MongoDbClassMetadata && $ metadata ->isNullable ($ property );
74
76
75
- return [new Type (Type ::BUILTIN_TYPE_OBJECT , $ nullable , $ class )];
77
+ return [new LegacyType (LegacyType ::BUILTIN_TYPE_OBJECT , $ nullable , $ class )];
76
78
}
77
79
78
- $ collectionKeyType = Type ::BUILTIN_TYPE_INT ;
80
+ $ collectionKeyType = LegacyType ::BUILTIN_TYPE_INT ;
79
81
80
82
return [
81
- new Type (
82
- Type ::BUILTIN_TYPE_OBJECT ,
83
+ new LegacyType (
84
+ LegacyType ::BUILTIN_TYPE_OBJECT ,
83
85
false ,
84
86
Collection::class,
85
87
true ,
86
- new Type ($ collectionKeyType ),
87
- new Type (Type ::BUILTIN_TYPE_OBJECT , false , $ class )
88
+ new LegacyType ($ collectionKeyType ),
89
+ new LegacyType (LegacyType ::BUILTIN_TYPE_OBJECT , false , $ class )
88
90
),
89
91
];
90
92
}
@@ -94,18 +96,18 @@ public function getTypes($class, $property, array $context = []): ?array
94
96
$ nullable = $ metadata instanceof MongoDbClassMetadata && $ metadata ->isNullable ($ property );
95
97
$ enumType = null ;
96
98
if (null !== $ enumClass = $ metadata instanceof MongoDbClassMetadata ? $ metadata ->getFieldMapping ($ property )['enumType ' ] ?? null : null ) {
97
- $ enumType = new Type (Type ::BUILTIN_TYPE_OBJECT , $ nullable , $ enumClass );
99
+ $ enumType = new LegacyType (LegacyType ::BUILTIN_TYPE_OBJECT , $ nullable , $ enumClass );
98
100
}
99
101
100
102
switch ($ typeOfField ) {
101
103
case MongoDbType::DATE :
102
- return [new Type (Type ::BUILTIN_TYPE_OBJECT , $ nullable , \DateTime::class)];
104
+ return [new LegacyType (LegacyType ::BUILTIN_TYPE_OBJECT , $ nullable , \DateTime::class)];
103
105
case MongoDbType::DATE_IMMUTABLE :
104
- return [new Type (Type ::BUILTIN_TYPE_OBJECT , $ nullable , \DateTimeImmutable::class)];
106
+ return [new LegacyType (LegacyType ::BUILTIN_TYPE_OBJECT , $ nullable , \DateTimeImmutable::class)];
105
107
case MongoDbType::HASH :
106
- return [new Type (Type ::BUILTIN_TYPE_ARRAY , $ nullable , null , true )];
108
+ return [new LegacyType (LegacyType ::BUILTIN_TYPE_ARRAY , $ nullable , null , true )];
107
109
case MongoDbType::COLLECTION :
108
- return [new Type (Type ::BUILTIN_TYPE_ARRAY , $ nullable , null , true , new Type (Type ::BUILTIN_TYPE_INT ))];
110
+ return [new LegacyType (LegacyType ::BUILTIN_TYPE_ARRAY , $ nullable , null , true , new LegacyType (LegacyType ::BUILTIN_TYPE_INT ))];
109
111
case MongoDbType::INT :
110
112
case MongoDbType::STRING :
111
113
if ($ enumType ) {
@@ -115,7 +117,7 @@ public function getTypes($class, $property, array $context = []): ?array
115
117
116
118
$ builtinType = $ this ->getPhpType ($ typeOfField );
117
119
118
- return $ builtinType ? [new Type ($ builtinType , $ nullable )] : null ;
120
+ return $ builtinType ? [new LegacyType ($ builtinType , $ nullable )] : null ;
119
121
}
120
122
121
123
return null ;
@@ -154,16 +156,21 @@ private function getMetadata(string $class): ?ClassMetadata
154
156
}
155
157
}
156
158
159
+ public function getType (string $ class , string $ property , array $ context = []): ?Type
160
+ {
161
+ return PropertyInfoToTypeInfoHelper::convertLegacyTypesToType ($ this ->getTypes ($ class , $ property , $ context ));
162
+ }
163
+
157
164
/**
158
165
* Gets the corresponding built-in PHP type.
159
166
*/
160
167
private function getPhpType (string $ doctrineType ): ?string
161
168
{
162
169
return match ($ doctrineType ) {
163
- MongoDbType::INTEGER , MongoDbType::INT , MongoDbType::INTID , MongoDbType::KEY => Type ::BUILTIN_TYPE_INT ,
164
- MongoDbType::FLOAT => Type ::BUILTIN_TYPE_FLOAT ,
165
- MongoDbType::STRING , MongoDbType::ID , MongoDbType::OBJECTID , MongoDbType::TIMESTAMP , MongoDbType::BINDATA , MongoDbType::BINDATABYTEARRAY , MongoDbType::BINDATACUSTOM , MongoDbType::BINDATAFUNC , MongoDbType::BINDATAMD5 , MongoDbType::BINDATAUUID , MongoDbType::BINDATAUUIDRFC4122 => Type ::BUILTIN_TYPE_STRING ,
166
- MongoDbType::BOOLEAN , MongoDbType::BOOL => Type ::BUILTIN_TYPE_BOOL ,
170
+ MongoDbType::INTEGER , MongoDbType::INT , MongoDbType::INTID , MongoDbType::KEY => LegacyType ::BUILTIN_TYPE_INT ,
171
+ MongoDbType::FLOAT => LegacyType ::BUILTIN_TYPE_FLOAT ,
172
+ MongoDbType::STRING , MongoDbType::ID , MongoDbType::OBJECTID , MongoDbType::TIMESTAMP , MongoDbType::BINDATA , MongoDbType::BINDATABYTEARRAY , MongoDbType::BINDATACUSTOM , MongoDbType::BINDATAFUNC , MongoDbType::BINDATAMD5 , MongoDbType::BINDATAUUID , MongoDbType::BINDATAUUIDRFC4122 => LegacyType ::BUILTIN_TYPE_STRING ,
173
+ MongoDbType::BOOLEAN , MongoDbType::BOOL => LegacyType ::BUILTIN_TYPE_BOOL ,
167
174
default => null ,
168
175
};
169
176
}
0 commit comments