1515use Doctrine \Common \Persistence \Mapping \MappingException as LegacyCommonMappingException ;
1616use Doctrine \Common \Util \ClassUtils ;
1717use Doctrine \DBAL \Types \Type ;
18+ use Doctrine \DBAL \Types \Types ;
1819use Doctrine \ORM \Mapping \ClassMetadataInfo ;
1920use Doctrine \ORM \Mapping \MappingException as LegacyMappingException ;
2021use Doctrine \Persistence \ManagerRegistry ;
@@ -30,12 +31,18 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
3031
3132 private $ cache = [];
3233
34+ private static $ useDeprecatedConstants ;
35+
3336 /**
3437 * @param ManagerRegistry|LegacyManagerRegistry $registry
3538 */
3639 public function __construct ($ registry )
3740 {
3841 $ this ->registry = $ registry ;
42+
43+ if (null === self ::$ useDeprecatedConstants ) {
44+ self ::$ useDeprecatedConstants = !class_exists (Types::class);
45+ }
3946 }
4047
4148 /**
@@ -57,30 +64,30 @@ public function guessType($class, $property)
5764 }
5865
5966 switch ($ metadata ->getTypeOfField ($ property )) {
60- case Type::TARRAY :
67+ case self :: $ useDeprecatedConstants ? Type::TARRAY : ' array ' :
6168 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\CollectionType ' , [], Guess::MEDIUM_CONFIDENCE );
62- case Type::BOOLEAN :
69+ case self :: $ useDeprecatedConstants ? Type:: BOOLEAN : Types ::BOOLEAN :
6370 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\CheckboxType ' , [], Guess::HIGH_CONFIDENCE );
64- case Type::DATETIME :
65- case Type::DATETIMETZ :
71+ case self :: $ useDeprecatedConstants ? Type::DATETIME : Types:: DATETIME_MUTABLE :
72+ case self :: $ useDeprecatedConstants ? Type::DATETIMETZ : Types:: DATETIMETZ_MUTABLE :
6673 case 'vardatetime ' :
6774 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateTimeType ' , [], Guess::HIGH_CONFIDENCE );
6875 case 'dateinterval ' :
6976 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateIntervalType ' , [], Guess::HIGH_CONFIDENCE );
70- case Type::DATE :
77+ case self :: $ useDeprecatedConstants ? Type::DATE : Types:: DATE_MUTABLE :
7178 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateType ' , [], Guess::HIGH_CONFIDENCE );
72- case Type::TIME :
79+ case self :: $ useDeprecatedConstants ? Type::TIME : Types:: TIME_MUTABLE :
7380 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TimeType ' , [], Guess::HIGH_CONFIDENCE );
74- case Type::DECIMAL :
75- case Type::FLOAT :
81+ case self :: $ useDeprecatedConstants ? Type:: DECIMAL : Types ::DECIMAL :
82+ case self :: $ useDeprecatedConstants ? Type:: FLOAT : Types ::FLOAT :
7683 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\NumberType ' , [], Guess::MEDIUM_CONFIDENCE );
77- case Type::INTEGER :
78- case Type::BIGINT :
79- case Type::SMALLINT :
84+ case self :: $ useDeprecatedConstants ? Type:: INTEGER : Types ::INTEGER :
85+ case self :: $ useDeprecatedConstants ? Type:: BIGINT : Types ::BIGINT :
86+ case self :: $ useDeprecatedConstants ? Type:: SMALLINT : Types ::SMALLINT :
8087 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\IntegerType ' , [], Guess::MEDIUM_CONFIDENCE );
81- case Type::STRING :
88+ case self :: $ useDeprecatedConstants ? Type:: STRING : Types ::STRING :
8289 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextType ' , [], Guess::MEDIUM_CONFIDENCE );
83- case Type::TEXT :
90+ case self :: $ useDeprecatedConstants ? Type:: TEXT : Types ::TEXT :
8491 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextareaType ' , [], Guess::MEDIUM_CONFIDENCE );
8592 default :
8693 return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextType ' , [], Guess::LOW_CONFIDENCE );
@@ -103,7 +110,7 @@ public function guessRequired($class, $property)
103110
104111 // Check whether the field exists and is nullable or not
105112 if (isset ($ classMetadata ->fieldMappings [$ property ])) {
106- if (!$ classMetadata ->isNullable ($ property ) && Type::BOOLEAN !== $ classMetadata ->getTypeOfField ($ property )) {
113+ if (!$ classMetadata ->isNullable ($ property ) && ( self :: $ useDeprecatedConstants ? Type::BOOLEAN : Types:: BOOLEAN ) !== $ classMetadata ->getTypeOfField ($ property )) {
107114 return new ValueGuess (true , Guess::HIGH_CONFIDENCE );
108115 }
109116
@@ -140,7 +147,7 @@ public function guessMaxLength($class, $property)
140147 return new ValueGuess ($ mapping ['length ' ], Guess::HIGH_CONFIDENCE );
141148 }
142149
143- if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), [Type::DECIMAL , Type::FLOAT ])) {
150+ if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), self :: $ useDeprecatedConstants ? [Type::DECIMAL , Type:: FLOAT ] : [Types:: DECIMAL , Types ::FLOAT ])) {
144151 return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
145152 }
146153 }
@@ -155,7 +162,7 @@ public function guessPattern($class, $property)
155162 {
156163 $ ret = $ this ->getMetadata ($ class );
157164 if ($ ret && isset ($ ret [0 ]->fieldMappings [$ property ]) && !$ ret [0 ]->hasAssociation ($ property )) {
158- if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), [Type::DECIMAL , Type::FLOAT ])) {
165+ if (\in_array ($ ret [0 ]->getTypeOfField ($ property ), self :: $ useDeprecatedConstants ? [Type::DECIMAL , Type:: FLOAT ] : [Types:: DECIMAL , Types ::FLOAT ])) {
159166 return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
160167 }
161168 }
0 commit comments