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