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

Skip to content

Commit eb6e219

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

File tree

6 files changed

+127
-70
lines changed

6 files changed

+127
-70
lines changed

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

Lines changed: 18 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;
@@ -56,31 +57,32 @@ public function guessType($class, $property)
5657
return new TypeGuess('Symfony\Bridge\Doctrine\Form\Type\EntityType', ['em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple], Guess::HIGH_CONFIDENCE);
5758
}
5859

60+
$useDeprecatedConstants = !class_exists(Types::class);
5961
switch ($metadata->getTypeOfField($property)) {
60-
case Type::TARRAY:
62+
case $useDeprecatedConstants ? Type::TARRAY : 'array':
6163
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
62-
case Type::BOOLEAN:
64+
case $useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN:
6365
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
64-
case Type::DATETIME:
65-
case Type::DATETIMETZ:
66+
case $useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE:
67+
case $useDeprecatedConstants ? Type::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
6668
case 'vardatetime':
6769
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
6870
case 'dateinterval':
6971
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
70-
case Type::DATE:
72+
case $useDeprecatedConstants ? Type::DATE : Types::DATE_MUTABLE:
7173
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
72-
case Type::TIME:
74+
case $useDeprecatedConstants ? Type::TIME : Types::TIME_MUTABLE:
7375
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
74-
case Type::DECIMAL:
75-
case Type::FLOAT:
76+
case $useDeprecatedConstants ? Type::DECIMAL : Types::DECIMAL:
77+
case $useDeprecatedConstants ? Type::FLOAT : Types::FLOAT:
7678
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:
79+
case $useDeprecatedConstants ? Type::INTEGER : Types::INTEGER:
80+
case $useDeprecatedConstants ? Type::BIGINT : Types::BIGINT:
81+
case $useDeprecatedConstants ? Type::SMALLINT : Types::SMALLINT:
8082
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
81-
case Type::STRING:
83+
case $useDeprecatedConstants ? Type::STRING : Types::STRING:
8284
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
83-
case Type::TEXT:
85+
case $useDeprecatedConstants ? Type::TEXT : Types::TEXT:
8486
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
8587
default:
8688
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
@@ -103,7 +105,7 @@ public function guessRequired($class, $property)
103105

104106
// Check whether the field exists and is nullable or not
105107
if (isset($classMetadata->fieldMappings[$property])) {
106-
if (!$classMetadata->isNullable($property) && Type::BOOLEAN !== $classMetadata->getTypeOfField($property)) {
108+
if (!$classMetadata->isNullable($property) && (!class_exists(Types::class) ? Type::BOOLEAN : Types::BOOLEAN) !== $classMetadata->getTypeOfField($property)) {
107109
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
108110
}
109111

@@ -140,7 +142,7 @@ public function guessMaxLength($class, $property)
140142
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
141143
}
142144

143-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
145+
if (\in_array($ret[0]->getTypeOfField($property), !class_exists(Types::class) ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
144146
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
145147
}
146148
}
@@ -155,7 +157,7 @@ public function guessPattern($class, $property)
155157
{
156158
$ret = $this->getMetadata($class);
157159
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
158-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
160+
if (\in_array($ret[0]->getTypeOfField($property), !class_exists(Types::class) ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
159161
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
160162
}
161163
}

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

Lines changed: 30 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;
@@ -146,14 +147,15 @@ public function getTypes($class, $property, array $context = [])
146147

147148
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
148149

150+
$useDeprecatedConstants = !class_exists(Types::class);
149151
switch ($builtinType) {
150152
case Type::BUILTIN_TYPE_OBJECT:
151153
switch ($typeOfField) {
152-
case DBALType::DATE:
153-
case DBALType::DATETIME:
154-
case DBALType::DATETIMETZ:
154+
case $useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
155+
case $useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
156+
case $useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
155157
case 'vardatetime':
156-
case DBALType::TIME:
158+
case $useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
157159
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
158160

159161
case 'date_immutable':
@@ -169,11 +171,12 @@ public function getTypes($class, $property, array $context = [])
169171
break;
170172
case Type::BUILTIN_TYPE_ARRAY:
171173
switch ($typeOfField) {
172-
case DBALType::TARRAY:
173-
case DBALType::JSON_ARRAY:
174+
case $useDeprecatedConstants ? DBALType::TARRAY : 'array':
175+
case 'json_array':
176+
case $useDeprecatedConstants ? false : Types::JSON:
174177
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
175178

176-
case DBALType::SIMPLE_ARRAY:
179+
case $useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
177180
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
178181
}
179182
}
@@ -220,44 +223,46 @@ private function isAssociationNullable(array $associationMapping)
220223
*/
221224
private function getPhpType($doctrineType)
222225
{
226+
$useDeprecatedConstants = !class_exists(Types::class);
223227
switch ($doctrineType) {
224-
case DBALType::SMALLINT:
225-
case DBALType::INTEGER:
228+
case $useDeprecatedConstants ? DBALType::SMALLINT : Types::SMALLINT:
229+
case $useDeprecatedConstants ? DBALType::INTEGER : Types::INTEGER:
226230
return Type::BUILTIN_TYPE_INT;
227231

228-
case DBALType::FLOAT:
232+
case $useDeprecatedConstants ? DBALType::FLOAT : Types::FLOAT:
229233
return Type::BUILTIN_TYPE_FLOAT;
230234

231-
case DBALType::BIGINT:
232-
case DBALType::STRING:
233-
case DBALType::TEXT:
234-
case DBALType::GUID:
235-
case DBALType::DECIMAL:
235+
case $useDeprecatedConstants ? DBALType::BIGINT : Types::BIGINT:
236+
case $useDeprecatedConstants ? DBALType::STRING : Types::STRING:
237+
case $useDeprecatedConstants ? DBALType::TEXT : Types::TEXT:
238+
case $useDeprecatedConstants ? DBALType::GUID : Types::GUID:
239+
case $useDeprecatedConstants ? DBALType::DECIMAL : Types::DECIMAL:
236240
return Type::BUILTIN_TYPE_STRING;
237241

238-
case DBALType::BOOLEAN:
242+
case $useDeprecatedConstants ? DBALType::BOOLEAN : Types::BOOLEAN:
239243
return Type::BUILTIN_TYPE_BOOL;
240244

241-
case DBALType::BLOB:
245+
case $useDeprecatedConstants ? DBALType::BLOB : Types::BLOB:
242246
case 'binary':
243247
return Type::BUILTIN_TYPE_RESOURCE;
244248

245-
case DBALType::OBJECT:
246-
case DBALType::DATE:
247-
case DBALType::DATETIME:
248-
case DBALType::DATETIMETZ:
249+
case $useDeprecatedConstants ? DBALType::OBJECT : Types::OBJECT:
250+
case $useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
251+
case $useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
252+
case $useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
249253
case 'vardatetime':
250-
case DBALType::TIME:
254+
case $useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
251255
case 'date_immutable':
252256
case 'datetime_immutable':
253257
case 'datetimetz_immutable':
254258
case 'time_immutable':
255259
case 'dateinterval':
256260
return Type::BUILTIN_TYPE_OBJECT;
257261

258-
case DBALType::TARRAY:
259-
case DBALType::SIMPLE_ARRAY:
260-
case DBALType::JSON_ARRAY:
262+
case $useDeprecatedConstants ? DBALType::TARRAY : 'array':
263+
case $useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
264+
case 'json_array':
265+
case $useDeprecatedConstants ? false : Types::JSON:
261266
return Type::BUILTIN_TYPE_ARRAY;
262267
}
263268

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

Lines changed: 4 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;
@@ -90,7 +91,7 @@ public function updateToken($series, $tokenValue, \DateTime $lastUsed)
9091
];
9192
$paramTypes = [
9293
'value' => \PDO::PARAM_STR,
93-
'lastUsed' => DoctrineType::DATETIME,
94+
'lastUsed' => !class_exists(Types::class) ? Type::DATETIME : Types::DATETIME_MUTABLE,
9495
'series' => \PDO::PARAM_STR,
9596
];
9697
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes);
@@ -119,7 +120,7 @@ public function createNewToken(PersistentTokenInterface $token)
119120
'username' => \PDO::PARAM_STR,
120121
'series' => \PDO::PARAM_STR,
121122
'value' => \PDO::PARAM_STR,
122-
'lastUsed' => DoctrineType::DATETIME,
123+
'lastUsed' => !class_exists(Types::class) ? Type::DATETIME : Types::DATETIME_MUTABLE,
123124
];
124125
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
125126
}

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")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Doctrine\ORM\Mapping\ManyToMany;
18+
use Doctrine\ORM\Mapping\ManyToOne;
19+
use Doctrine\ORM\Mapping\OneToMany;
20+
21+
/**
22+
* @Entity
23+
*/
24+
final class DoctrineDummy210 extends DoctrineDummy
25+
{
26+
/**
27+
* @Column(type="json", nullable=true)
28+
*/
29+
private $json;
30+
}

0 commit comments

Comments
 (0)
0