Passed
Push — master ( 20faea...42b488 )
by Lee
01:26
created

ValidatorChain   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 89
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setErrorCode() 0 5 1
A addValidator() 0 5 1
A setErrorMessage() 0 5 1
A isValid() 0 16 3
A reset() 0 7 1
A __call() 0 15 3
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
/**
6
 * Class ValidatorChain
7
 *
8
 * @package Lavibi\Popoya
9
 *
10
 * @method ValidatorChain sameAs(mixed $value)
11
 * @method ValidatorChain notSameAs(mixed $value)
12
 */
13
class ValidatorChain extends AbstractValidator
14
{
15
    /**
16
     * @var ValidatorInterface[]
17
     */
18
    protected $validators = [];
19
20
    /**
21
     * List of special methods of validators to set option value.
22
     * Each method belongs only validator.
23
     * Use as shortcut for addValidator method
24
     *
25
     * @var array
26
     */
27
    protected $setOptionMethods = [
28
        'sameAs' => '\Lavibi\Popoya\Same',
29
        'notSameAs' => '\Lavibi\Popoya\NotSame'
30
    ];
31
32
    /**
33
     *
34
     * @param $name
35
     * @param $arguments
36
     *
37
     * @return $this
38
     */
39
    public function __call($name, $arguments)
40
    {
41
        if (!isset($this->setOptionMethods[$name])) {
42
            throw new \InvalidArgumentException('Set option method ' . $name . ' is not support');
43
        }
44
45
        $validatorClass = $this->setOptionMethods[$name];
46
47
        if (!isset($this->validators[$validatorClass])) {
48
            $this->validators[$validatorClass] = new $validatorClass();
49
        }
50
51
        call_user_func_array([$this->validators[$validatorClass], $name], $arguments);
52
53
        return $this;
54
    }
55
56 4
    public function addValidator(ValidatorInterface $validator)
57
    {
58 4
        $this->validators[get_class($validator)] = $validator;
59
60 4
        return $this;
61
    }
62
63 4
    public function reset()
64
    {
65 4
        $this->validators = [];
66
67 4
        parent::reset();
68
69 4
        return $this;
70
    }
71
72 4
    public function isValid($value)
73
    {
74 4
        $this->value = $this->standardValue = $value;
75
76 4
        foreach ($this->validators as $validator) {
77 4
            $result = $validator->isValid($this->standardValue);
78
79 3
            if (!$result) {
80 1
                $this->setErrorCode($validator->getMessageCode())->setErrorMessage($validator->getMessage());
81 1
                return false;
82
            }
83
84 2
            $this->standardValue = $validator->getStandardValue();
85
        }
86
87 1
        return true;
88
    }
89
90 1
    protected function setErrorMessage($message)
91
    {
92 1
        $this->message = $message;
93
94 1
        return $this;
95
    }
96
97 1
    protected function setErrorCode($code)
98
    {
99 1
        $this->messageCode = $code;
100
101 1
        return $this;
102
    }
103
}
104