Skip to content

Commit

Permalink
[DoctrineBridge] Use new Types::* constants and support new json type
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Feb 24, 2020
1 parent 643f34f commit 3e35fa5
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 70 deletions.
39 changes: 23 additions & 16 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Expand Up @@ -15,6 +15,7 @@
use Doctrine\Common\Persistence\Mapping\MappingException as LegacyCommonMappingException;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -30,12 +31,18 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface

private $cache = [];

private static $useDeprecatedConstants;

/**
* @param ManagerRegistry|LegacyManagerRegistry $registry
*/
public function __construct($registry)
{
$this->registry = $registry;

if (null === self::$useDeprecatedConstants) {
self::$useDeprecatedConstants = !class_exists(Types::class);
}
}

/**
Expand All @@ -57,30 +64,30 @@ public function guessType($class, $property)
}

switch ($metadata->getTypeOfField($property)) {
case Type::TARRAY:
case self::$useDeprecatedConstants ? Type::TARRAY : 'array':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
case Type::BOOLEAN:
case self::$useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
case Type::DATETIME:
case Type::DATETIMETZ:
case self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE:
case self::$useDeprecatedConstants ? Type::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
case 'dateinterval':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
case Type::DATE:
case self::$useDeprecatedConstants ? Type::DATE : Types::DATE_MUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
case Type::TIME:
case self::$useDeprecatedConstants ? Type::TIME : Types::TIME_MUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
case Type::DECIMAL:
case Type::FLOAT:
case self::$useDeprecatedConstants ? Type::DECIMAL : Types::DECIMAL:
case self::$useDeprecatedConstants ? Type::FLOAT : Types::FLOAT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
case Type::INTEGER:
case Type::BIGINT:
case Type::SMALLINT:
case self::$useDeprecatedConstants ? Type::INTEGER : Types::INTEGER:
case self::$useDeprecatedConstants ? Type::BIGINT : Types::BIGINT:
case self::$useDeprecatedConstants ? Type::SMALLINT : Types::SMALLINT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
case Type::STRING:
case self::$useDeprecatedConstants ? Type::STRING : Types::STRING:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
case Type::TEXT:
case self::$useDeprecatedConstants ? Type::TEXT : Types::TEXT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
default:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
Expand All @@ -103,7 +110,7 @@ public function guessRequired($class, $property)

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

Expand Down Expand Up @@ -140,7 +147,7 @@ public function guessMaxLength($class, $property)
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
}

if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
}
Expand All @@ -155,7 +162,7 @@ public function guessPattern($class, $property)
{
$ret = $this->getMetadata($class);
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
}
Expand Down
59 changes: 34 additions & 25 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory as LegacyClassMetadataFactory;
use Doctrine\Common\Persistence\Mapping\MappingException as LegacyMappingException;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
Expand All @@ -31,12 +32,18 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
{
private $classMetadataFactory;

private static $useDeprecatedConstants;

/**
* @param ClassMetadataFactory|LegacyClassMetadataFactory $classMetadataFactory
*/
public function __construct($classMetadataFactory)
{
$this->classMetadataFactory = $classMetadataFactory;

if (null === self::$useDeprecatedConstants) {
self::$useDeprecatedConstants = !class_exists(Types::class);
}
}

