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
|
17 |
|
public function __construct(...$rule) |
25
|
|
|
{ |
26
|
17 |
|
$this->addRules($rule); |
27
|
17 |
|
} |
28
|
|
|
|
29
|
6 |
|
public function setName($name) |
30
|
|
|
{ |
31
|
6 |
|
$parentName = $this->getName(); |
32
|
6 |
|
foreach ($this->rules as $rule) { |
33
|
5 |
|
$ruleName = $rule->getName(); |
34
|
5 |
|
if ($ruleName && $parentName !== $ruleName) { |
35
|
2 |
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
$rule->setName($name); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
return parent::setName($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
13 |
|
public function addRule($validator, $arguments = []) |
45
|
|
|
{ |
46
|
13 |
|
if (!$validator instanceof Validatable) { |
47
|
1 |
|
$this->appendRule(Validator::buildRule($validator, $arguments)); |
48
|
|
|
} else { |
49
|
12 |
|
$this->appendRule($validator); |
50
|
|
|
} |
51
|
|
|
|
52
|
13 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function removeRules(): void |
56
|
|
|
{ |
57
|
1 |
|
$this->rules = []; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
17 |
|
public function addRules(array $validators) |
61
|
|
|
{ |
62
|
17 |
|
foreach ($validators as $key => $spec) { |
63
|
2 |
|
if ($spec instanceof Validatable) { |
64
|
1 |
|
$this->appendRule($spec); |
65
|
1 |
|
} elseif (is_numeric($key) && is_array($spec)) { |
66
|
|
|
$this->addRules($spec); |
67
|
1 |
|
} elseif (is_array($spec)) { |
68
|
1 |
|
$this->addRule($key, $spec); |
69
|
|
|
} else { |
70
|
2 |
|
$this->addRule($spec); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
17 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
public function getRules() |
78
|
|
|
{ |
79
|
4 |
|
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
|
14 |
|
protected function appendRule(Validatable $validator): void |
104
|
|
|
{ |
105
|
14 |
|
if (!$validator->getName() && $this->getName()) { |
106
|
2 |
|
$validator->setName($this->getName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
14 |
|
$this->rules[spl_object_hash($validator)] = $validator; |
110
|
14 |
|
} |
111
|
|
|
|
112
|
|
|
protected function validateRules($input) |
113
|
|
|
{ |
114
|
|
|
$validators = $this->getRules(); |
115
|
|
|
$exceptions = []; |
116
|
|
|
foreach ($validators as $v) { |
117
|
|
|
try { |
118
|
|
|
$v->assert($input); |
119
|
|
|
} catch (ValidationException $e) { |
120
|
|
|
$exceptions[] = $e; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $exceptions; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|