Completed
Push — master ( 4568b1...9a246b )
by Anton
05:03
created

ValidatorChain::getDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
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\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 implements ValidatorInterface
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 12
    public function __call($ruleName, $arguments) : ValidatorChain
86
    {
87 12
        $this->addRule(Validator::rule($ruleName, $arguments));
88 11
        return $this;
89
    }
90
91
    /**
92
     * Add Rule to ValidatorChain
93
     *
94
     * @param RuleInterface $rule
95
     *
96
     * @return ValidatorChain
97
     */
98 20
    public function addRule($rule) : ValidatorChain
99
    {
100 20
        $this->rules[] = $rule;
101 20
        return $this;
102
    }
103
104
    /**
105
     * Get required flag
106
     *
107
     * @return bool
108
     */
109
    public function isRequired() : bool
110
    {
111
        return array_key_exists('required', $this->rules);
112
    }
113
114
    /**
115
     * Add Callback Rule to ValidatorChain
116
     *
117
     * @param mixed       $callable
118
     * @param string|null $description
119
     *
120
     * @return ValidatorChain
121
     * @throws \Bluz\Validator\Exception\ComponentException
122
     */
123 9
    public function callback($callable, $description = null) : ValidatorChain
124
    {
125 9
        $rule = Validator::rule('callback', [$callable]);
126 9
        if (null !== $description) {
127 3
            $rule->setDescription($description);
128
        }
129 9
        $this->addRule($rule);
130 9
        return $this;
131
    }
132
133
    /**
134
     * Add Regexp Rule to ValidatorChain
135
     *
136
     * @param string      $expression
137
     * @param string|null $description
138
     *
139
     * @return ValidatorChain
140
     * @throws \Bluz\Validator\Exception\ComponentException
141
     */
142 1
    public function regexp($expression, $description = null) : ValidatorChain
143
    {
144 1
        $rule = Validator::rule('regexp', [$expression]);
145 1
        if (null !== $description) {
146 1
            $rule->setDescription($description);
147
        }
148 1
        $this->addRule($rule);
149 1
        return $this;
150
    }
151
152
    /**
153
     * Validate chain of rules
154
     *
155
     * @param mixed $input
156
     *
157
     * @return bool
158
     */
159 14
    public function validate($input) : bool
160
    {
161 14
        $this->error = null; // clean
162 14
        foreach ($this->rules as $rule) {
163 14
            if (!$rule->validate($input)) {
164
                // apply custom description or use description from rule
165 8
                $this->setError($this->description ?? $rule->getDescription());
166 14
                return false;
167
            }
168
        }
169 7
        return true;
170
    }
171
172
    /**
173
     * Assert
174
     *
175
     * @param  mixed $input
176
     *
177
     * @throws ValidatorException
178
     */
179 3
    public function assert($input)
180
    {
181 3
        if (!$this->validate($input)) {
182 3
            throw new ValidatorException($this->getError());
183
        }
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 2
    public function __invoke($input) : bool
190
    {
191 2
        return $this->validate($input);
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197 1
    public function __toString() : string
198
    {
199 1
        return implode("\n", $this->getDescription());
200
    }
201
202
    /**
203
     * Get error message
204
     *
205
     * @return null|string
206
     */
207 7
    public function getError()
208
    {
209 7
        return $this->error;
210
    }
211
212
    /**
213
     * Set error message for replace text from rule
214
     *
215
     * @param  string $message
216
     *
217
     * @return ValidatorChain
218
     */
219 8
    protected function setError($message) : ValidatorChain
220
    {
221 8
        $this->error = $message;
222 8
        return $this;
223
    }
224
225
    /**
226
     * Get validation description
227
     *
228
     * @return array
229
     */
230 5
    public function getDescription() : array
231
    {
232
        // apply custom description
233 5
        if ($this->description) {
234 2
            return [$this->description];
235
        }
236
237
        // eject description from rules
238 4
        return array_map('strval', $this->rules);
239
    }
240
241
    /**
242
     * Set validation description
243
     *
244
     * @param  string $message
245
     *
246
     * @return ValidatorChain
247
     */
248 4
    public function setDescription($message) : ValidatorChain
249
    {
250 4
        $this->description = $message;
251 4
        return $this;
252
    }
253
}
254