-
Notifications
You must be signed in to change notification settings - Fork 773
/
Validator.php
232 lines (222 loc) · 9.51 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation;
use finfo;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Rules\Key;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use function count;
/**
* @method static Validator allOf(Validatable ...$rule)
* @method static Validator alnum(string ...$additionalChars)
* @method static Validator alpha(string ...$additionalChars)
* @method static Validator alwaysInvalid()
* @method static Validator alwaysValid()
* @method static Validator anyOf(Validatable ...$rule)
* @method static Validator arrayType()
* @method static Validator arrayVal()
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
* @method static Validator base(int $base, string $chars = null)
* @method static Validator base64()
* @method static Validator between($minimum, $maximum)
* @method static Validator bic(string $countryCode)
* @method static Validator boolType()
* @method static Validator boolVal()
* @method static Validator bsn()
* @method static Validator call(callable $callable, Validatable $rule)
* @method static Validator callableType()
* @method static Validator callback(callable $callback)
* @method static Validator charset(string ...$charset)
* @method static Validator cnh()
* @method static Validator cnpj()
* @method static Validator cntrl(string ...$additionalChars)
* @method static Validator consonant(string ...$additionalChars)
* @method static Validator contains($containsValue, bool $identical = false)
* @method static Validator containsAny(array $needles, bool $strictCompareArray = false)
* @method static Validator countable()
* @method static Validator countryCode(string $set = null)
* @method static Validator currencyCode()
* @method static Validator cpf()
* @method static Validator creditCard(string $brand = null)
* @method static Validator date(string $format = 'Y-m-d')
* @method static Validator dateTime(string $format = null)
* @method static Validator digit(string ...$additionalChars)
* @method static Validator directory()
* @method static Validator domain(bool $tldCheck = true)
* @method static Validator each(Validatable $rule)
* @method static Validator email()
* @method static Validator endsWith($endValue, bool $identical = false)
* @method static Validator equals($compareTo)
* @method static Validator equivalent($compareTo)
* @method static Validator even()
* @method static Validator executable()
* @method static Validator exists()
* @method static Validator extension(string $extension)
* @method static Validator factor(int $dividend)
* @method static Validator falseVal()
* @method static Validator fibonacci()
* @method static Validator file()
* @method static Validator filterVar(int $filter, $options = null)
* @method static Validator finite()
* @method static Validator floatVal()
* @method static Validator floatType()
* @method static Validator graph(string ...$additionalChars)
* @method static Validator greaterThan($compareTo)
* @method static Validator hexRgbColor()
* @method static Validator iban()
* @method static Validator identical($value)
* @method static Validator identityCard(string $countryCode)
* @method static Validator image(finfo $fileInfo = null)
* @method static Validator imei()
* @method static Validator in($haystack, bool $compareIdentical = false)
* @method static Validator infinite()
* @method static Validator instance(string $instanceName)
* @method static Validator intVal()
* @method static Validator intType()
* @method static Validator ip(string $range = '*', int $options = null)
* @method static Validator isbn()
* @method static Validator iterableType()
* @method static Validator json()
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
* @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
* @method static Validator keySet(Key ...$rule)
* @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
* @method static Validator languageCode(string $set = null)
* @method static Validator leapDate(string $format)
* @method static Validator leapYear()
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
* @method static Validator lowercase()
* @method static Validator lessThan($compareTo)
* @method static Validator luhn()
* @method static Validator macAddress()
* @method static Validator max($compareTo)
* @method static Validator maxAge(int $age, string $format = null)
* @method static Validator mimetype(string $mimetype)
* @method static Validator min($compareTo)
* @method static Validator minAge(int $age, string $format = null)
* @method static Validator multiple(int $multipleOf)
* @method static Validator negative()
* @method static Validator nfeAccessKey()
* @method static Validator nif()
* @method static Validator no($useLocale = false)
* @method static Validator noneOf(Validatable ...$rule)
* @method static Validator not(Validatable $rule)
* @method static Validator notBlank()
* @method static Validator notEmoji()
* @method static Validator notEmpty()
* @method static Validator notOptional()
* @method static Validator noWhitespace()
* @method static Validator nullable(Validatable $rule)
* @method static Validator nullType()
* @method static Validator number()
* @method static Validator numericVal()
* @method static Validator objectType()
* @method static Validator odd()
* @method static Validator oneOf(Validatable ...$rule)
* @method static Validator optional(Validatable $rule)
* @method static Validator perfectSquare()
* @method static Validator pesel()
* @method static Validator phone()
* @method static Validator phpLabel()
* @method static Validator pis()
* @method static Validator positive()
* @method static Validator postalCode(string $countryCode)
* @method static Validator primeNumber()
* @method static Validator printable(string ...$additionalChars)
* @method static Validator punct(string ...$additionalChars)
* @method static Validator readable()
* @method static Validator regex(string $regex)
* @method static Validator resourceType()
* @method static Validator roman()
* @method static Validator scalarVal()
* @method static Validator sf(Constraint $constraint, ValidatorInterface $validator = null)
* @method static Validator size(string $minSize = null, string $maxSize = null)
* @method static Validator slug()
* @method static Validator sorted(string $direction)
* @method static Validator space(string ...$additionalChars)
* @method static Validator startsWith($startValue, bool $identical = false)
* @method static Validator stringType()
* @method static Validator stringVal()
* @method static Validator subdivisionCode(string $countryCode)
* @method static Validator subset(array $superset)
* @method static Validator symbolicLink()
* @method static Validator time(string $format = 'H:i:s')
* @method static Validator tld()
* @method static Validator trueVal()
* @method static Validator type(string $type)
* @method static Validator unique()
* @method static Validator uploaded()
* @method static Validator uppercase()
* @method static Validator url()
* @method static Validator uuid(int $version = null)
* @method static Validator vatin(string $countryCode)
* @method static Validator version()
* @method static Validator videoUrl(string $service = null)
* @method static Validator vowel(string ...$additionalChars)
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
* @method static Validator writable()
* @method static Validator xdigit(string ...$additionalChars)
* @method static Validator yes($useLocale = false)
* @method static Validator zend($validator, array $params = null)
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Validator extends AllOf
{
/**
* {@inheritdoc}
*/
public function check($input): void
{
try {
parent::check($input);
} catch (ValidationException $exception) {
if (count($this->getRules()) == 1 && $this->template) {
$exception->updateTemplate($this->template);
}
throw $exception;
}
}
/**
* Creates a new Validator instance with a rule that was called on the static method.
*
* @param mixed[] $arguments
*
* @throws ComponentException
*/
public static function __callStatic(string $ruleName, array $arguments): self
{
return self::create()->__call($ruleName, $arguments);
}
/**
* Create a new rule by the name of the method and adds the rule to the chain.
*
* @param mixed[] $arguments
*
* @throws ComponentException
*/
public function __call(string $ruleName, array $arguments): self
{
$this->addRule(Factory::getDefaultInstance()->rule($ruleName, $arguments));
return $this;
}
/**
* Create instance validator.
*/
public static function create(): self
{
return new self();
}
}