Completed
Push — master ( 6f2bb4...6400d0 )
by Anton
11s
created

ValidatorForm::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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