Completed
Pull Request — master (#452)
by Anton
14:21
created

ValidatorForm   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A get() 0 4 1
A validate() 0 15 4
A validateItem() 0 11 2
A assert() 0 6 2
A __invoke() 0 4 1
A getErrors() 0 4 1
A hasErrors() 0 4 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator;
12
13
use Bluz\Validator\Exception\ValidatorException;
14
15
/**
16
 * Validator Builder
17
 *
18
 * @package  Bluz\Validator
19
 * @author   Anton Shevchuk
20
 */
21
class ValidatorForm implements ValidatorInterface
22
{
23
    /**
24
     * Stack of validators
25
     *
26
     *   ['foo'] => ValidatorChain
27
     *   ['bar'] => ValidatorChain
28
     *
29
     * @var ValidatorChain[]
30
     */
31
    protected $validators = [];
32
33
    /**
34
     * Exception with list of validation errors
35
     *
36
     *   ['foo'] => "some field error"
37
     *   ['bar'] => "some field error"
38
     *
39
     * @var ValidatorException
40
     */
41
    protected $exception;
42
43
    /**
44
     * Add chain to form
45
     *
46
     * @param string      $name
47
     *
48
     * @return ValidatorChain
49
     */
50 6
    public function add($name) : ValidatorChain
51
    {
52 6
        $this->validators[$name] = $this->validators[$name] ?? Validator::create();
53 6
        return $this->validators[$name];
54
    }
55
56
    /**
57
     * Get chain to form
58
     *
59
     * @param string      $name
60
     *
61
     * @return ValidatorChain
62
     */
63 6
    public function get($name) : ValidatorChain
64
    {
65 6
        return $this->add($name);
66
    }
67
68 6
    /**
69
     * Validate chain of rules
70 6
     *
71 2
     * @param  array $input
72
     *
73 6
     * @return bool
74
     */
75
    public function validate($input) : bool
76 6
    {
77
        $this->exception = new ValidatorException();
78
79
        // run chains
80
        foreach ($this->validators as $key => $validators) {
81
            // skip validation for non required elements
82
            if (!isset($input[$key]) && !$validators->isRequired()) {
83
                continue;
84
            }
85
            $this->validateItem($key, $input[$key] ?? null);
86
        }
87 6
88
        return !$this->hasErrors();
89
    }
90 6
91
    /**
92 6
     * Validate chain of rules for single item
93 4
     *
94
     * @param  string $key
95
     * @param  mixed  $value
96 6
     *
97
     * @return bool
98
     */
99
    protected function validateItem($key, $value): bool
100
    {
101
        // run validators chain
102
        $result = $this->validators[$key]->validate($value);
103
104
        if (!$result) {
105
            $this->exception->setError($key, $this->validators[$key]->getError());
106
        }
107 6
108
        return $result;
109 6
    }
110 4
111
    /**
112 2
     * Assert
113
     *
114
     * @param  mixed $input
115
     *
116
     * @return void
117
     * @throws ValidatorException
118
     */
119
    public function assert($input)
120
    {
121
        if (!$this->validate($input)) {
122
            throw $this->exception;
123
        }
124
    }
125
126
    /**
127 2
     * @inheritdoc
128
     */
129 2
    public function __invoke($input) : bool
130
    {
131
        return $this->validate($input);
132
    }
133
134
    /**
135
     * Get errors
136
     *
137 6
     * @return array
138
     */
139 6
    public function getErrors() : array
140
    {
141
        return $this->exception->getErrors();
142
    }
143
144
    /**
145
     * Has errors?
146
     *
147
     * @return bool
148
     */
149
    public function hasErrors() : bool
150
    {
151
        return $this->exception->hasErrors();
152
    }
153
}
154