8000 [DoctrineBridge] Use new Types::* constants and support new json type · symfony/symfony@04649fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 04649fd

Browse files
committed
[DoctrineBridge] Use new Types::* constants and support new json type
1 parent 643f34f commit 04649fd

File tree

6 files changed

+136
-70
lines changed

6 files changed

+136
-70
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\Common\Persistence\Mapping\MappingException as LegacyCommonMappingException;
1616
use Doctrine\Common\Util\ClassUtils;
1717
use Doctrine\DBAL\Types\Type;
18+
use Doctrine\DBAL\Types\Types;
1819
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1920
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
2021
use Doctrine\Persistence\ManagerRegistry;
@@ -30,12 +31,16 @@ 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+
self::$useDeprecatedConstants = self::$useDeprecatedConstants ?? !class_exists(Types::class);
3944
}
4045

4146
/**
@@ -57,30 +62,30 @@ public function guessType($class, $property)
5762
}
5863

5964
switch ($metadata->getTypeOfField($property)) {
60-
case Type::TARRAY:
65+
case self::$useDeprecatedConstants ? Type::TARRAY : 'array':
6166
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
62-
case Type::BOOLEAN:
67+
case self::$useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN:
6368
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
64-
case Type::DATETIME:
65-
case Type::DATETIMETZ:
69+
case self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE:
70+
case self::$useDeprecatedConstants ? Type::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
6671
case 'vardatetime':
6772
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
6873
case 'dateinterval':
6974
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
70-
case Type::DATE:
75+
case self::$useDeprecatedConstants ? Type::DATE : Types::DATE_MUTABLE:
7176
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
72-
case Type::TIME:
77+
case self::$useDeprecatedConstants ? Type::TIME : Types::TIME_MUTABLE:
7378
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
74-
case Type::DECIMAL:
75-
case Type::FLOAT:
79+
case self::$useDeprecatedConstants ? Type::DECIMAL : Types::DECIMAL:
80+
case self::$useDeprecatedConstants ? Type::FLOAT : Types::FLOAT:
7681
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:
82+
case self::$useDeprecatedConstants ? Type::INTEGER : Types::INTEGER:
83+
case self::$useDeprecatedConstants ? Type::BIGINT : Types::BIGINT:
84+
case self::$useDeprecatedConstants ? Type::SMALLINT : Types::SMALLINT:
8085
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
81-
case Type::STRING:
86+
case self::$useDeprecatedConstants ? Type::STRING : Types::STRING:
8287
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
83-
case Type::TEXT:
88+
case self::$useDeprecatedConstants ? Type::TEXT : Types::TEXT:
8489
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
8590
default:
8691
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
@@ -103,7 +108,7 @@ public function guessRequired($class, $property)
103108

104109
// Check whether the field exists and is nullable or not
105110
if (isset($classMetadata->fieldMappings[$property])) {
106-
if (!$classMetadata->isNullable($property) && Type::BOOLEAN !== $classMetadata->getTypeOfField($property)) {
111+
if (!$classMetadata->isNullable($property) && (self::$useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN) !== $classMetadata->getTypeOfField($property)) {
107112
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
108113
}
109114

@@ -140,7 +145,7 @@ public function guessMaxLength($class, $property)
140145
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
141146
}
142147

143-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
148+
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
144149
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
145150
}
146151
}
@@ -155,7 +160,7 @@ public function guessPattern($class, $property)
155160
{
156161
$ret = $this->getMetadata($class);
157162
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
158-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
163+
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
159164
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
160165
}
161166
}

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory as LegacyClassMetadataFactory;
1515
use Doctrine\Common\Persistence\Mapping\MappingException as LegacyMappingException;
1616
use Doctrine\DBAL\Types\Type as DBALType;
17+
use Doctrine\DBAL\Types\Types;
1718
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1819
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1920
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
@@ -31,12 +32,16 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
3132
{
3233
private $classMetadataFactory;
3334

35+
private static $useDeprecatedConstants;
36+
3437
/**
3538
* @param ClassMetadataFactory|LegacyClassMetadataFactory $classMetadataFactory
3639
*/
3740
public function __construct($classMetadataFactory)
3841
{
3942
$this->classMetadataFactory = $classMetadataFactory;
43+
44+
self::$useDeprecatedConstants = self::$useDeprecatedConstants ?? !class_exists(Types::class);
4045
}
4146

