Test Failed
Pull Request — master (#19)
by Flo
04:39
created

ValidatorChain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 4 1
A validate() 0 19 4
1
<?php
2
/**
3
 * Class ValidatorChain | ValidatorChain.php
4
 * @package Form\Validator
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Form\Validator;
8
9
use Faulancer\Form\Type\AbstractType;
10
11
/**
12
 * Class ValidatorChain
13
 */
14
class ValidatorChain
15
{
16
17
    /** @var AbstractValidator[] */
18
    protected $validators = [];
19
20
    protected $messages = [];
21
22
    /** @var AbstractType|null */
23
    protected $field = null;
24
25
    /**
26
     * ValidatorChain constructor.
27
     * @param AbstractType $field
28
     * @codeCoverageIgnore
29
     */
30
    public function __construct(AbstractType $field)
31
    {
32
        $this->field = $field;
33
    }
34
35
    /**
36
     * @param AbstractValidator $validator
37
     * @codeCoverageIgnore
38
     */
39
    public function add(AbstractValidator $validator)
40
    {
41
        $this->validators[] = $validator;
42
    }
43
44
    /**
45
     * @return boolean
46
     * @codeCoverageIgnore
47
     */
48
    public function validate() :bool
49
    {
50
        foreach ($this->validators as $validator) {
51
52
            $result = $validator->process($this->field->getValue());
53
54
            if (!$result) {
55
                $this->messages[] = $validator->getMessage();
56
            }
57
58
        }
59
60
        if (!empty($this->messages)) {
61
            $this->validators[0]->getField()->setErrorMessages($this->messages);
62
            return false;
63
        }
64
65
        return true;
66
    }
67
68
}