|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt\Validation; |
|
5
|
|
|
|
|
6
|
|
|
use BrenoRoosevelt\Validation\Factories\ComparisonFactory; |
|
7
|
|
|
use BrenoRoosevelt\Validation\Rules\AllowsEmpty; |
|
8
|
|
|
use BrenoRoosevelt\Validation\Rules\AllowsNull; |
|
9
|
|
|
use BrenoRoosevelt\Validation\Rules\NotRequired; |
|
10
|
|
|
use Countable; |
|
11
|
|
|
use IteratorAggregate; |
|
12
|
|
|
use SplObjectStorage; |
|
13
|
|
|
|
|
14
|
|
|
class RuleSet implements Rule, IteratorAggregate, Countable |
|
15
|
|
|
{ |
|
16
|
|
|
use GuardForValidation, |
|
17
|
|
|
ComparisonFactory, |
|
18
|
|
|
MaybeBelongsToField { |
|
19
|
|
|
setField as private; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private SplObjectStorage $rules; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @throws ValidationException if the field is provided and is blank |
|
26
|
|
|
*/ |
|
27
|
|
|
final public function __construct(?string $field = null, Rule|RuleSet ...$rules) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->rules = new SplObjectStorage; |
|
30
|
|
|
$this->field = $field; |
|
31
|
|
|
$this->attachRules(...$rules); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function new(): self |
|
35
|
|
|
{ |
|
36
|
|
|
return new self; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function forField(string $field, Rule|RuleSet ...$rules): self |
|
40
|
|
|
{ |
|
41
|
|
|
return new self($field, ...$rules); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function withRules(Rule|RuleSet ...$rules): self |
|
45
|
|
|
{ |
|
46
|
|
|
return new self(null, ...$rules); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function attachRules(Rule|RuleSet ...$rules): void |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($rules as $validationOrSet) { |
|
52
|
|
|
if ($validationOrSet instanceof Rule) { |
|
53
|
|
|
$this->rules->attach($validationOrSet); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($validationOrSet instanceof RuleSet) { |
|
57
|
|
|
foreach ($validationOrSet as $validation) { |
|
58
|
|
|
$this->rules->attach($validation); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function add(Rule|RuleSet ...$rules): self |
|
65
|
|
|
{ |
|
66
|
|
|
$instance = clone $this; |
|
67
|
|
|
$instance->attachRules(...$rules); |
|
68
|
|
|
return $instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function validate(mixed $input, array $context = []): ValidationResult|ValidationResultByField |
|
72
|
|
|
{ |
|
73
|
|
|
$violations = $empty = $this->newEmptyValidationResult(); |
|
74
|
|
|
if (!$this->shouldValidate($input)) { |
|
75
|
|
|
return $empty; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach ($this->rules as $rule) { |
|
79
|
|
|
$violations = $violations->error(...$rule->validate($input, $context)->getErrors()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $violations; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function shouldValidate(mixed $input): bool |
|
86
|
|
|
{ |
|
87
|
|
|
if (null === $input && $this->allowsNull()) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (is_string($input) && empty($input) && $this->allowsEmpty()) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function isRequired(): bool |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->isEmpty()) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->rules as $rule) { |
|
105
|
|
|
if ($rule instanceof NotRequired) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function allowsEmpty(): bool |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->isEmpty()) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
foreach ($this->rules as $rule) { |
|
120
|
|
|
if ($rule instanceof AllowsEmpty) { |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function allowsNull(): bool |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->isEmpty()) { |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
foreach ($this->rules as $rule) { |
|
135
|
|
|
if ($rule instanceof AllowsNull) { |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function setNotRequired(): self |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->add(NotRequired::instance()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Allows empty arrays or strings |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setAllowsEmpty(): self |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->add(AllowsEmpty::instance()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Allows null values |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setAllowsNull(): self |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->add(AllowsNull::instance()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function isEmpty(): bool |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->rules->count() === 0; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** @return Rule[] */ |
|
170
|
|
|
public function toArray(): array |
|
171
|
|
|
{ |
|
172
|
|
|
return iterator_to_array($this->rules); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getIterator(): SplObjectStorage |
|
176
|
|
|
{ |
|
177
|
|
|
return clone $this->rules; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function count(): int |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->rules->count(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|