|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Validator\Rules; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Wandu\Validator\Exception\InvalidValueException; |
|
6
|
|
|
use function Wandu\Validator\validator; |
|
7
|
|
|
|
|
8
|
|
|
class CollectionValidator extends ValidatorAbstract |
|
9
|
|
|
{ |
|
10
|
|
|
const ERROR_TYPE = 'collection'; |
|
11
|
|
|
|
|
12
|
|
|
/** @var \Wandu\Validator\Contracts\ValidatorInterface */ |
|
13
|
|
|
protected $validator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param mixed $rule |
|
17
|
|
|
*/ |
|
18
|
6 |
|
public function __construct($rule = null) |
|
19
|
|
|
{ |
|
20
|
6 |
|
if ($rule) { |
|
21
|
5 |
|
$this->validator = validator()->from($rule); |
|
22
|
5 |
|
} |
|
23
|
6 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
6 |
|
function test($items) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
6 |
|
return is_array($items) || $items instanceof Traversable; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function assert($items) |
|
37
|
|
|
{ |
|
38
|
4 |
|
if (!isset($items)) $items = []; |
|
39
|
|
|
/** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */ |
|
40
|
4 |
|
$exceptions = []; |
|
41
|
4 |
|
if (!$this->test($items)) { |
|
42
|
2 |
|
throw $this->createException(); |
|
43
|
|
|
} |
|
44
|
4 |
|
if ($this->validator) { |
|
45
|
4 |
|
foreach ($items as $key => $item) { |
|
46
|
4 |
|
if (!is_int($key)) { |
|
47
|
|
|
$exceptions['.'] = $this->createException(); |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
try { |
|
51
|
4 |
|
$this->validator->assert($item); |
|
52
|
4 |
|
} catch (InvalidValueException $e) { |
|
53
|
3 |
|
$exceptions[$key] = $e; |
|
54
|
|
|
} |
|
55
|
4 |
|
} |
|
56
|
4 |
|
} else { |
|
57
|
|
|
foreach ($items as $key => $item) { |
|
58
|
|
|
if (!is_int($key)) { |
|
59
|
|
|
$exceptions['.'] = $this->createException(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
4 |
|
if (count($exceptions)) { |
|
64
|
3 |
|
throw InvalidValueException::merge($exceptions); |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function validate($items) |
|
72
|
|
|
{ |
|
73
|
2 |
|
if (!isset($items)) $items = []; |
|
74
|
2 |
|
if (!$this->test($items)) return false; |
|
75
|
|
|
|
|
76
|
2 |
|
if ($this->validator) { |
|
77
|
1 |
|
foreach ($items as $key => $item) { |
|
78
|
1 |
|
if (!is_int($key)) return false; |
|
79
|
1 |
|
if (!$this->validator->validate($item)) return false; |
|
80
|
1 |
|
} |
|
81
|
1 |
|
} else { |
|
82
|
1 |
|
foreach ($items as $key => $item) { |
|
83
|
1 |
|
if (!is_int($key)) return false; |
|
84
|
1 |
|
} |
|
85
|
|
|
} |
|
86
|
2 |
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.