Completed
Push — master ( 9ee4de...ccfc27 )
by Anton
18s
created

ValidatorChain::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
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\Rule\RuleInterface;
15
16
/**
17
 * Chain of Validators
18
 *
19
 * Chain can consists one or more validation rules
20
 *
21
 * @package  Bluz\Validator
22
 * @author   Anton Shevchuk
23
 *
24
 * @method ValidatorChain alpha($additionalCharacters = '')
25
 * @method ValidatorChain alphaNumeric($additionalCharacters = '')
26
 * @method ValidatorChain array($callback)
27
 * @method ValidatorChain between($min, $max)
28
 * @method ValidatorChain betweenInclusive($min, $max)
29
 * @method ValidatorChain condition($condition)
30
 * @method ValidatorChain contains($containsValue)
31
 * @method ValidatorChain containsStrict($containsValue)
32
 * @method ValidatorChain countryCode()
33
 * @method ValidatorChain creditCard()
34
 * @method ValidatorChain date($format)
35
 * @method ValidatorChain domain($checkDns = false)
36
 * @method ValidatorChain email($checkDns = false)
37
 * @method ValidatorChain equals($compareTo)
38
 * @method ValidatorChain equalsStrict($compareTo)
39
 * @method ValidatorChain float()
40
 * @method ValidatorChain in($haystack)
41
 * @method ValidatorChain inStrict($haystack)
42
 * @method ValidatorChain integer()
43
 * @method ValidatorChain ip($options = null)
44
 * @method ValidatorChain json()
45
 * @method ValidatorChain latin($additionalCharacters = '')
46
 * @method ValidatorChain latinNumeric($additionalCharacters = '')
47
 * @method ValidatorChain length($min = null, $max = null)
48
 * @method ValidatorChain less($maxValue)
49
 * @method ValidatorChain lessOrEqual($maxValue)
50
 * @method ValidatorChain more($minValue)
51
 * @method ValidatorChain moreOrEqual($minValue)
52
 * @method ValidatorChain notEmpty()
53
 * @method ValidatorChain noWhitespace()
54
 * @method ValidatorChain numeric()
55
 * @method ValidatorChain required()
56
 * @method ValidatorChain slug()
57
 * @method ValidatorChain string()
58
 */
59
class ValidatorChain
60
{
61
    /**
62
     * @var string description of rules chain
63
     */
64
    protected $description;
65
66
    /**
67
     * @var RuleInterface[] list of validation rules
68
     */
69
    protected $rules = [];
70
71
    /**
72
     * @var string Error message
73
     */
74
    protected $error;
75
76
    /**
77
     * Magic call for create new rule
78
     *
79
     * @param  string $ruleName
80
     * @param  array  $arguments
81
     *
82
     * @return ValidatorChain
83
     * @throws Exception\ComponentException
84
     */
85 9
    public function __call($ruleName, $arguments) : ValidatorChain
86
    {
87 9
        $this->addRule(Validator::rule($ruleName, $arguments));
88 9
        return $this;
89
    }
90
91
    /**
92
     * Add Rule to ValidatorChain
93
     *
94
     * @param RuleInterface $rule
95
     *
96
     * @return ValidatorChain
97
     */
98 16
    public function addRule($rule) : ValidatorChain
99
    {
100 16
        $this->rules[] = $rule;
101 16
        return $this;
102
    }
103
104
    /**
105
     * Add Callback Rule to ValidatorChain
106
     *
107
     * @param mixed       $callable
108
     * @param string|null $description
109
     *
110
     * @return ValidatorChain
111
     */
112 6
    public function callback($callable, $description = null) : ValidatorChain
113
    {
114 6
        $rule = Validator::rule('callback', [$callable]);
115 6
        if (null !== $description) {
116 2
            $rule->setDescription($description);
117
        }
118 6
        $this->addRule($rule);
119 6
        return $this;
120
    }
121
122
    /**
123
     * Add Regexp Rule to ValidatorChain
124
     *
125
     * @param string      $expression
126
     * @param string|null $description
127
     *
128
     * @return ValidatorChain
129
     */
130 1
    public function regexp($expression, $description = null) : ValidatorChain
131
    {
132 1
        $rule = Validator::rule('regexp', [$expression]);
133 1
        if (null !== $description) {
134 1
            $rule->setDescription($description);
135
        }
136 1
        $this->addRule($rule);
137 1
        return $this;
138
    }
139
140
    /**
141
     * Validate chain of rules
142
     *
143
     * @param mixed $input
144
     *
145
     * @return bool
146
     */
147 12
    public function validate($input) : bool
148
    {
149 12
        $this->error = null; // clean
150 12
        foreach ($this->rules as $rule) {
151 12
            if (!$rule->validate($input)) {
152 8
                $this->setError($rule->getDescription());
153 12
                return false;
154
            }
155
        }
156 5
        return true;
157
    }
158
159
    /**
160
     * Assert
161
     *
162
     * @param  mixed $input
163
     *
164
     * @throws ValidatorException
165
     */
166 3
    public function assert($input)
167
    {
168 3
        if (!$this->validate($input)) {
169 3
            throw new ValidatorException($this->getError());
170
        }
171
    }
172
173
    /**
174
     * Get required flag
175
     *
176
     * @return bool
177
     */
178
    public function isRequired() : bool
179
    {
180
        return array_key_exists('required', $this->rules);
181
    }
182
183
    /**
184
     * Get error message
185
     *
186
     * @return null|string
187
     */
188 7
    public function getError()
189
    {
190 7
        if ($this->description) {
191 3
            return $this->description;
192
        }
193 4
        return $this->error;
194
    }
195
196
    /**
197
     * Set error message for replace text from rule
198
     *
199
     * @param  string $message
200
     *
201
     * @return ValidatorChain
202
     */
203 8
    protected function setError($message) : ValidatorChain
204
    {
205 8
        $this->error = $message;
206 8
        return $this;
207
    }
208
209
    /**
210
     * Get validation description
211
     *
212
     * @return array
213
     */
214 5
    public function getDescription() : array
215
    {
216 5
        if ($this->description) {
217 2
            return [$this->description];
218
        }
219
220 4
        return array_map('strval', $this->rules);
221
    }
222
223
    /**
224
     * Set validation description
225
     *
226
     * @param  string $message
227
     *
228
     * @return ValidatorChain
229
     */
230 4
    public function setDescription($message) : ValidatorChain
231
    {
232 4
        $this->description = $message;
233 4
        return $this;
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239 1
    public function __invoke($input): bool
240
    {
241 1
        return $this->validate($input);
242
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247 1
    public function __toString()
248
    {
249 1
        return implode("\n", $this->getDescription());
250
    }
251
}
252