Completed
Pull Request — master (#439)
by Anton
27:34
created

ValidatorForm::assert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
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\ValidatorFormException;
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 ValidatorFormException
40
     */
41
    protected $exception;
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->exception = new ValidatorFormException();
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->exception->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 void
101
     * @throws ValidatorFormException
102
     */
103 5
    public function assert($input)
104
    {
105 5
        if (!$this->validate($input)) {
106 3
            throw $this->exception;
107
        }
108 2
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function __invoke($input) : bool
114
    {
115
        return $this->validate($input);
116
    }
117
118
    /**
119
     * Get errors
120
     *
121
     * @return array
122
     */
123 1
    public function getErrors() : array
124
    {
125 1
        return $this->exception->getErrors();
126
    }
127
128
    /**
129
     * Has errors?
130
     *
131
     * @return bool
132
     */
133 5
    public function hasErrors() : bool
134
    {
135 5
        return $this->exception->hasErrors();
136
    }
137
}
138