|
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 Respect\Validation\Validator; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractComposite extends AbstractRule |
|
21
|
|
|
{ |
|
22
|
|
|
protected $rules = []; |
|
23
|
|
|
|
|
24
|
229 |
|
public function __construct(...$rule) |
|
25
|
|
|
{ |
|
26
|
229 |
|
$this->addRules($rule); |
|
27
|
229 |
|
} |
|
28
|
|
|
|
|
29
|
33 |
|
public function setName(string $name): Validatable |
|
30
|
|
|
{ |
|
31
|
33 |
|
$parentName = $this->getName(); |
|
32
|
33 |
|
foreach ($this->rules as $rule) { |
|
33
|
32 |
|
$ruleName = $rule->getName(); |
|
34
|
32 |
|
if ($ruleName && $parentName !== $ruleName) { |
|
35
|
9 |
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
30 |
|
$rule->setName($name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
33 |
|
return parent::setName($name); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
225 |
|
public function addRule($validator, $arguments = []) |
|
45
|
|
|
{ |
|
46
|
225 |
|
if (!$validator instanceof Validatable) { |
|
47
|
|
|
$this->appendRule(Validator::buildRule($validator, $arguments)); |
|
48
|
|
|
} else { |
|
49
|
225 |
|
$this->appendRule($validator); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
225 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function removeRules(): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->rules = []; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
229 |
|
public function addRules(array $validators) |
|
61
|
|
|
{ |
|
62
|
229 |
|
foreach ($validators as $key => $spec) { |
|
63
|
30 |
|
if ($spec instanceof Validatable) { |
|
64
|
30 |
|
$this->appendRule($spec); |
|
65
|
|
|
} elseif (is_numeric($key) && is_array($spec)) { |
|
66
|
|
|
$this->addRules($spec); |
|
67
|
|
|
} elseif (is_array($spec)) { |
|
68
|
|
|
$this->addRule($key, $spec); |
|
69
|
|
|
} else { |
|
70
|
30 |
|
$this->addRule($spec); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
229 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
216 |
|
public function getRules() |
|
78
|
|
|
{ |
|
79
|
216 |
|
return $this->rules; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
public function hasRule($validator) |
|
83
|
|
|
{ |
|
84
|
4 |
|
if (empty($this->rules)) { |
|
85
|
1 |
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
if ($validator instanceof Validatable) { |
|
89
|
2 |
|
return isset($this->rules[spl_object_hash($validator)]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
if (is_string($validator)) { |
|
93
|
1 |
|
foreach ($this->rules as $rule) { |
|
94
|
1 |
|
if (get_class($rule) == __NAMESPACE__.'\\'.$validator) { |
|
95
|
1 |
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
226 |
|
protected function appendRule(Validatable $validator): void |
|
104
|
|
|
{ |
|
105
|
226 |
|
if (!$validator->getName() && $this->getName()) { |
|
106
|
2 |
|
$validator->setName($this->getName()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
226 |
|
$this->rules[spl_object_hash($validator)] = $validator; |
|
110
|
226 |
|
} |
|
111
|
|
|
|
|
112
|
158 |
|
protected function validateRules($input) |
|
113
|
|
|
{ |
|
114
|
158 |
|
$validators = $this->getRules(); |
|
115
|
158 |
|
$exceptions = []; |
|
116
|
158 |
|
foreach ($validators as $v) { |
|
117
|
|
|
try { |
|
118
|
158 |
|
$v->assert($input); |
|
119
|
134 |
|
} catch (ValidationException $e) { |
|
120
|
158 |
|
$exceptions[] = $e; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
158 |
|
return $exceptions; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|