ValidatorCollection::addValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace indigerd\scenarios\validation\collection;
4
5
use indigerd\scenarios\validation\validator\ValidatorInterface;
6
7
class ValidatorCollection
8
{
9
    protected $validators = [];
10
11
    protected $messages = [];
12
13 3
    public function __construct(ValidatorInterface ...$validators)
14
    {
15 3
        $this->validators = $validators;
16 3
    }
17
18 2
    public function addValidator(ValidatorInterface $validator) : self
19
    {
20 2
        $this->validators[] = $validator;
21 2
        return $this;
22
    }
23
24 2
    public function validate($value, array $context = []) : bool
25
    {
26 2
        $valid = true;
27 2
        $this->messages = [];
28 2
        foreach ($this->validators as $validator) {
29 2
            if (!$validator->validate($value, $context)) {
30 1
                $valid = false;
31 2
                $this->messages[] = $validator->getMessage();
32
            }
33
        }
34 2
        return $valid;
35
    }
36
37 1
    public function getMessages() : array
38
    {
39 1
        return $this->messages;
40
    }
41
}
42