1 | <?php |
||
44 | final class Validator |
||
45 | { |
||
46 | /** |
||
47 | * @var string[] |
||
48 | */ |
||
49 | private static $validators = [ |
||
50 | 'alpha' => Alpha::class, |
||
51 | 'alpha_dash' => AlphaDash::class, |
||
52 | 'alpha_numeric' => AlphaNumeric::class, |
||
53 | 'boolean' => Boolean::class, |
||
54 | 'callable' => Callables::class, |
||
55 | 'date' => Date::class, |
||
56 | 'db_timestamp' => MySqlDatetime::class, |
||
57 | 'encrypt' => Encrypt::class, |
||
58 | 'email' => Email::class, |
||
59 | 'enum' => Enum::class, |
||
60 | 'ip' => Ip::class, |
||
61 | 'matching' => Matching::class, |
||
62 | 'numeric' => Numeric::class, |
||
63 | 'password' => Password::class, |
||
64 | 'range' => Range::class, |
||
65 | 'required' => Required::class, |
||
66 | 'string' => Strings::class, |
||
67 | 'time_zone' => Timezone::class, |
||
68 | 'timestamp' => Timestamp::class, |
||
69 | 'unique' => Unique::class, |
||
70 | 'url' => Url::class, |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var ValidationRuleInterface[] |
||
75 | */ |
||
76 | private static $instances = []; |
||
77 | |||
78 | /** |
||
79 | * @var array |
||
80 | */ |
||
81 | private $rules; |
||
82 | |||
83 | /** |
||
84 | * @var string|null |
||
85 | */ |
||
86 | private $failingRule; |
||
87 | |||
88 | /** |
||
89 | * @var array |
||
90 | */ |
||
91 | private $failingOptions; |
||
92 | |||
93 | /** |
||
94 | * Rules can be defined in these formats: |
||
95 | * - [['matching'], ['string', 'min' => '5']] |
||
96 | * - ['matching', ['string', 'min' => '5']] |
||
97 | * - ['string', 'min' => '5'] |
||
98 | * - matching|password. |
||
99 | * |
||
100 | * @param array|string $rules |
||
101 | */ |
||
102 | public function __construct($rules) |
||
122 | |||
123 | /** |
||
124 | * Gets the rules. |
||
125 | */ |
||
126 | public function getRules(): array |
||
130 | |||
131 | /** |
||
132 | * Validates the given value against the rules. |
||
133 | * |
||
134 | * @param mixed $value |
||
135 | */ |
||
136 | public function validate(&$value, Model $model): bool |
||
152 | |||
153 | /** |
||
154 | * Gets the failing rule. |
||
155 | */ |
||
156 | public function getFailingRule(): ?string |
||
160 | |||
161 | /** |
||
162 | * Gets the options of the failing rule. |
||
163 | */ |
||
164 | public function getFailingRuleOptions(): ?array |
||
168 | |||
169 | private function getRule(string $name): ValidationRuleInterface |
||
182 | |||
183 | /** |
||
184 | * Validates and marshals a property value prior to saving. |
||
185 | * |
||
186 | * @param Property $property property definition |
||
187 | * @param mixed $value |
||
188 | */ |
||
189 | public static function validateProperty(Model $model, Property $property, &$value): bool |
||
223 | } |
||
224 |