4247
/**
@@ -149,11 +154,11 @@ public function getTypes($class, $property, array $context = [])
149154
switch ($builtinType) {
150155
case Type::BUILTIN_TYPE_OBJECT:
151156
switch ($typeOfField) {
152-
case DBALType::DATE:
153-
case DBALType::DATETIME:
154-
case DBALType::DATETIMETZ:
157+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
158+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
159+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
155160
case 'vardatetime':
156-
case DBALType::TIME:
161+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
157162
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
158163

159164
case 'date_immutable':
@@ -169,11 +174,12 @@ public function getTypes($class, $property, array $context = [])
169174
break;
170175
case Type::BUILTIN_TYPE_ARRAY:
171176
switch ($typeOfField) {
172-
case DBALType::TARRAY:
173-
case DBALType::JSON_ARRAY:
177+
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
178+
case 'json_array':
179+
case self::$useDeprecatedConstants ? false : Types::JSON:
174180
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
175181

176-
case DBALType::SIMPLE_ARRAY:
182+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
177183
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
178184
}
179185
}
@@ -221,43 +227,44 @@ private function isAssociationNullable(array $associationMapping)
221227
private function getPhpType($doctrineType)
222228
{
223229
switch ($doctrineType) {
224-
case DBALType::SMALLINT:
225-
case DBALType::INTEGER:
230+
case self::$useDeprecatedConstants ? DBALType::SMALLINT : Types::SMALLINT:
231+
case self::$useDeprecatedConstants ? DBALType::INTEGER : Types::INTEGER:
226232
return Type::BUILTIN_TYPE_INT;
227233

228-
case DBALType::FLOAT:
234+
case self::$useDeprecatedConstants ? DBALType::FLOAT : Types::FLOAT:
229235
return Type::BUILTIN_TYPE_FLOAT;
230236

231-
case DBALType::BIGINT:
232-
case DBALType::STRING:
233-
case DBALType::TEXT:
234-
case DBALType::GUID:
235-
case DBALType::DECIMAL:
237+
case self::$useDeprecatedConstants ? DBALType::BIGINT : Types::BIGINT:
238+
case self::$useDeprecatedConstants ? DBALType::STRING : Types::STRING:
239+
case self::$useDeprecatedConstants ? DBALType::TEXT : Types::TEXT:
240+
case self::$useDeprecatedConstants ? DBALType::GUID : Types::GUID:
241+
case self::$useDeprecatedConstants ? DBALType::DECIMAL : Types::DECIMAL:
236242
return Type::BUILTIN_TYPE_STRING;
237243

238-
case DBALType::BOOLEAN:
244+
case self::$useDeprecatedConstants ? DBALType::BOOLEAN : Types::BOOLEAN:
239245
return Type::BUILTIN_TYPE_BOOL;
240246

241-
case DBALType::BLOB:
247+
case self::$useDeprecatedConstants ? DBALType::BLOB : Types::BLOB:
242248
case 'binary':
243249
return Type::BUILTIN_TYPE_RESOURCE;
244250

245-
case DBALType::OBJECT:
246-
case DBALType::DATE:
247-
case DBALType::DATETIME:
248-
case DBALType::DATETIMETZ:
251+
case self::$useDeprecatedConstants ? DBALType::OBJECT : Types::OBJECT:
252+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
253+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
254+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
249255
case 'vardatetime':
250-
case DBALType::TIME:
256+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
251257
case 'date_immutable':
252258
case 'datetime_immutable':
253259
case 'datetimetz_immutable':
254260
case 'time_immutable':
255261
case 'dateinterval':
256262
return Type::BUILTIN_TYPE_OBJECT;
257263

258-
case DBALType::TARRAY:
259-
case DBALType::SIMPLE_ARRAY:
260-
case DBALType::JSON_ARRAY:
264+
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
265+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
266+
case 'json_array':
267+
case self::$useDeprecatedConstants ? false : Types::JSON:
261268
return Type::BUILTIN_TYPE_ARRAY;
262269
}
263270

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

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

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

44+
private static $useDeprecatedConstants;
45+
4346
public function __construct(Connection $conn)
4447
{
4548
$this->conn = $conn;
49+
50+
self::$useDeprecatedConstants = self::$useDeprecatedConstants ?? !class_exists(Types::class);
4651
}
4752

4853
/**
@@ -90,7 +95,7 @@ public function updateToken($series, $tokenValue, \DateTime $lastUsed)
9095
];
9196
$paramTypes = [
9297
'value' => \PDO::PARAM_STR,
93-
'lastUsed' => DoctrineType::DATETIME,
98+
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
9499
'series' => \PDO::PARAM_STR,
95100
];
96101
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes);
@@ -119,7 +124,7 @@ public function createNewToken(PersistentTokenInterface $token)
119124
'username' => \PDO::PARAM_STR,
120125
'series' => \PDO::PARAM_STR,
121126
'value' => \PDO::PARAM_STR,
122-
'lastUsed' => DoctrineType::DATETIME,
127+
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
123128
];
124129
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
125130
}

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
use Doctrine\Common\Collections\Collection;
1515
use Doctrine\DBAL\Types\Type as DBALType;
16+
use Doctrine\DBAL\Types\Types;
1617
use Doctrine\ORM\EntityManager;
1718
use Doctrine\ORM\Tools\Setup;
1819
use PHPUnit\Framework\TestCase;
1920
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
21+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
2022
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
2123
use Symfony\Component\PropertyInfo\Type;
2224

@@ -45,29 +47,40 @@ protected function setUp()
4547

4648
public function testGetProperties()
4749
{
50+
// Fields
51+
$expected = [
52+
'id',
53+
'guid',
54+
'time',
55+
'timeImmutable',
56+
'dateInterval',
57+
'jsonArray',
58+
'simpleArray',
59+
'float',
60+
'decimal',
61+
'bool',
62+
'binary',
63+
'customFoo',
64+
'bigint',
65+
];
66+
67+
if (class_exists(Types::class)) {
68+
$expected[] = 'json';
69+
}
70+
71+
// Associations
72+
$expected = array_merge($expected, [
73+
'foo',
74+
'bar',
75+
'indexedBar',
76+
'indexedFoo',
77+
'indexedByDt',
78+
'indexedByCustomType',
79+
]);
80+
4881
$this->assertEquals(
49-
[
50-
'id',
51-
'guid',
52-
'time',
53-
'timeImmutable',
54-
'dateInterval',
55-
'json',
56-
'simpleArray',
57-
'float',
58-
'decimal',
59-
'bool',
60-
'binary',
61-
'customFoo',
62-
'bigint',
63-
'foo',
64-
'bar',
65-
'indexedBar',
66-
'indexedFoo',
67-
'indexedByDt',
68-
'indexedByCustomType',
69-
],
70-
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
82+
$expected,
83+
$this->extractor->getProperties(!class_exists(Types::class) ? 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class)
7184
);
7285
}
7386

@@ -91,7 +104,7 @@ public function testGetPropertiesWithEmbedded()
91104
*/
92105
public function testExtract($property, array $type = null)
93106
{
94-
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, []));
107+
$this->assertEquals($type, $this->extractor->getTypes(!class_exists(Types::class) ? 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class, $property, []));
95108
}
96109

