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

CollectionValidator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.72%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 36
cts 43
cp 0.8372
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A test() 0 4 2
D assert() 0 31 10
B validate() 0 17 9
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