ValidatorForm::getErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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