97110
public function testExtractWithEmbedded()
@@ -117,7 +130,7 @@ public function testExtractWithEmbedded()
117130

118131
public function typesProvider()
119132
{
120-
return [
133+
$provider = [
121134
['id', [new Type(Type::BUILTIN_TYPE_INT)]],
122135
['guid', [new Type(Type::BUILTIN_TYPE_STRING)]],
123136
['bigint', [new Type(Type::BUILTIN_TYPE_STRING)]],
@@ -128,7 +141,7 @@ public function typesProvider()
128141
['decimal', [new Type(Type::BUILTIN_TYPE_STRING)]],
129142
['bool', [new Type(Type::BUILTIN_TYPE_BOOL)]],
130143
['binary', [new Type(Type::BUILTIN_TYPE_RESOURCE)]],
131-
['json', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
144+
['jsonArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
132145
['foo', [new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')]],
133146
['bar', [new Type(
134147
Type::BUILTIN_TYPE_OBJECT,
@@ -167,6 +180,12 @@ public function typesProvider()
167180
)]],
168181
['indexedByCustomType', null],
169182
];
183+
184+
if (class_exists(Types::class)) {
185+
$provider[] = ['json', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]];
186+
}
187+
188+
return $provider;
170189
}
171190

172191
public function testGetPropertiesCatchException()

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)
0