Completed
Pull Request — master (#357)
by Anton
03:23
created

ValidatorBuilder::validateItem()   C

Complexity

Conditions 13
Paths 55

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 13.0056
Metric Value
dl 0
loc 54
ccs 30
cts 31
cp 0.9677
rs 6.7593
cc 13
eloc 27
nc 55
nop 2
crap 13.0056

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
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 ValidatorBuilder
23
{
24
    /**
25
     * Stack of validators
26
     *
27
     *   ['foo'] => [Validator, ...]
28
     *   ['bar'] => [Validator, ...]
29
     *
30
     * @var array
31
     */
32
    protected $validators = array();
33
34
    /**
35
     * @var array list of validation errors
36
     */
37
    protected $errors = array();
38
39
    /**
40
     * Add validator to builder
41
     * @param string    $name
42
     * @param Validator ...$validators
43
     * @return ValidatorBuilder
44
     */
45 15
    public function add($name, ...$validators)
46
    {
47 15
        if (isset($this->validators[$name])) {
48 1
            $this->validators[$name] = array_merge($this->validators[$name], $validators);
49 1
        } else {
50 15
            $this->validators[$name] = $validators;
51
        }
52 15
        return $this;
53
    }
54
55
    /**
56
     * Validate chain of rules
57
     *
58
     * @param  array|object $input
59
     * @return bool
60
     */
61 15
    public function validate($input)
62
    {
63 15
        $this->errors = array();
64 15
        $result = true;
65
        // check be validators
66 15
        foreach ($this->validators as $key => $validators) {
67 15
            if (!$this->validateItem($key, $input)) {
68 9
                $result = false;
69 9
            }
70 15
        }
71 15
        return $result;
72
    }
73
74
    /**
75
     * Validate chain of rules for single item
76
     *
77
     * @param  string $key
78
     * @param  array|object $input
79
     * @return bool
80
     */
81 15
    public function validateItem($key, $input)
82
    {
83
        // w/out any rules element is valid
84 15
        if (!isset($this->validators[$key])) {
85
            return true;
86
        }
87
88 15
        $validators = $this->validators[$key];
89
90
        // check be validators
91
        // extract input from ...
92 15
        if (is_array($input) && isset($input[$key])) {
93
            // array
94 12
            $value = $input[$key];
95 15
        } elseif (is_object($input) && isset($input->{$key})) {
96
            // object
97 1
            $value = $input->{$key};
98 1
        } else {
99
            // ... oh, not exists key
100
            // check chains for required
101 4
            $required = false;
102 4
            foreach ($validators as $validator) {
103
                /* @var Validator $validator */
104 4
                if ($validator->isRequired()) {
105 3
                    $required = true;
106 3
                    break;
107
                }
108 4
            }
109
110 4
            if ($required) {
111 3
                $value = '';
112 3
            } else {
113 2
                return true;
114
            }
115
        }
116
117
        // run validators chain
118 15
        foreach ($validators as $validator) {
119
            /* @var Validator $validator */
120 15
            if (!$validator->getName()) {
121
                // setup field name as property name
122 15
                $validator->setName(ucfirst($key));
123 15
            }
124
125 15
            if (!$validator->validate($value)) {
126 9
                if (!isset($this->errors[$key])) {
127 9
                    $this->errors[$key] = array();
128 9
                }
129 9
                $this->errors[$key][] = $validator->getError();
130 9
                return false;
131
            }
132 7
        }
133 6
        return true;
134
    }
135
136
    /**
137
     * Assert
138
     *
139
     * @param  mixed $input
140
     * @return bool
141
     * @throws ValidatorException
142
     */
143 15
    public function assert($input)
144
    {
145 15
        if (!$this->validate($input)) {
146 9
            $exception = new ValidatorException();
147 9
            $exception->setErrors($this->getErrors());
148 9
            throw $exception;
149
        }
150 6
        return true;
151
    }
152
153
    /**
154
     * Get errors
155
     *
156
     * @return array
157
     */
158 9
    public function getErrors()
159
    {
160 9
        return $this->errors;
161
    }
162
}
163