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
|
|
|
public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null) |
23
|
|
|
{ |
24
|
|
|
$this->itemValidator = $itemValidator; |
25
|
|
|
$this->keyValidator = $keyValidator; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function assert($input) |
29
|
|
|
{ |
30
|
|
|
$exceptions = []; |
31
|
|
|
|
32
|
|
|
if (!parent::validate($input)) { |
|
|
|
|
33
|
|
|
throw $this->reportError($input); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($input as $key => $item) { |
37
|
|
|
if (isset($this->itemValidator)) { |
38
|
|
|
try { |
39
|
|
|
$this->itemValidator->assert($item); |
40
|
|
|
} catch (ValidationException $e) { |
41
|
|
|
$exceptions[] = $e; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (isset($this->keyValidator)) { |
46
|
|
|
try { |
47
|
|
|
$this->keyValidator->assert($key); |
48
|
|
|
} catch (ValidationException $e) { |
49
|
|
|
$exceptions[] = $e; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!empty($exceptions)) { |
55
|
|
|
throw $this->reportError($input)->setRelated($exceptions); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function check($input) |
62
|
|
|
{ |
63
|
|
|
if (!parent::validate($input)) { |
|
|
|
|
64
|
|
|
throw $this->reportError($input); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($input as $key => $item) { |
68
|
|
|
if (isset($this->itemValidator)) { |
69
|
|
|
$this->itemValidator->check($item); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (isset($this->keyValidator)) { |
73
|
|
|
$this->keyValidator->check($key); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function validate($input) |
81
|
|
|
{ |
82
|
|
|
if (!parent::validate($input)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($input as $key => $item) { |
87
|
|
|
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
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 theSon
calls the wrong method in the parent class.