1 | <?php |
||
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 | 12 | public function __construct($rule = null) |
|
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 12 | function test($items) |
|
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | 8 | public function assert($items) |
|
37 | { |
||
38 | 8 | if (!isset($items)) $items = []; |
|
39 | /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */ |
||
40 | 8 | $exceptions = []; |
|
41 | 8 | if (!$this->test($items)) { |
|
42 | 4 | throw $this->createException(); |
|
43 | } |
||
44 | 8 | if ($this->validator) { |
|
45 | 8 | foreach ($items as $key => $item) { |
|
46 | 8 | if (!is_int($key)) { |
|
47 | 2 | $exceptions['.'] = $this->createException(); |
|
48 | continue; |
||
49 | } |
||
50 | try { |
||
51 | 8 | $this->validator->assert($item); |
|
52 | 8 | } catch (InvalidValueException $e) { |
|
53 | 8 | $exceptions[$key] = $e; |
|
54 | } |
||
55 | 8 | } |
|
56 | 8 | } else { |
|
57 | foreach ($items as $key => $item) { |
||
58 | if (!is_int($key)) { |
||
59 | $exceptions['.'] = $this->createException(); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | 8 | if (count($exceptions)) { |
|
64 | 8 | throw InvalidValueException::merge($exceptions); |
|
65 | } |
||
66 | 8 | } |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 4 | public function validate($items) |
|
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.