Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitddf3353

Browse files
committed
[4.4][DoctrineBridge] Use new Types::* constants and support new json type
1 parent1024f5f commitddf3353

File tree

5 files changed

+113
-71
lines changed

5 files changed

+113
-71
lines changed

‎src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bridge\Doctrine\Form;
1313

1414
useDoctrine\DBAL\Types\Type;
15+
useDoctrine\DBAL\Types\Types;
1516
useDoctrine\ORM\Mapping\ClassMetadataInfo;
1617
useDoctrine\ORM\Mapping\MappingExceptionasLegacyMappingException;
1718
useDoctrine\Persistence\ManagerRegistry;
@@ -28,9 +29,15 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
2829

2930
private$cache = [];
3031

32+
privatestatic$useDeprecatedConstants;
33+
3134
publicfunction__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+
caseself::$useDeprecatedConstants ?Type::TARRAY : Types::ARRAY:
63+
caseself::$useDeprecatedConstants ?Type::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
5764
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
58-
case Type::BOOLEAN:
65+
caseself::$useDeprecatedConstants ?Type::BOOLEAN : Types::BOOLEAN:
5966
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
60-
case Type::DATETIME:
61-
case Type::DATETIMETZ:
67+
caseself::$useDeprecatedConstants ?Type::DATETIME : Types::DATETIME_MUTABLE:
68+
caseself::$useDeprecatedConstants ?Type::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
6269
case'vardatetime':
6370
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
6471
case'datetime_immutable':
6572
case'datetimetz_immutable':
6673
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' =>'datetime_immutable'], Guess::HIGH_CONFIDENCE);
6774
case'dateinterval':
6875
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
69-
case Type::DATE:
76+
caseself::$useDeprecatedConstants ?Type::DATE : Types::DATE_MUTABLE:
7077
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
7178
case'date_immutable':
7279
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' =>'datetime_immutable'], Guess::HIGH_CONFIDENCE);
73-
case Type::TIME:
80+
caseself::$useDeprecatedConstants ?Type::TIME : Types::TIME_MUTABLE:
7481
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
7582
case'time_immutable':
7683
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' =>'datetime_immutable'], Guess::HIGH_CONFIDENCE);
77-
case Type::DECIMAL:
84+
caseself::$useDeprecatedConstants ?Type::DECIMAL : Types::DECIMAL:
7885
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' =>'string'], Guess::MEDIUM_CONFIDENCE);
79-
case Type::FLOAT:
86+
caseself::$useDeprecatedConstants ?Type::FLOAT : Types::FLOAT:
8087
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
81-
case Type::INTEGER:
82-
case Type::BIGINT:
83-
case Type::SMALLINT:
88+
caseself::$useDeprecatedConstants ?Type::INTEGER : Types::INTEGER:
89+
caseself::$useDeprecatedConstants ?Type::BIGINT : Types::BIGINT:
90+
caseself::$useDeprecatedConstants ?Type::SMALLINT : Types::SMALLINT:
8491
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
85-
case Type::STRING:
92+
caseself::$useDeprecatedConstants ?Type::STRING : Types::STRING:
8693
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
87-
case Type::TEXT:
94+
caseself::$useDeprecatedConstants ?Type::TEXT : Types::TEXT:
8895
returnnewTypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
8996
default:
9097
returnnewTypeGuess('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
returnnewValueGuess(true, Guess::HIGH_CONFIDENCE);
112119
}
113120

@@ -144,7 +151,7 @@ public function guessMaxLength($class, $property)
144151
returnnewValueGuess($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
returnnewValueGuess(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
returnnewValueGuess(null, Guess::MEDIUM_CONFIDENCE);
164171
}
165172
}

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bridge\Doctrine\PropertyInfo;
1313

1414
useDoctrine\DBAL\Types\TypeasDBALType;
15+
useDoctrine\DBAL\Types\Types;
1516
useDoctrine\ORM\EntityManagerInterface;
1617
useDoctrine\ORM\Mapping\ClassMetadata;
1718
useDoctrine\ORM\Mapping\ClassMetadataInfo;
@@ -33,6 +34,8 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
3334
private$entityManager;
3435
private$classMetadataFactory;
3536

