|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
|
15
|
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
|
17
|
|
|
use Respect\Validation\Validatable; |
|
18
|
|
|
use function array_filter; |
|
19
|
|
|
use function array_map; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Abstract class for rules that are composed by other rules. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
|
25
|
|
|
* @author Henrique Moody <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractComposite extends AbstractRule |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Validatable[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $rules = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initializes the rule adding other rules to the stack. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Validatable ...$rules |
|
38
|
|
|
*/ |
|
39
|
222 |
|
public function __construct(Validatable ...$rules) |
|
40
|
|
|
{ |
|
41
|
222 |
|
$this->rules = $rules; |
|
42
|
222 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
33 |
|
public function setName(string $name): Validatable |
|
48
|
|
|
{ |
|
49
|
33 |
|
$parentName = $this->getName(); |
|
50
|
33 |
|
foreach ($this->rules as $rule) { |
|
51
|
32 |
|
$ruleName = $rule->getName(); |
|
52
|
32 |
|
if ($ruleName && $parentName !== $ruleName) { |
|
53
|
9 |
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
30 |
|
$rule->setName($name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
33 |
|
return parent::setName($name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Append a rule into the stack of rules. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Validatable $rule |
|
66
|
|
|
* |
|
67
|
|
|
* @return AbstractComposite |
|
68
|
|
|
*/ |
|
69
|
221 |
|
public function addRule(Validatable $rule): self |
|
70
|
|
|
{ |
|
71
|
221 |
|
if ($this->shouldHaveNameOverwritten($rule)) { |
|
72
|
2 |
|
$rule->setName($this->getName()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
221 |
|
$this->rules[] = $rule; |
|
76
|
|
|
|
|
77
|
221 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns all the rules in the stack. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Validatable[] |
|
84
|
|
|
*/ |
|
85
|
216 |
|
public function getRules(): array |
|
86
|
|
|
{ |
|
87
|
216 |
|
return $this->rules; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Validate the whole stack of rule. |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $input |
|
94
|
|
|
* |
|
95
|
|
|
* @return ValidationException[] |
|
96
|
|
|
*/ |
|
97
|
161 |
|
protected function validateRules($input): array |
|
98
|
|
|
{ |
|
99
|
161 |
|
return array_filter( |
|
100
|
161 |
|
array_map( |
|
101
|
161 |
|
function (Validatable $rule) use ($input): ?ValidationException { |
|
102
|
|
|
try { |
|
103
|
161 |
|
$rule->assert($input); |
|
104
|
138 |
|
} catch (ValidationException $exception) { |
|
105
|
138 |
|
return $exception; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
31 |
|
return null; |
|
109
|
161 |
|
}, |
|
110
|
161 |
|
$this->getRules() |
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
221 |
|
private function shouldHaveNameOverwritten(Validatable $rule): bool |
|
116
|
|
|
{ |
|
117
|
221 |
|
return $this->hasName($this) && !$this->hasName($rule); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
221 |
|
private function hasName(Validatable $rule): bool |
|
121
|
|
|
{ |
|
122
|
221 |
|
return null !== $rule->getName(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|