-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathValidator.php
More file actions
224 lines (201 loc) · 4.64 KB
/
Copy pathValidator.php
File metadata and controls
224 lines (201 loc) · 4.64 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
223
224
<?php
declare(strict_types=1);
namespace Bow\Validation;
use Bow\Support\Str;
use Bow\Validation\Rules\BetweenRule;
use Bow\Validation\Rules\BooleanRule;
use Bow\Validation\Rules\ConfirmedRule;
use Bow\Validation\Rules\DatabaseRule;
use Bow\Validation\Rules\DatetimeRule;
use Bow\Validation\Rules\DifferentRule;
use Bow\Validation\Rules\EmailRule;
use Bow\Validation\Rules\IpRule;
use Bow\Validation\Rules\JsonRule;
use Bow\Validation\Rules\NullableRule;
use Bow\Validation\Rules\NumericRule;
use Bow\Validation\Rules\RegexRule;
use Bow\Validation\Rules\StringRule;
use Bow\Validation\Rules\UrlRule;
use Bow\Validation\Rules\UuidRule;
class Validator
{
use FieldLexical;
use DatabaseRule;
use DatetimeRule;
use EmailRule;
use NumericRule;
use StringRule;
use RegexRule;
use NullableRule;
use UrlRule;
use IpRule;
use BooleanRule;
use JsonRule;
use UuidRule;
use ConfirmedRule;
use DifferentRule;
use BetweenRule;
/**
* The Fails flag
*
* @var bool
*/
protected bool $fails = false;
/**
* The last name
*
* @var ?string
*/
protected ?string $last_message = null;
/**
* The errors list
*
* @var array
*/
protected array $errors = [];
/**
* The validation DATA
*
* @var array
*/
protected array $inputs = [];
/**
* The user messages
*
* @var array
*/
protected array $messages = [];
/**
* Define the valid rule list
*
* @var array
*/
protected array $rules = [
'Nullable',
'Required',
"RequiredIf",
'Max',
'Min',
'Lower',
'Upper',
'Size',
'Same',
'Alpha',
'AlphaNum',
'Number',
'Email',
'In',
'Int',
'Regex',
'Float',
'Date',
'DateTime',
'NotExists',
'Unique',
'Exists',
'Url',
'Ip',
'Boolean',
'Json',
'Uuid',
'Confirmed',
'Different',
'Between',
];
/**
* The lexical
*
* @var array
*/
private array $lexical;
/**
* Validator constructor
*
* @return void
*/
public function __construct()
{
$this->lexical = include __DIR__ . '/stubs/lexical.php';
}
/**
* Any possible markers.
*
* @param array $inputs
* @param array $rules
* @param array $messages
*
* @return Validate
*/
public static function make(array $inputs, array $rules, array $messages = []): Validate
{
$validator = new Validator();
$validator->setCustomMessages($messages);
return $validator->validate($inputs, $rules);
}
/**
* Set the user custom message
*
* @param array $messages
*/
public function setCustomMessages(array $messages)
{
$this->messages = $messages;
}
/**
* Make validation
*
* @param array $inputs
* @param array $rules
*
* @return Validate
*/
public function validate(array $inputs, array $rules): Validate
{
$this->inputs = $inputs;
/**
* Formatting and validation of each rule
* eg. name => "required|max:100|alpha"
*/
foreach ($rules as $field => $rule) {
$this->checkRule($rule, $field);
}
return new Validate(
$this->fails,
$this->last_message,
$this->errors
);
}
/**
* Check atomic rule
*
* @param string $rule
* @param string $field
* @return void
*/
private function checkRule(string $rule, string $field): void
{
$masques = explode("|", $rule);
// `required` always runs, even when `nullable` matched — an explicit
// `required` is an unconditional contract.
$required_declared = in_array('required', $masques, true);
foreach ($masques as $masque) {
// In the box there is a | super flux.
if (is_int($masque) || Str::len($masque) == "") {
continue;
}
if ($masque == "nullable" && $this->compileNullable($field, $masque)) {
if ($required_declared) {
continue;
}
break;
}
// Mask on the required rule
foreach ($this->rules as $rule_item) {
$this->{'compile' . $rule_item}($field, $masque);
if ($rule_item == 'Required' && $this->fails) {
break;
}
}
}
}
}