Completed
Pull Request — master (#430)
by Anton
04:30
created

ValidatorForm::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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
use Bluz\Validator\Exception\ValidatorFormException;
15
16
/**
17
 * Validator Builder
18
 *
19
 * @package  Bluz\Validator
20
 * @author   Anton Shevchuk
21
 */
22
class ValidatorForm
23
{
24
    /**
25
     * Stack of validators
26
     *
27
     *   ['foo'] => ValidatorChain
28
     *   ['bar'] => ValidatorChain
29
     *
30
     * @var ValidatorChain[]
31
     */
32
    protected $validators = [];
33
34
    /**
35
     * list of validation errors
36
     *
37
     *   ['foo'] => "some field error"
38
     *   ['bar'] => "some field error"
39
     *
40
     * @var array
41
     */
42
    protected $errors = [];
43
44
    /**
45
     * Add chain to form
46
     *
47
     * @param string      $name
48
     *
49
     * @return ValidatorChain
50
     */
51 5
    public function add($name) : ValidatorChain
52
    {
53 5
        $this->validators[$name] = $this->validators[$name] ?? Validator::create();
54 5
        return $this->validators[$name];
55
    }
56
57
    /**
58
     * Validate chain of rules
59
     *
60
     * @param  array $input
61
     *
62
     * @return bool
63
     */
64 5
    public function validate($input) : bool
65
    {
66 5
        $this->resetErrors();
67
68 5
        foreach ($this->validators as $key => $validators) {
69 5
            $this->validateItem($key, $input[$key] ?? null);
70
        }
71
72 5
        return !$this->hasErrors();
73
    }
74
75
    /**
76
     * Validate chain of rules for single item
77
     *
78
     * @param  string $key
79
     * @param  mixed  $value
80
     *
81
     * @return bool
82
     */
83 5
    public function validateItem($key, $value): bool
84
    {
85 5
        $validatorsChain = $this->validators[$key] ?? null;
86
87
        // w/out any rules element is valid
88 5
        if (is_null($validatorsChain)) {
89
            return true;
90
        }
91
92
        // run validators chain
93 5
        if ($validatorsChain->validate($value)) {
94 3
            return true;
95
        }
96
97 3
        $this->setError($key, $validatorsChain->getError());
98 3
        return false;
99
    }
100
101
    /**
102
     * Assert
103
     *
104
     * @param  mixed $input
105
     *
106
     * @return bool
107
     * @throws ValidatorException
108
     */
109 5
    public function assert($input)
110
    {
111 5
        if (!$this->validate($input)) {
112 3
            $exception = new ValidatorFormException();
113 3
            $exception->setFromArray($this->getErrors());
114 3
            throw $exception;
115
        }
116 2
        return true;
117
    }
118
119
    /**
120
     * Add Error by field name
121
     *
122
     * @param  string $name
123
     * @param  string $message
124
     *
125
     * @return void
126
     */
127 3
    protected function setError($name, $message)
128
    {
129 3
        $this->errors[$name] = $message;
130 3
    }
131
132
    /**
133
     * Reset errors
134
     *
135
     * @return void
136
     */
137 5
    protected function resetErrors()
138
    {
139 5
        $this->errors = [];
140 5
    }
141
142
    /**
143
     * Get errors
144
     *
145
     * @return array
146
     */
147 3
    public function getErrors() : array
148
    {
149 3
        return $this->errors;
150
    }
151
152
    /**
153
     * Has errors?
154
     *
155
     * @return bool
156
     */
157 5
    public function hasErrors() : bool
158
    {
159 5
        return (bool)count($this->errors);
160
    }
161
}
162