|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "elao/enum" package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2016 Elao |
|
7
|
|
|
* |
|
8
|
|
|
* @author Elao <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Elao\Enum\Bridge\Symfony\Validator\Constraint; |
|
12
|
|
|
|
|
13
|
|
|
use Elao\Enum\EnumInterface; |
|
14
|
|
|
use Symfony\Component\Validator\Constraints\Choice; |
|
15
|
|
|
use Symfony\Component\Validator\Constraints\ChoiceValidator; |
|
16
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
17
|
|
|
|
|
18
|
|
|
class Enum extends Choice |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
public $class; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set to true in order to validate enum values instead of instances. |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public $asValue = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($options) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($options); |
|
36
|
|
|
|
|
37
|
|
|
$this->strict = true; |
|
38
|
|
|
|
|
39
|
|
|
if (!is_a($this->class, EnumInterface::class, true)) { |
|
40
|
|
|
throw new ConstraintDefinitionException(sprintf( |
|
41
|
|
|
'The "class" option value must be a class FQCN implementing "%s". "%s" given.', |
|
42
|
|
|
EnumInterface::class, |
|
43
|
|
|
$this->class |
|
44
|
|
|
)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Normalize choices |
|
48
|
|
|
if (is_array($this->choices)) { |
|
49
|
|
|
$choices = []; |
|
50
|
|
|
foreach ($this->choices as $choiceValue) { |
|
51
|
|
|
if (false === call_user_func([$this->class, 'accepts'], $choiceValue)) { |
|
52
|
|
|
throw new ConstraintDefinitionException(sprintf( |
|
53
|
|
|
'Choice %s is not a valid value for enum type "%s".', |
|
54
|
|
|
json_encode($choiceValue), |
|
55
|
|
|
$this->class |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$choices[] = $this->asValue ? $choiceValue : call_user_func([$this->class, 'get'], $choiceValue); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->choices = $choices; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// only set the callback if no choice list set |
|
66
|
|
|
if (!is_array($this->choices)) { |
|
67
|
|
|
$this->callback = $this->asValue ? [$this->class, 'values'] : [$this->class, 'instances']; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function validatedBy() |
|
75
|
|
|
{ |
|
76
|
|
|
return ChoiceValidator::class; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getDefaultOption() |
|
83
|
|
|
{ |
|
84
|
|
|
return 'class'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRequiredOptions() |
|
91
|
|
|
{ |
|
92
|
|
|
return ['class']; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|