1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JBen87\ParsleyBundle\Constraint\Constraints; |
4
|
|
|
|
5
|
|
|
use JBen87\ParsleyBundle\Constraint\Constraint; |
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
7
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
8
|
|
|
|
9
|
|
|
class Range extends Constraint |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Constraint[] |
13
|
|
|
*/ |
14
|
|
|
private $constraints; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param array $options |
18
|
|
|
*/ |
19
|
4 |
|
public function __construct(array $options = []) |
20
|
|
|
{ |
21
|
4 |
|
parent::__construct($options); |
22
|
|
|
|
23
|
2 |
|
$this->constraints = [ |
24
|
3 |
|
'min' => $this->createMin($options), |
25
|
2 |
|
'max' => $this->createMax($options), |
26
|
|
|
]; |
27
|
2 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
1 |
|
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array |
33
|
|
|
{ |
34
|
1 |
|
return array_merge( |
35
|
1 |
|
$this->constraints['min']->normalize($normalizer, 'array'), |
36
|
1 |
|
$this->constraints['max']->normalize($normalizer, 'array') |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
* |
43
|
|
|
* @codeCoverageIgnore |
44
|
|
|
*/ |
45
|
|
|
protected function getAttribute(): string |
46
|
|
|
{ |
47
|
|
|
throw new \RuntimeException('Should not be called.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
* |
53
|
|
|
* @codeCoverageIgnore |
54
|
|
|
*/ |
55
|
|
|
protected function getValue(): string |
56
|
|
|
{ |
57
|
|
|
throw new \RuntimeException('Should not be called.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
4 |
|
protected function configureOptions(OptionsResolver $resolver): void |
64
|
|
|
{ |
65
|
|
|
$resolver |
66
|
4 |
|
->setRequired(['min', 'max']) |
67
|
4 |
|
->setAllowedTypes('min', ['numeric']) |
68
|
4 |
|
->setAllowedTypes('max', ['numeric']) |
69
|
4 |
|
->setDefined(['minMessage', 'maxMessage']) |
70
|
|
|
; |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $defaults |
75
|
|
|
* |
76
|
|
|
* @return Min |
77
|
|
|
*/ |
78
|
3 |
|
private function createMin(array $defaults): Min |
79
|
|
|
{ |
80
|
3 |
|
$options = ['min' => $defaults['min']]; |
81
|
|
|
|
82
|
3 |
|
if (array_key_exists('minMessage', $defaults)) { |
83
|
1 |
|
$options['message'] = $defaults['minMessage']; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return new Min($options); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $defaults |
91
|
|
|
* |
92
|
|
|
* @return Max |
93
|
|
|
*/ |
94
|
2 |
|
private function createMax(array $defaults): Max |
95
|
|
|
{ |
96
|
2 |
|
$options = ['max' => $defaults['max']]; |
97
|
|
|
|
98
|
2 |
|
if (array_key_exists('maxMessage', $defaults)) { |
99
|
1 |
|
$options['message'] = $defaults['maxMessage']; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return new Max($options); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|