Completed
Push — master ( 3e9a7d...862f35 )
by Anton
13s
created

ValidatorChain::setDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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