|
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\ComponentException; |
|
14
|
|
|
use Bluz\Validator\Rule\RuleInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Validator |
|
18
|
|
|
* |
|
19
|
|
|
* @package Bluz\Validator |
|
20
|
|
|
* @author Anton Shevchuk |
|
21
|
|
|
* @link https://github.com/Respect/Validation |
|
22
|
|
|
* |
|
23
|
|
|
* @method static RuleInterface alpha($additionalCharacters = '') |
|
24
|
|
|
* @method static RuleInterface alphaNumeric($additionalCharacters = '') |
|
25
|
|
|
* @method static RuleInterface array($callback) |
|
26
|
|
|
* @method static RuleInterface between($min, $max) |
|
27
|
|
|
* @method static RuleInterface betweenInclusive($min, $max) |
|
28
|
|
|
* @method static RuleInterface callback($callback) |
|
29
|
|
|
* @method static RuleInterface condition($condition) |
|
30
|
|
|
* @method static RuleInterface contains($containsValue) |
|
31
|
|
|
* @method static RuleInterface containsStrict($containsValue) |
|
32
|
|
|
* @method static RuleInterface countryCode() |
|
33
|
|
|
* @method static RuleInterface creditCard() |
|
34
|
|
|
* @method static RuleInterface date($format) |
|
35
|
|
|
* @method static RuleInterface domain($checkDns = false) |
|
36
|
|
|
* @method static RuleInterface email($checkDns = false) |
|
37
|
|
|
* @method static RuleInterface equals($compareTo) |
|
38
|
|
|
* @method static RuleInterface equalsStrict($compareTo) |
|
39
|
|
|
* @method static RuleInterface float() |
|
40
|
|
|
* @method static RuleInterface in($haystack) |
|
41
|
|
|
* @method static RuleInterface inStrict($haystack) |
|
42
|
|
|
* @method static RuleInterface integer() |
|
43
|
|
|
* @method static RuleInterface ip($options = null) |
|
44
|
|
|
* @method static RuleInterface json() |
|
45
|
|
|
* @method static RuleInterface latin($additionalCharacters = '') |
|
46
|
|
|
* @method static RuleInterface latinNumeric($additionalCharacters = '') |
|
47
|
|
|
* @method static RuleInterface length($min = null, $max = null) |
|
48
|
|
|
* @method static RuleInterface less($maxValue) |
|
49
|
|
|
* @method static RuleInterface lessOrEqual($maxValue) |
|
50
|
|
|
* @method static RuleInterface more($minValue) |
|
51
|
|
|
* @method static RuleInterface moreOrEqual($minValue) |
|
52
|
|
|
* @method static RuleInterface notEmpty() |
|
53
|
|
|
* @method static RuleInterface noWhitespace() |
|
54
|
|
|
* @method static RuleInterface numeric() |
|
55
|
|
|
* @method static RuleInterface required() |
|
56
|
|
|
* @method static RuleInterface regexp($expression) |
|
57
|
|
|
* @method static RuleInterface slug() |
|
58
|
|
|
* @method static RuleInterface string() |
|
59
|
|
|
*/ |
|
60
|
|
|
class Validator |
|
61
|
|
|
{ |
|
62
|
|
|
/** |
|
63
|
|
|
* Create new instance if ValidatorChain |
|
64
|
|
|
* |
|
65
|
|
|
* @return ValidatorChain |
|
66
|
|
|
*/ |
|
67
|
14 |
|
public static function create(): ValidatorChain |
|
68
|
|
|
{ |
|
69
|
14 |
|
return new ValidatorChain(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Magic static call for create instance of Validator |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $ruleName |
|
76
|
|
|
* @param array $arguments |
|
77
|
|
|
* |
|
78
|
|
|
* @return RuleInterface |
|
79
|
|
|
* @throws Exception\ComponentException |
|
80
|
|
|
*/ |
|
81
|
6 |
|
public static function __callStatic($ruleName, $arguments) |
|
82
|
|
|
{ |
|
83
|
6 |
|
return self::rule($ruleName, $arguments); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create new rule by name |
|
88
|
|
|
* |
|
89
|
|
|
* @todo create extension point for custom rules |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $ruleName |
|
92
|
|
|
* @param array $arguments |
|
93
|
|
|
* |
|
94
|
|
|
* @return RuleInterface |
|
95
|
|
|
* @throws Exception\ComponentException |
|
96
|
|
|
*/ |
|
97
|
17 |
|
public static function rule($ruleName, $arguments) : RuleInterface |
|
98
|
|
|
{ |
|
99
|
17 |
|
$ruleClass = '\\Bluz\\Validator\\Rule\\' . ucfirst($ruleName) . 'Rule'; |
|
100
|
|
|
|
|
101
|
17 |
|
if (!class_exists($ruleClass)) { |
|
102
|
1 |
|
throw new ComponentException("Class for validator `$ruleName` not found"); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
16 |
|
return new $ruleClass(...$arguments); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|