Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

CollectionValidator::test()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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