37+
privatestatic$useDeprecatedConstants;
38+
3639
/**
3740
* @param EntityManagerInterface $entityManager
3841
*/
@@ -46,6 +49,10 @@ public function __construct($entityManager)
4649
}else {
4750
thrownew \TypeError(sprintf('$entityManager must be an instance of "%s", "%s" given.', EntityManagerInterface::class,\is_object($entityManager) ?\get_class($entityManager) :\gettype($entityManager)));
4851
}
52+
53+
if (null ===self::$useDeprecatedConstants) {
54+
self::$useDeprecatedConstants = !class_exists(Types::class);
55+
}
4956
}
5057

5158
/**
@@ -146,11 +153,11 @@ public function getTypes($class, $property, array $context = [])
146153
switch ($builtinType) {
147154
case Type::BUILTIN_TYPE_OBJECT:
148155
switch ($typeOfField) {
149-
case DBALType::DATE:
150-
case DBALType::DATETIME:
151-
case DBALType::DATETIMETZ:
156+
caseself::$useDeprecatedConstants ?DBALType::DATE : Types::DATE_MUTABLE:
157+
caseself::$useDeprecatedConstants ?DBALType::DATETIME : Types::DATETIME_MUTABLE:
158+
caseself::$useDeprecatedConstants ?DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
152159
case'vardatetime':
153-
case DBALType::TIME:
160+
caseself::$useDeprecatedConstants ?DBALType::TIME : Types::TIME_MUTABLE:
154161
return [newType(Type::BUILTIN_TYPE_OBJECT,$nullable,'DateTime')];
155162

156163
case'date_immutable':
@@ -166,11 +173,12 @@ public function getTypes($class, $property, array $context = [])
166173
break;
167174
case Type::BUILTIN_TYPE_ARRAY:
168175
switch ($typeOfField) {
169-
case DBALType::TARRAY:
170-
case DBALType::JSON_ARRAY:
176+
caseself::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY:
177+
case'json_array':
178+
caseself::$useDeprecatedConstants ?false : Types::JSON:
171179
return [newType(Type::BUILTIN_TYPE_ARRAY,$nullable,null,true)];
172180

173-
case DBALType::SIMPLE_ARRAY:
181+
caseself::$useDeprecatedConstants ?DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
174182
return [newType(Type::BUILTIN_TYPE_ARRAY,$nullable,null,true,newType(Type::BUILTIN_TYPE_INT),newType(Type::BUILTIN_TYPE_STRING))];
175183
}
176184
}
@@ -245,43 +253,44 @@ private function isAssociationNullable(array $associationMapping): bool
245253
privatefunctiongetPhpType(string$doctrineType): ?string
246254
{
247255
switch ($doctrineType) {
248-
case DBALType::SMALLINT:
249-
case DBALType::INTEGER:
256+
caseself::$useDeprecatedConstants ?DBALType::SMALLINT : Types::SMALLINT:
257+
caseself::$useDeprecatedConstants ?DBALType::INTEGER : Types::INTEGER:
250258
return Type::BUILTIN_TYPE_INT;
251259

252-
case DBALType::FLOAT:
260+
caseself::$useDeprecatedConstants ?DBALType::FLOAT : Types::FLOAT:
253261
return Type::BUILTIN_TYPE_FLOAT;
254262

255-
case DBALType::BIGINT:
256-
case DBALType::STRING:
257-
case DBALType::TEXT:
258-
case DBALType::GUID:
259-
case DBALType::DECIMAL:
263+
caseself::$useDeprecatedConstants ?DBALType::BIGINT : Types::BIGINT:
264+
caseself::$useDeprecatedConstants ?DBALType::STRING : Types::STRING:
265+
caseself::$useDeprecatedConstants ?DBALType::TEXT : Types::TEXT:
266+
caseself::$useDeprecatedConstants ?DBALType::GUID : Types::GUID:
267+
caseself::$useDeprecatedConstants ?DBALType::DECIMAL : Types::DECIMAL:
260268
return Type::BUILTIN_TYPE_STRING;
261269

262-
case DBALType::BOOLEAN:
270+
caseself::$useDeprecatedConstants ?DBALType::BOOLEAN : Types::BOOLEAN:
263271
return Type::BUILTIN_TYPE_BOOL;
264272

265-
case DBALType::BLOB:
273+
caseself::$useDeprecatedConstants ?DBALType::BLOB : Types::BLOB:
266274
case'binary':
267275
return Type::BUILTIN_TYPE_RESOURCE;
268276

269-
case DBALType::OBJECT:
270-
case DBALType::DATE:
271-
case DBALType::DATETIME:
272-
case DBALType::DATETIMETZ:
277+
caseself::$useDeprecatedConstants ?DBALType::OBJECT : Types::OBJECT:
278+
caseself::$useDeprecatedConstants ?DBALType::DATE : Types::DATE_MUTABLE:
279+
caseself::$useDeprecatedConstants ?DBALType::DATETIME : Types::DATETIME_MUTABLE:
280+
caseself::$useDeprecatedConstants ?DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
273281
case'vardatetime':
274-
case DBALType::TIME:
282+
caseself::$useDeprecatedConstants ?DBALType::TIME : Types::TIME_MUTABLE:
275283
case'date_immutable':
276284
case'datetime_immutable':
277285
case'datetimetz_immutable':
278286
case'time_immutable':
279287
case'dateinterval':
280288
return Type::BUILTIN_TYPE_OBJECT;
281289

282-
case DBALType::TARRAY:
283-
case DBALType::SIMPLE_ARRAY:
284-
case DBALType::JSON_ARRAY:
290+
caseself::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY:
291+
caseself::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
292+
case'json_array':
293+
caseself::$useDeprecatedConstants ?false : Types::JSON:
285294
return Type::BUILTIN_TYPE_ARRAY;
286295
}
287296

‎src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespaceSymfony\Bridge\Doctrine\Security\RememberMe;
1313

1414
useDoctrine\DBAL\Connection;
15-
useDoctrine\DBAL\Types\TypeasDoctrineType;
15+
useDoctrine\DBAL\Types\Type;
16+
useDoctrine\DBAL\Types\Types;
1617
useSymfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
1718
useSymfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface;
1819
useSymfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface;
@@ -40,9 +41,15 @@ class DoctrineTokenProvider implements TokenProviderInterface
4041
{
4142
private$conn;
4243

44+
privatestatic$useDeprecatedConstants;
45+
4346
publicfunction__construct(Connection$conn)
4447
{
4548
$this->conn =$conn;
49+
50+
if (null ===self::$useDeprecatedConstants) {
51+
self::$useDeprecatedConstants = !class_exists(Types::class);
52+
}
4653
}
4754

4855
/**
@@ -90,7 +97,7 @@ public function updateToken($series, $tokenValue, \DateTime $lastUsed)
9097
];
9198
$paramTypes = [
9299
'value' => \PDO::PARAM_STR,
93-
'lastUsed' =>DoctrineType::DATETIME,
100+
'lastUsed' =>self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
94101
'series' => \PDO::PARAM_STR,
95102
];
96103
$updated =$this->conn->executeUpdate($sql,$paramValues,$paramTypes);
@@ -119,7 +126,7 @@ public function createNewToken(PersistentTokenInterface $token)
119126
'username' => \PDO::PARAM_STR,
120127
'series' => \PDO::PARAM_STR,
121128
'value' => \PDO::PARAM_STR,
122-
'lastUsed' =>DoctrineType::DATETIME,
129+
'lastUsed' =>self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
123130
];
124131
$this->conn->executeUpdate($sql,$paramValues,$paramTypes);
125132
}

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php‎

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
useDoctrine\Common\Collections\Collection;
1515
useDoctrine\DBAL\Types\TypeasDBALType;
16+
useDoctrine\DBAL\Types\Types;
1617
useDoctrine\ORM\EntityManager;
1718
useDoctrine\ORM\Tools\Setup;
1819
usePHPUnit\Framework\TestCase;
1920
useSymfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
21+
useSymfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
2022
useSymfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
2123
useSymfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
2224
useSymfony\Component\PropertyInfo\Type;
@@ -51,29 +53,40 @@ public function testLegacyGetProperties()
5153

5254
privatefunctiondoTestGetProperties(bool$legacy)
5355
{
56+
// Fields
57+
$expected = [
58+
'id',
59+
'guid',
60+
'time',
61+
'timeImmutable',
62+
'dateInterval',
63+
'jsonArray',
64+
'simpleArray',
65+
'float',
66+
'decimal',
67+
'bool',
68+
'binary',
69+
'customFoo',
70+
'bigint',
71+
];
72+
73+
if (class_exists(Types::class)) {
74+
$expected[] ='json';
75+
}
76+
77+
// Associations
78+
$expected =array_merge($expected, [
79+
'foo',
80+
'bar',
81+
'indexedBar',
82+
'indexedFoo',
83+
'indexedByDt',
84+
'indexedByCustomType',
85+
]);
86+
5487
$this->assertEquals(
55-
[
56-
'id',
57-
'guid',
58-
'time',
59-
'timeImmutable',
60-
'dateInterval',
61-
'json',
62-
'simpleArray',
63-
'float',
64-
'decimal',
65-
'bool',
66-
'binary',
67-
'customFoo',
68-
'bigint',
69-
'foo',
70-
'bar',
71-
'indexedBar',
72-
'indexedFoo',
73-
'indexedByDt',
74-
'indexedByCustomType',
75-
],
76-
$this->createExtractor($legacy)->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
88+
$expected,
89+
$this->createExtractor($legacy)->getProperties(!class_exists(Types::class) ?'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class)
7790
);
7891
}
7992

@@ -120,7 +133,7 @@ public function testLegacyExtract($property, array $type = null)
120133

121134
privatefunctiondoTestExtract(bool$legacy,$property,array$type =null)
122135
{
123-
$this->assertEquals($type,$this->createExtractor($legacy)->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy',$property, []));
136+
$this->assertEquals($type,$this->createExtractor($legacy)->getTypes(!class_exists(Types::class) ?'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class,$property, []));
124137
}
125138

126139
publicfunctiontestExtractWithEmbedded()
@@ -156,7 +169,7 @@ private function doTestExtractWithEmbedded(bool $legacy)
156169

157170
publicfunctiontypesProvider()
158171
{
159-
return [
172+
$provider = [
160173
['id', [newType(Type::BUILTIN_TYPE_INT)]],
161174
['guid', [newType(Type::BUILTIN_TYPE_STRING)]],
162175
['bigint', [newType(Type::BUILTIN_TYPE_STRING)]],
@@ -167,7 +180,7 @@ public function typesProvider()
167180
['decimal', [newType(Type::BUILTIN_TYPE_STRING)]],
168181
['bool', [newType(Type::BUILTIN_TYPE_BOOL)]],
169182
['binary', [newType(Type::BUILTIN_TYPE_RESOURCE)]],
170-
['json', [newType(Type::BUILTIN_TYPE_ARRAY,false,null,true)]],
183+
['jsonArray', [newType(Type::BUILTIN_TYPE_ARRAY,false,null,true)]],
171184
['foo', [newType(Type::BUILTIN_TYPE_OBJECT,true,'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')]],
172185
['bar', [newType(
173186
Type::BUILTIN_TYPE_OBJECT,
@@ -206,6 +219,12 @@ public function typesProvider()
206219
)]],
207220
['indexedByCustomType',null],
208221
];
222+
223+
if (class_exists(Types::class)) {
224+
$provider[] = ['json', [newType(Type::BUILTIN_TYPE_ARRAY,true,null,true)]];
225+
}
226+
227+
return$provider;
209228
}
210229

211230
publicfunctiontestGetPropertiesCatchException()

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class DoctrineDummy
7474
/**
7575
* @Column(type="json_array")
7676
*/
77-
private$json;
77+
private$jsonArray;
7878

7979
/**
8080
* @Column(type="simple_array")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp