Completed
Push — master ( 3e9a7d...862f35 )
by Anton
13s
created

ValidatorForm::hasErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * Validate chain of rules
58
     *
59
     * @param  array $input
60
     *
61
     * @return bool
62
     */
63 6
    public function validate($input) : bool
64
    {
65 6
        $this->exception = new ValidatorException();
66
67
        // run chains
68 6
        foreach ($this->validators as $key => $validators) {
69
            // skip validation for non required elements
70 6
            if (!isset($input[$key]) && !$validators->isRequired()) {
71 2
                continue;
72
            }
73 6
            $this->validateItem($key, $input[$key] ?? null);
74
        }
75
76 6
        return !$this->hasErrors();
77
    }
78
79
    /**
80
     * Validate chain of rules for single item
81
     *
82
     * @param  string $key
83
     * @param  mixed  $value
84
     *
85
     * @return bool
86
     */
87 6
    protected function validateItem($key, $value): bool
88
    {
89
        // run validators chain
90 6
        $result = $this->validators[$key]->validate($value);
91
92 6
        if (!$result) {
93 4
            $this->exception->setError($key, $this->validators[$key]->getError());
94
        }
95
96 6
        return $result;
97
    }
98
99
    /**
100
     * Assert
101
     *
102
     * @param  mixed $input
103
     *
104
     * @return void
105
     * @throws ValidatorException
106
     */
107 6
    public function assert($input)
108
    {
109 6
        if (!$this->validate($input)) {
110 4
            throw $this->exception;
111
        }
112 2
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function __invoke($input) : bool
118
    {
119
        return $this->validate($input);
120
    }
121
122
    /**
123
     * Get errors
124
     *
125
     * @return array
126
     */
127 2
    public function getErrors() : array
128
    {
129 2
        return $this->exception->getErrors();
130
    }
131
132
    /**
133
     * Has errors?
134
     *
135
     * @return bool
136
     */
137 6
    public function hasErrors() : bool
138
    {
139 6
        return $this->exception->hasErrors();
140
    }
141
}
142