/**
Expand Down Expand Up @@ -149,11 +156,11 @@ public function getTypes($class, $property, array $context = [])
switch ($builtinType) {
case Type::BUILTIN_TYPE_OBJECT:
switch ($typeOfField) {
case DBALType::DATE:
case DBALType::DATETIME:
case DBALType::DATETIMETZ:
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
case DBALType::TIME:
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];

case 'date_immutable':
Expand All @@ -169,11 +176,12 @@ public function getTypes($class, $property, array $context = [])
break;
case Type::BUILTIN_TYPE_ARRAY:
switch ($typeOfField) {
case DBALType::TARRAY:
case DBALType::JSON_ARRAY:
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
case 'json_array':
case self::$useDeprecatedConstants ? false : Types::JSON:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];

case DBALType::SIMPLE_ARRAY:
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
}
}
Expand Down Expand Up @@ -221,43 +229,44 @@ private function isAssociationNullable(array $associationMapping)
private function getPhpType($doctrineType)
{
switch ($doctrineType) {
case DBALType::SMALLINT:
case DBALType::INTEGER:
case self::$useDeprecatedConstants ? DBALType::SMALLINT : Types::SMALLINT:
case self::$useDeprecatedConstants ? DBALType::INTEGER : Types::INTEGER:
return Type::BUILTIN_TYPE_INT;

case DBALType::FLOAT:
case self::$useDeprecatedConstants ? DBALType::FLOAT : Types::FLOAT:
return Type::BUILTIN_TYPE_FLOAT;

case DBALType::BIGINT:
case DBALType::STRING:
case DBALType::TEXT:
case DBALType::GUID:
case DBALType::DECIMAL:
case self::$useDeprecatedConstants ? DBALType::BIGINT : Types::BIGINT:
case self::$useDeprecatedConstants ? DBALType::STRING : Types::STRING:
case self::$useDeprecatedConstants ? DBALType::TEXT : Types::TEXT:
case self::$useDeprecatedConstants ? DBALType::GUID : Types::GUID:
case self::$useDeprecatedConstants ? DBALType::DECIMAL : Types::DECIMAL:
return Type::BUILTIN_TYPE_STRING;

case DBALType::BOOLEAN:
case self::$useDeprecatedConstants ? DBALType::BOOLEAN : Types::BOOLEAN:
return Type::BUILTIN_TYPE_BOOL;

case DBALType::BLOB:
case self::$useDeprecatedConstants ? DBALType::BLOB : Types::BLOB:
case 'binary':
return Type::BUILTIN_TYPE_RESOURCE;

case DBALType::OBJECT:
case DBALType::DATE:
case DBALType::DATETIME:
case DBALType::DATETIMETZ:
case self::$useDeprecatedConstants ? DBALType::OBJECT : Types::OBJECT:
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
case DBALType::TIME:
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
case 'date_immutable':
case 'datetime_immutable':
case 'datetimetz_immutable':
case 'time_immutable':
case 'dateinterval':
return Type::BUILTIN_TYPE_OBJECT;

case DBALType::TARRAY:
case DBALType::SIMPLE_ARRAY:
case DBALType::JSON_ARRAY:
case self::$useDeprecatedConstants ? DBALType::TARRAY : 'array':
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
case 'json_array':
case self::$useDeprecatedConstants ? false : Types::JSON:
return Type::BUILTIN_TYPE_ARRAY;
}

Expand Down
Expand Up @@ -12,7 +12,8 @@
namespace Symfony\Bridge\Doctrine\Security\RememberMe;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type as DoctrineType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface;
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface;
Expand Down Expand Up @@ -40,9 +41,15 @@ class DoctrineTokenProvider implements TokenProviderInterface
{
private $conn;

private static $useDeprecatedConstants;

public function __construct(Connection $conn)
{
$this->conn = $conn;

if (null === self::$useDeprecatedConstants) {
self::$useDeprecatedConstants = !class_exists(Types::class);
}
}

/**
Expand Down Expand Up @@ -90,7 +97,7 @@ public function updateToken($series, $tokenValue, \DateTime $lastUsed)
];
$paramTypes = [
'value' => \PDO::PARAM_STR,
'lastUsed' => DoctrineType::DATETIME,
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
'series' => \PDO::PARAM_STR,
];
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes);
Expand Down Expand Up @@ -119,7 +126,7 @@ public function createNewToken(PersistentTokenInterface $token)
'username' => \PDO::PARAM_STR,
'series' => \PDO::PARAM_STR,
'value' => \PDO::PARAM_STR,
'lastUsed' => DoctrineType::DATETIME,
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
];
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
}
Expand Down
Expand Up @@ -13,10 +13,12 @@

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
use Symfony\Component\PropertyInfo\Type;

Expand Down Expand Up @@ -45,29 +47,40 @@ protected function setUp()

public function testGetProperties()
{
// Fields
$expected = [
'id',
'guid',
'time',
'timeImmutable',
'dateInterval',
'jsonArray',
'simpleArray',
'float',
'decimal',
'bool',
'binary',
'customFoo',
'bigint',
];

if (class_exists(Types::class)) {
$expected[] = 'json';
}

// Associations
$expected = array_merge($expected, [
'foo',
'bar',
'indexedBar',
'indexedFoo',
'indexedByDt',
'indexedByCustomType',
]);

$this->assertEquals(
[
'id',
'guid',
'time',
'timeImmutable',
'dateInterval',
'json',
'simpleArray',
'float',
'decimal',
'bool',
'binary',
'customFoo',
'bigint',
'foo',
'bar',
'indexedBar',
'indexedFoo',
'indexedByDt',
'indexedByCustomType',
],
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
$expected,
$this->extractor->getProperties(!class_exists(Types::class) ? 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class)
);
}

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

public function testExtractWithEmbedded()
Expand All @@ -117,7 +130,7 @@ public function testExtractWithEmbedded()

public function typesProvider()
{
return [
$provider = [
['id', [new Type(Type::BUILTIN_TYPE_INT)]],
['guid', [new Type(Type::BUILTIN_TYPE_STRING)]],
['bigint', [new Type(Type::BUILTIN_TYPE_STRING)]],
Expand All @@ -128,7 +141,7 @@ public function typesProvider()
['decimal', [new Type(Type::BUILTIN_TYPE_STRING)]],
['bool', [new Type(Type::BUILTIN_TYPE_BOOL)]],
['binary', [new Type(Type::BUILTIN_TYPE_RESOURCE)]],
['json', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
['jsonArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
['foo', [new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')]],
['bar', [new Type(
Type::BUILTIN_TYPE_OBJECT,
Expand Down Expand Up @@ -167,6 +180,12 @@ public function typesProvider()
)]],
['indexedByCustomType', null],
];

if (class_exists(Types::class)) {
$provider[] = ['json', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]];
}

return $provider;
}

public function testGetPropertiesCatchException()
Expand Down
Expand Up @@ -74,7 +74,7 @@ class DoctrineDummy
/**
* @Column(type="json_array")
*/
private $json;
private $jsonArray;

/**
* @Column(type="simple_array")
Expand Down

0 comments on commit 3e35fa5

Please sign in to comment.