|
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
|
|
|
|
|
17
|
|
|
class Each extends IterableType |
|
18
|
|
|
{ |
|
19
|
|
|
public $itemValidator; |
|
20
|
|
|
public $keyValidator; |
|
21
|
|
|
|
|
22
|
7 |
|
public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null) |
|
23
|
|
|
{ |
|
24
|
7 |
|
$this->itemValidator = $itemValidator; |
|
25
|
7 |
|
$this->keyValidator = $keyValidator; |
|
26
|
7 |
|
} |
|
27
|
|
|
|
|
28
|
6 |
|
public function assert($input) |
|
29
|
|
|
{ |
|
30
|
6 |
|
$exceptions = []; |
|
31
|
|
|
|
|
32
|
6 |
|
if (!parent::validate($input)) { |
|
|
|
|
|
|
33
|
1 |
|
throw $this->reportError($input); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
5 |
|
foreach ($input as $key => $item) { |
|
37
|
5 |
|
if (isset($this->itemValidator)) { |
|
38
|
|
|
try { |
|
39
|
3 |
|
$this->itemValidator->assert($item); |
|
40
|
3 |
|
} catch (ValidationException $e) { |
|
41
|
1 |
|
$exceptions[] = $e; |
|
42
|
|
|
} |
|
43
|
3 |
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
if (isset($this->keyValidator)) { |
|
46
|
|
|
try { |
|
47
|
3 |
|
$this->keyValidator->assert($key); |
|
48
|
3 |
|
} catch (ValidationException $e) { |
|
49
|
1 |
|
$exceptions[] = $e; |
|
50
|
|
|
} |
|
51
|
3 |
|
} |
|
52
|
5 |
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if (!empty($exceptions)) { |
|
55
|
2 |
|
throw $this->reportError($input)->setRelated($exceptions); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
public function check($input) |
|
62
|
|
|
{ |
|
63
|
4 |
|
if (!parent::validate($input)) { |
|
|
|
|
|
|
64
|
1 |
|
throw $this->reportError($input); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
foreach ($input as $key => $item) { |
|
68
|
3 |
|
if (isset($this->itemValidator)) { |
|
69
|
2 |
|
$this->itemValidator->check($item); |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
if (isset($this->keyValidator)) { |
|
73
|
2 |
|
$this->keyValidator->check($key); |
|
74
|
2 |
|
} |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
11 |
|
public function validate($input) |
|
81
|
|
|
{ |
|
82
|
11 |
|
if (!parent::validate($input)) { |
|
83
|
4 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
7 |
|
foreach ($input as $key => $item) { |
|
87
|
7 |
|
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) { |
|
88
|
1 |
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
6 |
|
if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
5 |
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.