|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Leaf; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\AbstractElementBuilder; |
|
6
|
|
|
use Bdf\Form\Choice\ChoiceBuilderTrait; |
|
7
|
|
|
use Bdf\Form\Registry\RegistryInterface; |
|
8
|
|
|
use Bdf\Form\Transformer\TransformerInterface; |
|
9
|
|
|
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints\LessThanOrEqual; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Positive; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base builder for an number element |
|
15
|
|
|
* |
|
16
|
|
|
* @template E as \Bdf\Form\ElementInterface |
|
17
|
|
|
* @extends AbstractElementBuilder<E> |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class NumberElementBuilder extends AbstractElementBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
use ChoiceBuilderTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $raw = false; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* NumberElementBuilder constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param RegistryInterface|null $registry |
|
33
|
|
|
*/ |
|
34
|
138 |
|
public function __construct(RegistryInterface $registry = null) |
|
35
|
|
|
{ |
|
36
|
138 |
|
parent::__construct($registry); |
|
37
|
|
|
|
|
38
|
138 |
|
$this->addTransformerProvider([$this, 'provideNumberTransformer']); |
|
39
|
138 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set the minimal value constraint |
|
43
|
|
|
* |
|
44
|
|
|
* @param int|float $min The minimal value (included) |
|
45
|
|
|
* @param string|null $message The error message |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
20 |
|
public function min($min, ?string $message = null): self |
|
50
|
|
|
{ |
|
51
|
20 |
|
$options = ['value' => $min]; |
|
52
|
|
|
|
|
53
|
20 |
|
if ($message) { |
|
54
|
2 |
|
$options['message'] = $message; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
20 |
|
$this->satisfy(new GreaterThanOrEqual($options)); |
|
58
|
|
|
|
|
59
|
20 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the maximal value constraint |
|
64
|
|
|
* |
|
65
|
|
|
* @param int|float $max The maximal value (included) |
|
66
|
|
|
* @param string|null $message The error message |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
5 |
|
public function max($max, ?string $message = null): self |
|
71
|
|
|
{ |
|
72
|
5 |
|
$options = ['value' => $max]; |
|
73
|
|
|
|
|
74
|
5 |
|
if ($message) { |
|
75
|
2 |
|
$options['message'] = $message; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
$this->satisfy(new LessThanOrEqual($options)); |
|
79
|
|
|
|
|
80
|
5 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The number must be positive |
|
85
|
|
|
* |
|
86
|
|
|
* @param array|string $options The constraint options, or the error message |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
* @see Positive |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function positive($options = []): self |
|
92
|
|
|
{ |
|
93
|
2 |
|
if (is_string($options)) { |
|
94
|
1 |
|
$options = ['message' => $options]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
return $this->satisfy(new Positive($options)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Enable raw number mode |
|
102
|
|
|
* |
|
103
|
|
|
* In raw mode, the value will not be parsed according the the locale, but only cast the value to the php number type |
|
104
|
|
|
* This mode is useful when the input format is normalized, like in APIs |
|
105
|
|
|
* |
|
106
|
|
|
* @param bool $flag Enable or disable the raw mode |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
22 |
|
public function raw(bool $flag = true): self |
|
111
|
|
|
{ |
|
112
|
22 |
|
$this->raw = $flag; |
|
113
|
|
|
|
|
114
|
22 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
137 |
|
protected function defaultTransformerExceptionConstraintOptions(): array |
|
121
|
|
|
{ |
|
122
|
|
|
return [ |
|
123
|
137 |
|
'message' => 'The value is not a valid number.', |
|
124
|
|
|
'code' => 'INVALID_NUMBER_ERROR', |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Creates the localized number transformer |
|
130
|
|
|
* |
|
131
|
|
|
* @return TransformerInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
abstract protected function numberTransformer(): TransformerInterface; |
|
134
|
|
|
|
|
135
|
137 |
|
final protected function provideNumberTransformer(): array |
|
136
|
|
|
{ |
|
137
|
137 |
|
if (!$this->raw) { |
|
138
|
115 |
|
return [$this->numberTransformer()]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
22 |
|
return []; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|