1212namespace Symfony \Bridge \Doctrine \Form ;
1313
1414use Doctrine \DBAL \Types \Type ;
15+ use Doctrine \DBAL \Types \Types ;
1516use Doctrine \ORM \Mapping \ClassMetadataInfo ;
1617use Doctrine \ORM \Mapping \MappingException as LegacyMappingException ;
1718use Doctrine \Persistence \ManagerRegistry ;
@@ -28,9 +29,15 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
2829
2930 private $ cache = [];
3031
32+ private static $ useDeprecatedConstants ;
33+
3134 public function __construct (ManagerRegistry $ registry )
3235 {
3336 $ this ->registry = $ registry ;
37+
38+ if (null === self ::$ useDeprecatedConstants ) {
39+ self ::$ useDeprecatedConstants = !class_exists (Types::class);
40+ }
3441 }
3542
3643 /**
@@ -52,39 +59,39 @@ public function guessType($class, $property)
5259 }
5360
5461 switch ($ metadata ->getTypeOfField ($ property )) {
55- case Type::TARRAY :
56- case Type::SIMPLE_ARRAY :
62+ case self :: $ useDeprecatedConstants ? Type::TARRAY : Types:: ARRAY :
63+ case self :: $ useDeprecatedConstants ? Type:: SIMPLE_ARRAY : Types ::SIMPLE_ARRAY :
5764 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\CollectionType ' , [], Guess::MEDIUM_CONFIDENCE );
58- case Type::BOOLEAN :
65+ case self :: $ useDeprecatedConstants ? Type:: BOOLEAN : Types ::BOOLEAN :
5966 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\CheckboxType ' , [], Guess::HIGH_CONFIDENCE );
60- case Type::DATETIME :
61- case Type::DATETIMETZ :
67+ case self :: $ useDeprecatedConstants ? Type::DATETIME : Types:: DATETIME_MUTABLE :
68+ case self :: $ useDeprecatedConstants ? Type::DATETIMETZ : Types:: DATETIMETZ_MUTABLE :
6269 case 'vardatetime ' :
6370 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateTimeType ' , [], Guess::HIGH_CONFIDENCE );
6471 case 'datetime_immutable ' :
6572 case 'datetimetz_immutable ' :
6673 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateTimeType ' , ['input ' => 'datetime_immutable ' ], Guess::HIGH_CONFIDENCE );
6774 case 'dateinterval ' :
6875 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateIntervalType ' , [], Guess::HIGH_CONFIDENCE );
69- case Type::DATE :
76+ case self :: $ useDeprecatedConstants ? Type::DATE : Types:: DATE_MUTABLE :
7077 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateType ' , [], Guess::HIGH_CONFIDENCE );
7178 case 'date_immutable ' :
7279 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateType ' , ['input ' => 'datetime_immutable ' ], Guess::HIGH_CONFIDENCE );
73- case Type::TIME :
80+ case self :: $ useDeprecatedConstants ? Type::TIME : Types:: TIME_MUTABLE :
7481 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TimeType ' , [], Guess::HIGH_CONFIDENCE );
7582 case 'time_immutable ' :
7683 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TimeType ' , ['input ' => 'datetime_immutable ' ], Guess::HIGH_CONFIDENCE );
77- case Type::DECIMAL :
84+ case self :: $ useDeprecatedConstants ? Type:: DECIMAL : Types ::DECIMAL :
7885 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\NumberType ' , ['input ' => 'string ' ], Guess::MEDIUM_CONFIDENCE );
79- case Type::FLOAT :
86+ case self :: $ useDeprecatedConstants ? Type:: FLOAT : Types ::FLOAT :
8087 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\NumberType ' , [], Guess::MEDIUM_CONFIDENCE );
81- case Type::INTEGER :
82- case Type::BIGINT :
83- case Type::SMALLINT :
88+ case self :: $ useDeprecatedConstants ? Type:: INTEGER : Types ::INTEGER :
89+ case self :: $ useDeprecatedConstants ? Type:: BIGINT : Types ::BIGINT :
90+ case self :: $ useDeprecatedConstants ? Type:: SMALLINT : Types ::SMALLINT :
8491 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\IntegerType ' , [], Guess::MEDIUM_CONFIDENCE );
85- case Type::STRING :
92+ case self :: $ useDeprecatedConstants ? Type:: STRING : Types ::STRING :
8693 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextType ' , [], Guess::MEDIUM_CONFIDENCE );
87- case Type::TEXT :
94+ case self :: $ useDeprecatedConstants ? Type:: TEXT : Types ::TEXT :
8895 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextareaType ' , [], Guess::MEDIUM_CONFIDENCE );
8996 default :
9097 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextType ' , [], Guess::LOW_CONFIDENCE );
@@ -107,7 +114,7 @@ public function guessRequired($class, $property)
107114
108115 // Check whether the field exists and is nullable or not
109116 if (isset ($ classMetadata ->fieldMappings [$ property ])) {
110- if (!$ classMetadata ->isNullable ($ property ) && Type::BOOLEAN !== $ classMetadata ->getTypeOfField ($ property )) {
117+ if (!$ classMetadata ->isNullable ($ property ) && ( self :: $ useDeprecatedConstants ? Type::BOOLEAN : Types:: BOOLEAN ) !== $ classMetadata ->getTypeOfField ($ property )) {
111118 return new ValueGuess (true , Guess::HIGH_CONFIDENCE );
112119 }
113120
@@ -144,7 +151,7 @@ public function guessMaxLength($class, $property)
144151 return new ValueGuess ($ mapping ['length ' ], Guess::HIGH_CONFIDENCE );
145152 }
146153
147- if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), [Type::DECIMAL , Type::FLOAT ])) {
154+ if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), self :: $ useDeprecatedConstants ? [Type::DECIMAL , Type:: FLOAT ] : [Types:: DECIMAL , Types ::FLOAT ])) {
148155 return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
149156 }
150157 }
@@ -159,7 +166,7 @@ public function guessPattern($class, $property)
159166 {
160167 $ ret = $ this ->getMetadata ($ class );
161168 if ($ ret && isset ($ ret [0 ]->fieldMappings [$ property ]) && !$ ret [0 ]->hasAssociation ($ property )) {
162- if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), [Type::DECIMAL , Type::FLOAT ])) {
169+ if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), self :: $ useDeprecatedConstants ? [Type::DECIMAL , Type:: FLOAT ] : [Types:: DECIMAL , Types ::FLOAT ])) {
163170 return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
164171 }
165172 }
0 commit comments