12
12
namespace Symfony \Bridge \Doctrine \Form ;
13
13
14
14
use Doctrine \DBAL \Types \Type ;
15
+ use Doctrine \DBAL \Types \Types ;
15
16
use Doctrine \ORM \Mapping \ClassMetadataInfo ;
16
17
use Doctrine \ORM \Mapping \MappingException as LegacyMappingException ;
17
18
use Doctrine \Persistence \ManagerRegistry ;
@@ -28,9 +29,15 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
28
29
29
30
private $ cache = [];
30
31
32
+ private static $ useDeprecatedConstants ;
33
+
31
34
public function __construct (ManagerRegistry $ registry )
32
35
{
33
36
$ this ->registry = $ registry ;
37
+
38
+ if (null === self ::$ useDeprecatedConstants ) {
39
+ self ::$ useDeprecatedConstants = !class_exists (Types::class);
40
+ }
34
41
}
35
42
36
43
/**
@@ -52,39 +59,39 @@ public function guessType($class, $property)
52
59
}
53
60
54
61
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 :
57
64
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 :
59
66
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 :
62
69
case 'vardatetime ' :
63
70
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateTimeType ' , [], Guess::HIGH_CONFIDENCE );
64
71
case 'datetime_immutable ' :
65
72
case 'datetimetz_immutable ' :
66
73
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateTimeType ' , ['input ' => 'datetime_immutable ' ], Guess::HIGH_CONFIDENCE );
67
74
case 'dateinterval ' :
68
75
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 :
70
77
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\DateType ' , [], Guess::HIGH_CONFIDENCE );
71
78
case 'date_immutable ' :
72
79
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 :
74
81
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TimeType ' , [], Guess::HIGH_CONFIDENCE );
75
82
case 'time_immutable ' :
76
83
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 :
78
85
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 :
80
87
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 :
84
91
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 :
86
93
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 :
88
95
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextareaType ' , [], Guess::MEDIUM_CONFIDENCE );
89
96
default :
90
97
return new TypeGuess ('Symfony\Component\Form\Extension\Core\Type\TextType ' , [], Guess::LOW_CONFIDENCE );
@@ -107,7 +114,7 @@ public function guessRequired($class, $property)
107
114
108
115
// Check whether the field exists and is nullable or not
109
116
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 )) {
111
118
return new ValueGuess (true , Guess::HIGH_CONFIDENCE );
112
119
}
113
120
@@ -144,7 +151,7 @@ public function guessMaxLength($class, $property)
144
151
return new ValueGuess ($ mapping ['length ' ], Guess::HIGH_CONFIDENCE );
145
152
}
146
153
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 ])) {
148
155
return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
149
156
}
150
157
}
@@ -159,7 +166,7 @@ public function guessPattern($class, $property)
159
166
{
160
167
$ ret = $ this ->getMetadata ($ class );
161
168
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 ])) {
163
170
return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
164
171
}
165
172
}
0 commit comments