Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
78 / 78
100.00% covered (success)
100.00%
39 / 39
CRAP
100.00% covered (success)
100.00%
1 / 1
Validator
100.00% covered (success)
100.00%
78 / 78
100.00% covered (success)
100.00%
39 / 39
39
100.00% covered (success)
100.00%
1 / 1
 alpha
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 number
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 alphaNumber
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 uuid
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 timezone
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 base64
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 md5
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hexColor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 json
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 regex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 notRegex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 notEquals
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 between
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 notBetween
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 in
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 notIn
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 ip
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 url
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 datetime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 email
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 greater
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 greaterOrEqual
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 less
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 lessOrEqual
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 latin
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 maxLength
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 minLength
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 length
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 required
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 array
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 bool
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 float
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 int
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 object
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 string
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 specialChar
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Validation Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Validation\Traits;
11
12use JetBrains\PhpStorm\Language;
13
14/**
15 * Trait Validator.
16 *
17 * @package validation
18 */
19trait Validator
20{
21    /**
22     * Validates alphabetic characters.
23     *
24     * @return static
25     */
26    public function alpha() : static
27    {
28        $this->rules[] = 'alpha';
29        return $this;
30    }
31
32    /**
33     * Validates a number.
34     *
35     * @return static
36     */
37    public function number() : static
38    {
39        $this->rules[] = 'number';
40        return $this;
41    }
42
43    /**
44     * Validates a number or alphabetic characters.
45     *
46     * @return static
47     */
48    public function alphaNumber() : static
49    {
50        $this->rules[] = 'alphaNumber';
51        return $this;
52    }
53
54    /**
55     * Validates a UUID.
56     *
57     * @return static
58     */
59    public function uuid() : static
60    {
61        $this->rules[] = 'uuid';
62        return $this;
63    }
64
65    /**
66     * Validates a timezone.
67     *
68     * @return static
69     */
70    public function timezone() : static
71    {
72        $this->rules[] = 'timezone';
73        return $this;
74    }
75
76    /**
77     * Validates a base64 string.
78     *
79     * @return static
80     */
81    public function base64() : static
82    {
83        $this->rules[] = 'base64';
84        return $this;
85    }
86
87    /**
88     * Validates a md5 hash.
89     *
90     * @return static
91     */
92    public function md5() : static
93    {
94        $this->rules[] = 'md5';
95        return $this;
96    }
97
98    /**
99     * Validates a hexadecimal string.
100     *
101     * @return static
102     */
103    public function hex() : static
104    {
105        $this->rules[] = 'hex';
106        return $this;
107    }
108
109    /**
110     * Validates a hexadecimal color.
111     *
112     * @return static
113     */
114    public function hexColor() : static
115    {
116        $this->rules[] = 'hexColor';
117        return $this;
118    }
119
120    /**
121     * Validates a JSON string.
122     *
123     * @return static
124     */
125    public function json() : static
126    {
127        $this->rules[] = 'json';
128        return $this;
129    }
130
131    /**
132     * Validates a Regex pattern.
133     *
134     * @param string $pattern
135     *
136     * @return static
137     */
138    public function regex(#[Language('RegExp')] string $pattern) : static
139    {
140        $this->rules[] = 'regex:' . $this->esc($pattern);
141        return $this;
142    }
143
144    /**
145     * Validates a Regex no matching pattern.
146     *
147     * @param string $pattern
148     *
149     * @return static
150     */
151    public function notRegex(#[Language('RegExp')] string $pattern) : static
152    {
153        $this->rules[] = 'notRegex:' . $this->esc($pattern);
154        return $this;
155    }
156
157    /**
158     * Validate field has value equals other field.
159     *
160     * @param string $equalsField
161     *
162     * @return static
163     */
164    public function equals(string $equalsField) : static
165    {
166        $this->rules[] = 'equals:' . $this->esc($equalsField);
167        return $this;
168    }
169
170    /**
171     * Validate field has not value equals other field.
172     *
173     * @param string $diffField
174     *
175     * @return static
176     */
177    public function notEquals(string $diffField) : static
178    {
179        $this->rules[] = 'notEquals:' . $this->esc($diffField);
180        return $this;
181    }
182
183    /**
184     * Validate field between min and max values.
185     *
186     * @param int|string $min
187     * @param int|string $max
188     *
189     * @return static
190     */
191    public function between(int | string $min, int | string $max) : static
192    {
193        $this->rules[] = 'between:' . $this->esc((string) $min) . ',' . $this->esc((string) $max);
194        return $this;
195    }
196
197    /**
198     * Validate field not between min and max values.
199     *
200     * @param int|string $min
201     * @param int|string $max
202     *
203     * @return static
204     */
205    public function notBetween(int | string $min, int | string $max) : static
206    {
207        $this->rules[] = 'notBetween:' . $this->esc((string) $min) . ',' . $this->esc((string) $max);
208        return $this;
209    }
210
211    /**
212     * Validate field is in list.
213     *
214     * @param string $in
215     * @param string ...$others
216     *
217     * @return static
218     */
219    public function in(string $in, string ...$others) : static
220    {
221        $this->rules[] = 'in:' . $this->implode([$in, ...$others]);
222        return $this;
223    }
224
225    /**
226     * Validate field is not in list.
227     *
228     * @param string $notIn
229     * @param string ...$others
230     *
231     * @return static
232     */
233    public function notIn(string $notIn, string ...$others) : static
234    {
235        $this->rules[] = 'notIn:' . $this->implode([$notIn, ...$others]);
236        return $this;
237    }
238
239    /**
240     * Validates an IP.
241     *
242     * @param int $version 4, 6 or 0 to both
243     *
244     * @return static
245     */
246    public function ip(int $version = 0) : static
247    {
248        $this->rules[] = 'ip:' . $version;
249        return $this;
250    }
251
252    /**
253     * Validates an URL.
254     *
255     * @return static
256     */
257    public function url() : static
258    {
259        $this->rules[] = 'url';
260        return $this;
261    }
262
263    /**
264     * Validates a datetime format.
265     *
266     * @param string $format
267     *
268     * @return static
269     */
270    public function datetime(string $format = 'Y-m-d H:i:s') : static
271    {
272        $this->rules[] = 'datetime:' . $this->esc($format);
273        return $this;
274    }
275
276    /**
277     * Validates a email.
278     *
279     * @return static
280     */
281    public function email() : static
282    {
283        $this->rules[] = 'email';
284        return $this;
285    }
286
287    /**
288     * Validates is greater than.
289     *
290     * @param int|string $greaterThan
291     *
292     * @return static
293     */
294    public function greater(int | string $greaterThan) : static
295    {
296        $this->rules[] = 'greater:' . $this->esc((string) $greaterThan);
297        return $this;
298    }
299
300    /**
301     * Validates is greater than or equal to.
302     *
303     * @param int|string $greaterThanOrEqualTo
304     *
305     * @return static
306     */
307    public function greaterOrEqual(int | string $greaterThanOrEqualTo) : static
308    {
309        $this->rules[] = 'greaterOrEqual:' . $this->esc((string) $greaterThanOrEqualTo);
310        return $this;
311    }
312
313    /**
314     * Validates is less than.
315     *
316     * @param int|string $lessThan
317     *
318     * @return static
319     */
320    public function less(int | string $lessThan) : static
321    {
322        $this->rules[] = 'less:' . $this->esc((string) $lessThan);
323        return $this;
324    }
325
326    /**
327     * Validates is less than or equal to.
328     *
329     * @param int|string $lessThanOrEqualTo
330     *
331     * @return static
332     */
333    public function lessOrEqual(int | string $lessThanOrEqualTo) : static
334    {
335        $this->rules[] = 'lessOrEqual:' . $this->esc((string) $lessThanOrEqualTo);
336        return $this;
337    }
338
339    /**
340     * Validates a latin text.
341     *
342     * @return static
343     */
344    public function latin() : static
345    {
346        $this->rules[] = 'latin';
347        return $this;
348    }
349
350    /**
351     * Validates max length.
352     *
353     * @param int $maxLength
354     *
355     * @return static
356     */
357    public function maxLength(int $maxLength) : static
358    {
359        $this->rules[] = 'maxLength:' . $maxLength;
360        return $this;
361    }
362
363    /**
364     * Validates min length.
365     *
366     * @param int $minLength
367     *
368     * @return static
369     */
370    public function minLength(int $minLength) : static
371    {
372        $this->rules[] = 'minLength:' . $minLength;
373        return $this;
374    }
375
376    /**
377     * Validates exact length.
378     *
379     * @param int $length
380     *
381     * @return static
382     */
383    public function length(int $length) : static
384    {
385        $this->rules[] = 'length:' . $length;
386        return $this;
387    }
388
389    /**
390     * Validates required value.
391     *
392     * @return static
393     */
394    public function required() : static
395    {
396        $this->rules[] = 'required';
397        return $this;
398    }
399
400    /**
401     * Validates field is set.
402     *
403     * @return static
404     */
405    public function isset() : static
406    {
407        $this->rules[] = 'isset';
408        return $this;
409    }
410
411    /**
412     * Validates array.
413     *
414     * @since 2.2
415     *
416     * @return static
417     */
418    public function array() : static
419    {
420        $this->rules[] = 'array';
421        return $this;
422    }
423
424    /**
425     * Validates boolean.
426     *
427     * @since 2.2
428     *
429     * @return static
430     */
431    public function bool() : static
432    {
433        $this->rules[] = 'bool';
434        return $this;
435    }
436
437    /**
438     * Validates float.
439     *
440     * @since 2.2
441     *
442     * @return static
443     */
444    public function float() : static
445    {
446        $this->rules[] = 'float';
447        return $this;
448    }
449
450    /**
451     * Validates integer.
452     *
453     * @since 2.2
454     *
455     * @return static
456     */
457    public function int() : static
458    {
459        $this->rules[] = 'int';
460        return $this;
461    }
462
463    /**
464     * Validates object.
465     *
466     * @since 2.2
467     *
468     * @return static
469     */
470    public function object() : static
471    {
472        $this->rules[] = 'object';
473        return $this;
474    }
475
476    /**
477     * Validates string.
478     *
479     * @since 2.2
480     *
481     * @return static
482     */
483    public function string() : static
484    {
485        $this->rules[] = 'string';
486        return $this;
487    }
488
489    /**
490     * Validates special characters.
491     *
492     * @see https://owasp.org/www-community/password-special-characters
493     *
494     * @param int $quantity
495     * @param string $characters
496     *
497     * @return static
498     */
499    public function specialChar(
500        int $quantity = 1,
501        string $characters = '!"#$%&\'()*+,-./:;=<>?@[\]^_`{|}~'
502    ) : static {
503        $this->rules[] = 'specialChar:' . $quantity . ',' . $this->esc($characters);
504        return $this;
505    }
506}