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

Skip to content

Commit 3e35fa5

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

File tree

6 files changed

+142
-70
lines changed

6 files changed

+142
-70
lines changed

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

Lines changed: 23 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,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
}

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

Lines changed: 34 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,18 @@ 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+
if (null === self::$useDeprecatedConstants) {
45+
self::$useDeprecatedConstants = !class_exists(Types::class);
46+
}
4047
}
4148

4249
/**
@@ -149,11 +156,11 @@ public function getTypes($class, $property, array $context = [])
149156
switch ($builtinType) {
150157
case Type::BUILTIN_TYPE_OBJECT:
151158
switch ($typeOfField) {
152-
case DBALType::DATE:
153-
case DBALType::DATETIME:
154-
case DBALType::DATETIMETZ:
159+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
160+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
161+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
155162
case 'vardatetime':
156-
case DBALType::TIME:
163+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
157164
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
158165

159166
case 'date_immutable':
@@ -169,11 +176,12 @@ public function getTypes($class, $property, array $context = [])
169176
break;
170177
case Type::BUILTIN_TYPE_ARRAY:
171178
switch ($typeOfField) {
172-
case DBALType::TARRAY:
173-
case DBALType::JSON_ARRAY:
179+
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
180+
case 'json_array':
181+
case self::$useDeprecatedConstants ? false : Types::JSON:
174182
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
175183

176-
case DBALType::SIMPLE_ARRAY:
184+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
177185
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
178186
}
179187
}
@@ -221,43 +229,44 @@ private function isAssociationNullable(array $associationMapping)
221229
private function getPhpType($doctrineType)
222230
{
223231
switch ($doctrineType) {
224-
case DBALType::SMALLINT:
225-
case DBALType::INTEGER:
232+
case self::$useDeprecatedConstants ? DBALType::SMALLINT : Types::SMALLINT:
233+
case self::$useDeprecatedConstants ? DBALType::INTEGER : Types::INTEGER:
226234
return Type::BUILTIN_TYPE_INT;
227235

228-
case DBALType::FLOAT:
236+
case self::$useDeprecatedConstants ? DBALType::FLOAT : Types::FLOAT:
229237
return Type::BUILTIN_TYPE_FLOAT;
230238

231-
case DBALType::BIGINT:
232-
case DBALType::STRING:
233-
case DBALType::TEXT:
234-
case DBALType::GUID:
235-
case DBALType::DECIMAL:
239+
case self::$useDeprecatedConstants ? DBALType::BIGINT : Types::BIGINT:
240+
case self::$useDeprecatedConstants ? DBALType::STRING : Types::STRING:
241+
case self::$useDeprecatedConstants ? DBALType::TEXT : Types::TEXT:
242+
case self::$useDeprecatedConstants ? DBALType::GUID : Types::GUID:
243+
case self::$useDeprecatedConstants ? DBALType::DECIMAL : Types::DECIMAL:
236244
return Type::BUILTIN_TYPE_STRING;
237245

238-
case DBALType::BOOLEAN:
246+
case self::$useDeprecatedConstants ? DBALType::BOOLEAN : Types::BOOLEAN:
239247
return Type::BUILTIN_TYPE_BOOL;
240248

241-
case DBALType::BLOB:
249+
case self::$useDeprecatedConstants ? DBALType::BLOB : Types::BLOB:
242250
case 'binary':
243251
return Type::BUILTIN_TYPE_RESOURCE;
244252

245-
case DBALType::OBJECT:
246-
case DBALType::DATE:
247-
case DBALType::DATETIME:
248-
case DBALType::DATETIMETZ:
253+
case self::$useDeprecatedConstants ? DBALType::OBJECT : Types::OBJECT:
254+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
255+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
256+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
249257
case 'vardatetime':
250-
case DBALType::TIME:
258+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
251259
case 'date_immutable':
252260
case 'datetime_immutable':
253261
case 'datetimetz_immutable':
254262
case 'time_immutable':
255263
case 'dateinterval':
256264
return Type::BUILTIN_TYPE_OBJECT;
257265

258-
case DBALType::TARRAY:
259-
case DBALType::SIMPLE_ARRAY:
260-
case DBALType::JSON_ARRAY:
266+
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
267+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
268+
case 'json_array':
269+
case self::$useDeprecatedConstants ? false : Types::JSON:
261270
return Type::BUILTIN_TYPE_ARRAY;
262271
}
263272

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
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,15 @@ 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+
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
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',
4E34 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