-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathValidator.php
More file actions
222 lines (200 loc) · 6.92 KB
/
Validator.php
File metadata and controls
222 lines (200 loc) · 6.92 KB
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
<?php
namespace Rakit\Validation;
class Validator
{
use Traits\TranslationsTrait, Traits\MessagesTrait;
/** @var array */
protected $translations = [];
/** @var array */
protected $validators = [];
/** @var bool */
protected $allowRuleOverride = false;
/** @var bool */
protected $useHumanizedKeys = true;
/**
* Constructor
*
* @param array $messages
* @return void
*/
public function __construct(array $messages = [])
{
$this->messages = $messages;
$this->registerBaseValidators();
}
/**
* Register or override existing validator
*
* @param mixed $key
* @param \Rakit\Validation\Rule $rule
* @return void
*/
public function setValidator(string $key, Rule $rule)
{
$this->validators[$key] = $rule;
$rule->setKey($key);
}
/**
* Get validator object from given $key
*
* @param mixed $key
* @return mixed
*/
public function getValidator($key)
{
return isset($this->validators[$key]) ? $this->validators[$key] : null;
}
/**
* Validate $inputs
*
* @param array $inputs
* @param array $rules
* @param array $messages
* @return Validation
*/
public function validate(array $inputs, array $rules, array $messages = []): Validation
{
$validation = $this->make($inputs, $rules, $messages);
$validation->validate();
return $validation;
}
/**
* Given $inputs, $rules and $messages to make the Validation class instance
*
* @param array $inputs
* @param array $rules
* @param array $messages
* @return Validation
*/
public function make(array $inputs, array $rules, array $messages = []): Validation
{
$messages = array_merge($this->messages, $messages);
$validation = new Validation($this, $inputs, $rules, $messages);
$validation->setTranslations($this->getTranslations());
return $validation;
}
/**
* Magic invoke method to make Rule instance
*
* @param string $rule
* @return Rule
* @throws RuleNotFoundException
*/
public function __invoke(string $rule): Rule
{
$args = func_get_args();
$rule = array_shift($args);
$params = $args;
$validator = $this->getValidator($rule);
if (!$validator) {
throw new RuleNotFoundException("Validator '{$rule}' is not registered", 1);
}
$clonedValidator = clone $validator;
$clonedValidator->fillParameters($params);
return $clonedValidator;
}
/**
* Initialize base validators array
*
* @return void
*/
protected function registerBaseValidators()
{
$baseValidator = [
'required' => new Rules\Required,
'required_if' => new Rules\RequiredIf,
'required_unless' => new Rules\RequiredUnless,
'required_with' => new Rules\RequiredWith,
'required_without' => new Rules\RequiredWithout,
'required_with_all' => new Rules\RequiredWithAll,
'required_without_all' => new Rules\RequiredWithoutAll,
'email' => new Rules\Email,
'alpha' => new Rules\Alpha,
'numeric' => new Rules\Numeric,
'alpha_num' => new Rules\AlphaNum,
'alpha_dash' => new Rules\AlphaDash,
'alpha_spaces' => new Rules\AlphaSpaces,
'in' => new Rules\In,
'not_in' => new Rules\NotIn,
'min' => new Rules\Min,
'max' => new Rules\Max,
'between' => new Rules\Between,
'url' => new Rules\Url,
'integer' => new Rules\Integer,
'boolean' => new Rules\Boolean,
'ip' => new Rules\Ip,
'ipv4' => new Rules\Ipv4,
'ipv6' => new Rules\Ipv6,
'extension' => new Rules\Extension,
'array' => new Rules\TypeArray,
'same' => new Rules\Same,
'regex' => new Rules\Regex,
'date' => new Rules\Date,
'accepted' => new Rules\Accepted,
'present' => new Rules\Present,
'different' => new Rules\Different,
'uploaded_file' => new Rules\UploadedFile,
'mimes' => new Rules\Mimes,
'callback' => new Rules\Callback,
'before' => new Rules\Before,
'after' => new Rules\After,
'lowercase' => new Rules\Lowercase,
'uppercase' => new Rules\Uppercase,
'json' => new Rules\Json,
'digits' => new Rules\Digits,
'digits_between' => new Rules\DigitsBetween,
'defaults' => new Rules\Defaults,
'default' => new Rules\Defaults, // alias of defaults
'nullable' => new Rules\Nullable,
'length' => new Rules\Length,
];
foreach ($baseValidator as $key => $validator) {
$this->setValidator($key, $validator);
}
}
/**
* Given $ruleName and $rule to add new validator
*
* @param string $ruleName
* @param \Rakit\Validation\Rule $rule
* @return void
*/
public function addValidator(string $ruleName, Rule $rule)
{
if (!$this->allowRuleOverride && array_key_exists($ruleName, $this->validators)) {
throw new RuleQuashException(
"You cannot override a built in rule. You have to rename your rule"
);
}
$this->setValidator($ruleName, $rule);
}
/**
* Set rule can allow to be overrided
*
* @param boolean $status
* @return void
*/
public function allowRuleOverride(bool $status = false)
{
$this->allowRuleOverride = $status;
}
/**
* Set this can use humanize keys
*
* @param boolean $useHumanizedKeys
* @return void
*/
public function setUseHumanizedKeys(bool $useHumanizedKeys = true)
{
$this->useHumanizedKeys = $useHumanizedKeys;
}
/**
* Get $this->useHumanizedKeys value
*
* @return void
*/
public function isUsingHumanizedKey(): bool
{
return $this->useHumanizedKeys;
}
}