|
1
|
|
|
<?php |
|
2
|
|
|
namespace Respect\Validation\Rules; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Respect\Validation\Validatable; |
|
6
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
|
7
|
|
|
|
|
8
|
|
|
class Each extends AbstractRule |
|
9
|
|
|
{ |
|
10
|
|
|
public $itemValidator; |
|
11
|
|
|
public $keyValidator; |
|
12
|
|
|
|
|
13
|
8 |
|
public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null) |
|
14
|
|
|
{ |
|
15
|
8 |
|
$this->itemValidator = $itemValidator; |
|
16
|
8 |
|
$this->keyValidator = $keyValidator; |
|
17
|
8 |
|
} |
|
18
|
|
|
|
|
19
|
6 |
|
public function assert($input) |
|
20
|
|
|
{ |
|
21
|
6 |
|
$exceptions = array(); |
|
22
|
|
|
|
|
23
|
6 |
|
if (!is_array($input) || $input instanceof Traversable) { |
|
24
|
1 |
|
throw $this->reportError($input); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
5 |
|
if (empty($input)) { |
|
28
|
|
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
5 |
|
foreach ($input as $key => $item) { |
|
32
|
5 |
|
if (isset($this->itemValidator)) { |
|
33
|
|
|
try { |
|
34
|
3 |
|
$this->itemValidator->assert($item); |
|
35
|
3 |
|
} catch (ValidationException $e) { |
|
36
|
1 |
|
$exceptions[] = $e; |
|
37
|
|
|
} |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
if (isset($this->keyValidator)) { |
|
41
|
|
|
try { |
|
42
|
3 |
|
$this->keyValidator->assert($key); |
|
43
|
3 |
|
} catch (ValidationException $e) { |
|
44
|
1 |
|
$exceptions[] = $e; |
|
45
|
|
|
} |
|
46
|
3 |
|
} |
|
47
|
5 |
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
if (!empty($exceptions)) { |
|
50
|
2 |
|
throw $this->reportError($input)->setRelated($exceptions); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function check($input) |
|
57
|
|
|
{ |
|
58
|
3 |
|
if (empty($input)) { |
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
if (!is_array($input) || $input instanceof Traversable) { |
|
63
|
|
|
throw $this->reportError($input); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
foreach ($input as $key => $item) { |
|
67
|
3 |
|
if (isset($this->itemValidator)) { |
|
68
|
2 |
|
$this->itemValidator->check($item); |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
if (isset($this->keyValidator)) { |
|
72
|
2 |
|
$this->keyValidator->check($key); |
|
73
|
2 |
|
} |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
public function validate($input) |
|
80
|
|
|
{ |
|
81
|
5 |
|
if (!is_array($input) || $input instanceof Traversable) { |
|
82
|
1 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
if (empty($input)) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
foreach ($input as $key => $item) { |
|
90
|
4 |
|
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) { |
|
91
|
1 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
3 |
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|