forked from fre5h/DoctrineEnumBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumTypeGuesser.php
104 lines (88 loc) · 3.36 KB
/
EnumTypeGuesser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* This file is part of the FreshDoctrineEnumBundle
*
* (c) Artem Genvald <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Fresh\DoctrineEnumBundle\Form;
use Doctrine\Common\Persistence\ManagerRegistry;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException;
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
/**
* EnumTypeGuesser.
*
* @author Artem Genvald <[email protected]>
* @author Jaik Dean <[email protected]>
*/
class EnumTypeGuesser extends DoctrineOrmTypeGuesser
{
/**
* @var AbstractEnumType[] $registeredEnumTypes Array of registered ENUM types
*/
protected $registeredEnumTypes = [];
/**
* Constructor.
*
* @param ManagerRegistry $registry Registry
* @param array $registeredTypes Array of registered ENUM types
*/
public function __construct(ManagerRegistry $registry, array $registeredTypes)
{
parent::__construct($registry);
foreach ($registeredTypes as $type => $details) {
$this->registeredEnumTypes[$type] = $details['class'];
}
}
/**
* Returns a field guess for a property name of a class.
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
*
* @return TypeGuess A guess for the field's type and options
*
* @throws EnumTypeIsRegisteredButClassDoesNotExistException
*/
public function guessType($class, $property)
{
$classMetadata = $this->getMetadata($class);
// If no metadata for this class - can't guess anything
if (!$classMetadata) {
return null;
}
/** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */
list($metadata) = $classMetadata;
$fieldType = $metadata->getTypeOfField($property);
// This is not one of the registered ENUM types
if (!isset($this->registeredEnumTypes[$fieldType])) {
return null;
}
$registeredEnumTypeFQCN = $this->registeredEnumTypes[$fieldType];
if (!class_exists($registeredEnumTypeFQCN)) {
throw new EnumTypeIsRegisteredButClassDoesNotExistException(sprintf(
'ENUM type "%s" is registered as "%s", but that class does not exist',
$fieldType,
$registeredEnumTypeFQCN
));
}
$abstractEnumTypeFQCN = 'Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType';
if (get_parent_class($registeredEnumTypeFQCN) !== $abstractEnumTypeFQCN) {
return null;
}
// Get the choices from the fully qualified class name
$parameters = [
'choices' => $registeredEnumTypeFQCN::getChoices(),
'required' => !$metadata->isNullable($property),
];
// Compatibility with Symfony <3.0
$fieldType = LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\ChoiceType');
return new TypeGuess($fieldType, $parameters, Guess::VERY_HIGH_CONFIDENCE);
}
}