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