1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Form\Extension; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractType; |
6
|
|
|
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser; |
7
|
|
|
use Symfony\Component\Form\Guess\Guess; |
8
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
11
|
|
|
use Yokai\EnumBundle\Form\Type\EnumType; |
12
|
|
|
use Yokai\EnumBundle\Registry\EnumRegistryInterface; |
13
|
|
|
use Yokai\EnumBundle\Validator\Constraints\Enum; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Yann Eugoné <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class EnumTypeGuesser extends ValidatorTypeGuesser |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EnumRegistryInterface |
22
|
|
|
*/ |
23
|
|
|
private $enumRegistry; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $enumFormType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
32
|
|
|
* @param EnumRegistryInterface $enumRegistry |
33
|
|
|
*/ |
34
|
5 |
|
public function __construct(MetadataFactoryInterface $metadataFactory, EnumRegistryInterface $enumRegistry) |
35
|
|
|
{ |
36
|
5 |
|
parent::__construct($metadataFactory); |
37
|
|
|
|
38
|
5 |
|
$this->enumRegistry = $enumRegistry; |
39
|
|
|
|
40
|
5 |
|
if (method_exists(AbstractType::class, 'getBlockPrefix')) { |
41
|
|
|
$this->enumFormType = EnumType::class; //Symfony 3.x support |
42
|
|
|
} else { |
43
|
5 |
|
$this->enumFormType = 'enum'; //Symfony 2.x support |
44
|
|
|
} |
45
|
5 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
2 |
|
public function guessTypeForConstraint(Constraint $constraint) |
51
|
|
|
{ |
52
|
2 |
|
if (!$constraint instanceof Enum) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return new TypeGuess( |
57
|
2 |
|
$this->enumFormType, |
58
|
|
|
[ |
59
|
2 |
|
'enum' => $constraint->enum, |
60
|
2 |
|
'multiple' => $constraint->multiple, |
61
|
2 |
|
], |
62
|
|
|
Guess::HIGH_CONFIDENCE |
63
|
2 |
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
2 |
|
public function guessRequired($class, $property) |
70
|
|
|
{ |
71
|
2 |
|
return null; //override parent : not able to guess |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
2 |
|
public function guessMaxLength($class, $property) |
78
|
|
|
{ |
79
|
2 |
|
return null; //override parent : not able to guess |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
2 |
|
public function guessPattern($class, $property) |
86
|
|
|
{ |
87
|
2 |
|
return null; //override parent : not able to guess |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|