1313
1414namespace ApiPlatform \Metadata ;
1515
16- use Symfony \Component \PropertyInfo \Type ;
16+ use ApiPlatform \Metadata \Util \PropertyInfoToTypeInfoHelper ;
17+ use Symfony \Component \PropertyInfo \Type as LegacyType ;
1718use Symfony \Component \Serializer \Attribute \Context ;
1819use Symfony \Component \Serializer \Attribute \Groups ;
1920use Symfony \Component \Serializer \Attribute \Ignore ;
2021use Symfony \Component \Serializer \Attribute \MaxDepth ;
2122use Symfony \Component \Serializer \Attribute \SerializedName ;
2223use Symfony \Component \Serializer \Attribute \SerializedPath ;
24+ use Symfony \Component \TypeInfo \Type ;
2325
2426/**
2527 * ApiProperty annotation.
@@ -31,6 +33,15 @@ final class ApiProperty
3133{
3234 private ?array $ types ;
3335 private ?array $ serialize ;
36+ private ?Type $ phpType ;
37+
38+ /**
39+ * Used to know if only legacy types are defined without triggering deprecation.
40+ * To be removed in 5.0.
41+ *
42+ * @internal
43+ */
44+ public bool $ usesLegacyType = false ;
3445
3546 /**
3647 * @param bool|null $readableLink https://api-platform.com/docs/core/serialization/#force-iri-with-relations-of-the-same-type-parentchilds-relations
@@ -47,10 +58,11 @@ final class ApiProperty
4758 * @param string|\Stringable|null $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
4859 * @param string[] $types the RDF types of this property
4960 * @param string[] $iris
50- * @param Type[] $builtinTypes
61+ * @param LegacyType[] $builtinTypes
5162 * @param string|null $uriTemplate (experimental) whether to return the subRessource collection IRI instead of an iterable of IRI
5263 * @param string|null $property The property name
5364 * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize Serializer attributes
65+ * @param Type $phpType The internal PHP type
5466 */
5567 public function __construct (
5668 private ?string $ description = null ,
@@ -206,6 +218,8 @@ public function __construct(
206218 array |string |null $ types = null ,
207219 /*
208220 * The related php types.
221+ *
222+ * deprecated since 4.1, use "phpType" instead.
209223 */
210224 private ?array $ builtinTypes = null ,
211225 private ?array $ schema = null ,
@@ -221,9 +235,23 @@ public function __construct(
221235 */
222236 private ?bool $ hydra = null ,
223237 private array $ extraProperties = [],
238+ ?Type $ phpType = null ,
224239 ) {
225240 $ this ->types = \is_string ($ types ) ? (array ) $ types : $ types ;
226241 $ this ->serialize = \is_array ($ serialize ) ? $ serialize : [$ serialize ];
242+ $ this ->phpType = $ phpType ;
243+
244+ if ($ this ->builtinTypes ) {
245+ // trigger_deprecation('api_platform/metadata', '4.1', 'The "builtinTypes" argument of "%s()" is deprecated, use "phpType" instead.');
246+
247+ $ this ->usesLegacyType = true ;
248+
249+ if (!$ this ->phpType ) {
250+ $ this ->phpType = PropertyInfoToTypeInfoHelper::convertLegacyTypesToType ($ this ->builtinTypes );
251+ }
252+ } elseif ($ this ->phpType ) {
253+ $ this ->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes ($ this ->phpType );
254+ }
227255 }
228256
229257 public function getProperty (): ?string
@@ -490,20 +518,43 @@ public function withTypes(array|string $types = []): static
490518 }
491519
492520 /**
493- * @return Type[]
521+ * deprecated since 4.1, use "getPhpType" instead.
522+ *
523+ * @return LegacyType[]
494524 */
495525 public function getBuiltinTypes (): ?array
496526 {
527+ // trigger_deprecation('api-platform/metadata', '4.1', 'The "%s()" method is deprecated, use "%s::getPhpType()" instead.', __METHOD__, self::class);
528+
497529 return $ this ->builtinTypes ;
498530 }
499531
500532 /**
501- * @param Type[] $builtinTypes
533+ * deprecated since 4.1, use "withPhpType" instead.
534+ *
535+ * @param LegacyType[] $builtinTypes
502536 */
503537 public function withBuiltinTypes (array $ builtinTypes = []): static
504538 {
539+ // trigger_deprecation('api-platform/metadata', '4.1', 'The "%s()" method is deprecated, use "%s::withPhpType()" instead.', __METHOD__, self::class);
540+
505541 $ self = clone $ this ;
506542 $ self ->builtinTypes = $ builtinTypes ;
543+ $ self ->phpType = PropertyInfoToTypeInfoHelper::convertLegacyTypesToType ($ builtinTypes );
544+
545+ return $ self ;
546+ }
547+
548+ public function getPhpType (): ?Type
549+ {
550+ return $ this ->phpType ;
551+ }
552+
553+ public function withPhpType (?Type $ phpType ): self
554+ {
555+ $ self = clone $ this ;
556+ $ self ->phpType = $ phpType ;
557+ $ self ->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes ($ phpType );
507558
508559 return $ self ;
509560 }
0 commit comments