|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Artem Henvald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Fresh\DoctrineEnumBundle\Form; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
16
|
|
|
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType; |
|
17
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException; |
|
18
|
|
|
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
20
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
21
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* EnumTypeGuesser. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Artem Henvald <[email protected]> |
|
27
|
|
|
* @author Jaik Dean <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class EnumTypeGuesser extends DoctrineOrmTypeGuesser |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var string[] */ |
|
32
|
|
|
private $registeredEnumTypes = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ManagerRegistry $registry |
|
36
|
|
|
* @param mixed[] $registeredTypes |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ManagerRegistry $registry, array $registeredTypes) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct($registry); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($registeredTypes as $type => $details) { |
|
43
|
|
|
$this->registeredEnumTypes[$type] = $details['class']; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $class |
|
49
|
|
|
* @param string $property |
|
50
|
|
|
* |
|
51
|
|
|
* @throws EnumTypeIsRegisteredButClassDoesNotExistException |
|
52
|
|
|
* |
|
53
|
|
|
* @return TypeGuess|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function guessType(string $class, string $property): ?TypeGuess |
|
56
|
|
|
{ |
|
57
|
|
|
$classMetadata = $this->getMetadata($class); |
|
58
|
|
|
|
|
59
|
|
|
// If no metadata for this class - can't guess anything |
|
60
|
|
|
if (!$classMetadata) { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */ |
|
65
|
|
|
[$metadata] = $classMetadata; |
|
|
|
|
|
|
66
|
|
|
$fieldType = $metadata->getTypeOfField($property); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
// This is not one of the registered ENUM types |
|
69
|
|
|
if (!\is_string($fieldType) || !isset($this->registeredEnumTypes[$fieldType])) { |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$registeredEnumTypeFQCN = $this->registeredEnumTypes[$fieldType]; |
|
74
|
|
|
|
|
75
|
|
|
if (!\class_exists($registeredEnumTypeFQCN)) { |
|
76
|
|
|
$exceptionMessage = \sprintf( |
|
77
|
|
|
'ENUM type "%s" is registered as "%s", but that class does not exist', |
|
78
|
|
|
$fieldType, |
|
79
|
|
|
$registeredEnumTypeFQCN |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
throw new EnumTypeIsRegisteredButClassDoesNotExistException($exceptionMessage); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!\is_subclass_of($registeredEnumTypeFQCN, AbstractEnumType::class)) { |
|
|
|
|
|
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** @var AbstractEnumType $registeredEnumTypeFQCN */ |
|
90
|
|
|
$parameters = [ |
|
91
|
|
|
'choices' => $registeredEnumTypeFQCN::getChoices(), // Get the choices from the fully qualified class name |
|
92
|
|
|
'required' => !$metadata->isNullable($property), |
|
|
|
|
|
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
return new TypeGuess(ChoiceType::class, $parameters, Guess::VERY_HIGH_CONFIDENCE); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.