Completed
Push — master ( 476779...f1dfa3 )
by Lee
01:28
created

ValidatorChain::setErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
class ValidatorChain extends AbstractValidator
6
{
7
    /**
8
     * @var ValidatorInterface[]
9
     */
10
    protected $validators = [];
11
12 4
    public function addValidator(ValidatorInterface $validator)
13
    {
14 4
        $this->validators[] = $validator;
15
16 4
        return $this;
17
    }
18
19 4
    public function reset()
20
    {
21 4
        $this->validators = [];
22
23 4
        parent::reset();
24
25 4
        return $this;
26
    }
27
28 4
    public function isValid($value)
29
    {
30 4
        $this->value = $this->standardValue = $value;
31
32 4
        foreach ($this->validators as $validator) {
33 4
            $result = $validator->isValid($this->standardValue);
34
35 3
            if (!$result) {
36 1
                $this->setErrorCode($validator->getMessageCode())->setErrorMessage($validator->getMessage());
37 1
                return false;
38
            }
39
40 2
            $this->standardValue = $validator->getStandardValue();
41
        }
42
43 1
        return true;
44
    }
45
46 1
    protected function setErrorMessage($message)
47
    {
48 1
        $this->message = $message;
49
50 1
        return $this;
51
    }
52
53 1
    protected function setErrorCode($code)
54
    {
55 1
        $this->messageCode = $code;
56
57 1
        return $this;
58
    }
59
}
60