1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt\Validation; |
5
|
|
|
|
6
|
|
|
use BrenoRoosevelt\Validation\Exception\ValidateOrFailTrait; |
7
|
|
|
use BrenoRoosevelt\Validation\Rules\AllowsEmpty; |
8
|
|
|
use BrenoRoosevelt\Validation\Rules\AllowsNull; |
9
|
|
|
use BrenoRoosevelt\Validation\Rules\IsEmpty; |
10
|
|
|
use BrenoRoosevelt\Validation\Rules\Required; |
11
|
|
|
|
12
|
|
|
class RuleSet implements Rule, BelongsToField, Stopable |
13
|
|
|
{ |
14
|
|
|
use RuleChainTrait, BelongsToFieldTrait, StopableTrait, ValidateOrFailTrait; |
15
|
|
|
|
16
|
|
|
/** @var Rule[] */ |
17
|
|
|
private array $rules = []; |
18
|
|
|
|
19
|
|
|
final public function __construct(?string $field = null, Rule | RuleSet ...$rules) |
20
|
|
|
{ |
21
|
|
|
$this->field = $field; |
22
|
|
|
foreach ($rules as $ruleOrRuleSet) { |
23
|
|
|
array_push( |
24
|
|
|
$this->rules, |
25
|
|
|
...($ruleOrRuleSet instanceof RuleSet ? $ruleOrRuleSet->rules() : [$ruleOrRuleSet]) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function new(): self |
31
|
|
|
{ |
32
|
|
|
return new self; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function of(string $field, Rule | RuleSet ...$rules): self |
36
|
|
|
{ |
37
|
|
|
return new self($field, ...$rules); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function withRules(Rule | RuleSet ...$rules): self |
41
|
|
|
{ |
42
|
|
|
return new self(null, ...$rules); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function add(Rule | RuleSet ...$rules): static |
46
|
|
|
{ |
47
|
|
|
return new self($this->field, ...$this->rules, ...$rules); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @inheritDoc */ |
51
|
|
|
public function validate(mixed $input, array $context = []): Result |
52
|
|
|
{ |
53
|
|
|
if (!$this->shouldValidate($input, $context)) { |
54
|
|
|
return ErrorReporting::success(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$errorReporting = new ErrorReporting; |
58
|
|
|
foreach ($this->rules as $rule) { |
59
|
|
|
if ($rule instanceof BelongsToField) { |
60
|
|
|
$rule = $rule->setField($this->getField()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$result = $rule->validate($input, $context); |
64
|
|
|
$errorReporting = $errorReporting->add($result); |
65
|
|
|
if ($this->shouldStop($rule, $result)) { |
66
|
|
|
$this->stopOnFailure = true; |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $errorReporting; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function shouldValidate(mixed $input, array $context): bool |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (null === $input && $this->hasAllowsNull()) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ((new IsEmpty)->isValid($input) && $this->hasAllowsEmpty()) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function shouldStop(Rule $rule, Result $result): bool |
88
|
|
|
{ |
89
|
|
|
return $rule instanceof Stopable && $rule->stopOnFailure() && !$result->isOk(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function hasRequired(): bool |
93
|
|
|
{ |
94
|
|
|
return $this->someRule(fn(Rule $rule) => $rule instanceof Required); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function hasAllowsNull(): bool |
98
|
|
|
{ |
99
|
|
|
return $this->someRule(fn(Rule $rule) => $rule instanceof AllowsNull); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function hasAllowsEmpty(): bool |
103
|
|
|
{ |
104
|
|
|
return $this->someRule(fn(Rule $rule) => $rule instanceof AllowsEmpty); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function isEmpty(): bool |
108
|
|
|
{ |
109
|
|
|
return empty($this->rules); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @return Rule[] */ |
113
|
|
|
public function rules(): array |
114
|
|
|
{ |
115
|
|
|
return $this->rules; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function someRule(callable $callback): bool |
119
|
|
|
{ |
120
|
|
|
foreach ($this->rules as $rule) { |
121
|
|
|
if (true === call_user_func_array($callback, [$rule])) { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.