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