Completed
Push — master ( 419843...f5da21 )
by Anton
14s
created

ValidatorForm::validateItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
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 2
dl 0
loc 11
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
15
/**
16
 * Validator Builder
17
 *
18
 * @package  Bluz\Validator
19
 * @author   Anton Shevchuk
20
 */
21
class ValidatorForm
22
{
23
    /**
24
     * Stack of validators
25
     *
26
     *   ['foo'] => ValidatorChain
27
     *   ['bar'] => ValidatorChain
28
     *
29
     * @var ValidatorChain[]
30
     */
31
    protected $validators = [];
32
33
    /**
34
     * list of validation errors
35
     *
36
     *   ['foo'] => "some field error"
37
     *   ['bar'] => "some field error"
38
     *
39
     * @var array
40
     */
41
    protected $errors = [];
42
43
    /**
44
     * Add chain to form
45
     *
46
     * @param string      $name
47
     *
48
     * @return ValidatorChain
49
     */
50 5
    public function add($name) : ValidatorChain
51
    {
52 5
        $this->validators[$name] = $this->validators[$name] ?? Validator::create();
53 5
        return $this->validators[$name];
54
    }
55
56
    /**
57
     * Validate chain of rules
58
     *
59
     * @param  array $input
60
     *
61
     * @return bool
62
     */
63 5
    public function validate($input) : bool
64
    {
65 5
        $this->resetErrors();
66
67
        // run chains
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
    protected function validateItem($key, $value): bool
84
    {
85
        // run validators chain
86 5
        $result = $this->validators[$key]->validate($value);
87
88 5
        if (!$result) {
89 3
            $this->setError($key, $this->validators[$key]->getError());
90
        }
91
92 5
        return $result;
93
    }
94
95
    /**
96
     * Assert
97
     *
98
     * @param  mixed $input
99
     *
100
     * @return bool
101
     * @throws ValidatorException
102
     */
103 5
    public function assert($input)
104
    {
105 5
        if (!$this->validate($input)) {
106 3
            $exception = new ValidatorException();
107 3
            $exception->setErrors($this->getErrors());
108 3
            throw $exception;
109
        }
110 2
        return true;
111
    }
112
113
    /**
114
     * Add Error by field name
115
     *
116
     * @param  string $name
117
     * @param  string $message
118
     *
119
     * @return void
120
     */
121 3
    protected function setError($name, $message)
122
    {
123 3
        $this->errors[$name] = $message;
124 3
    }
125
126
    /**
127
     * Reset errors
128
     *
129
     * @return void
130
     */
131 5
    protected function resetErrors()
132
    {
133 5
        $this->errors = [];
134 5
    }
135
136
    /**
137
     * Get errors
138
     *
139
     * @return array
140
     */
141 3
    public function getErrors() : array
142
    {
143 3
        return $this->errors;
144
    }
145
146
    /**
147
     * Has errors?
148
     *
149
     * @return bool
150
     */
151 5
    public function hasErrors() : bool
152
    {
153 5
        return (bool)count($this->errors);
154
    }
155
}
156