ValidatorCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 35
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addValidator() 0 5 1
A validate() 0 12 3
A getMessages() 0 4 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