-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzschema.php
571 lines (472 loc) · 19.5 KB
/
zschema.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
class ZSchema {
private array $validations = [];
private array $transforms = [];
private array $messages = [];
function __construct(
private string $type,
private mixed $message = null,
private mixed $array = null,
) {}
public static function int($message = null): self {
return new self("int", $message);
}
public static function float($message = null): self {
return new self("float", $message);
}
public static function string($message = null): self {
return new self("string", $message);
}
public static function bool($message = null): self {
return new self("bool", $message);
}
public static function array($array = null, $message = null): mixed {
// if (!is_array($array)) throw new Exception("the argument must be an array");
// if (count($array) == 0) throw new Exception("array cannot be empty");
if ($array != null && is_array($array) && count($array) > 0) {
foreach($array as $key => $value) {
if (!($value instanceof Schema)) throw new Exception("$key is not a Schema");
}
}
return new self("array", $message, $array);
}
public static function null(): self {
return new self("null");
}
public function parse($value, $key = null) {
switch ($this->type) {
case "int":
$is_valid = $this->validate_int($value, $key);
if ($is_valid['success'] == true) return $value;
throw new Exception($is_valid['message']);
case "float":
$is_valid = $this->validate_float($value, $key);
if ($is_valid['success'] == true) return $value;
throw new Exception($is_valid['message']);
case "string":
$is_valid = $this->validate_string($value, $key);
if ($is_valid['success'] == true) return $value;
throw new Exception($is_valid['message']);
case "bool":
if (!is_bool($value)) throw new Exception($this->message ?? "$value is not a boolean");
return $value;
case "array":
if (!is_array($value)) throw new Exception($this->message ?? "$value is not an array");
foreach ($this->array as $tKey => $tValue) {
if (!($tValue instanceof Schema)) throw new Exception("$tKey is not a Schema");
if (isset($value[$tKey])) $tValue->parse($value[$tKey], $tKey);
else if (isset($tValue->get_validations()['required'])) {
throw new Exception($tValue->get_messages()["required"] ?? "$tKey is required");
}
}
return $value;
case "null":
if (!is_null($value)) throw new Exception("$value is not null");
return null;
}
}
public function safe_parse($value): array {
try {
return [
"success" => true,
"value" => $this->parse($value)
];
} catch (Exception $e) {
return [
"success" => false,
"message" => $e->getMessage(),
"value" => $value
];
}
}
private function validate_string($value, $key = null): array {
if ($key == null) $key = $value;
if (!is_string($value)) return [
"success" => false,
"message" => $this->message ?? "$key is not a valid string"
];
foreach ($this->validations as $validation_name => $validation_value) {
switch ($validation_name) {
case "not_empty":
if (empty($value)) return [
"success" => false,
"message" => $this->messages["not_empty"] ?? "$key cannot be empty"
];
break;
case "max_length":
if (strlen($value) > $validation_value) return [
"success" => false,
"message" => $this->messages["max_length"] ?? "$key must not be greater than $validation_value characters"
];
break;
case "min_length":
if (strlen($value) < $validation_value) return [
"success" => false,
"message" => $this->messages["min_length"] ?? "$key must not be less than $validation_value characters"
];
break;
case "length":
if (strlen($value) != $validation_value) return [
"success" => false,
"message" => $this->messages["length"] ?? "$key must be $validation_value characters"
];
break;
case "email":
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) return [
"success" => false,
"message" => $this->messages["email"] ?? "$key is not a valid email"
];
break;
case "url":
if (!filter_var($value, FILTER_VALIDATE_URL)) return [
"success" => false,
"message" => $this->messages["url"] ?? "$key is not a valid url"
];
break;
case "uuid":
if (!preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i', $value)) return [
"success" => false,
"message" => $this->messages["uuid"] ?? "$key is not a valid uuid"
];
break;
case "ipv4":
if (!filter_var($value, FILTER_VALIDATE_IP)) return [
"success" => false,
"message" => $this->messages["ipv4"] ?? "$key is not a valid ipv4"
];
break;
case "ipv6":
if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) return [
"success" => false,
"message" => $this->messages["ipv6"] ?? "$key is not a valid ipv6"
];
break;
case "regex":
if (!preg_match($validation_value, $value)) return [
"success" => false,
"message" => $this->messages["regex"] ?? "$key does not match $validation_value"
];
break;
case "includes":
if (!str_contains($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["includes"] ?? "$key does not contain $validation_value"
];
break;
case "not_includes":
if (str_contains($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["not_includes"] ?? "$key contains $validation_value"
];
break;
case "starts_with":
if (!str_starts_with($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["starts_with"] ?? "$key should start with $validation_value"
];
break;
case "not_starts_with":
if (str_starts_with($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["not_starts_with"] ?? "$key should not start with $validation_value"
];
break;
case "ends_with":
if (!str_ends_with($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["ends_with"] ?? "$key should end with $validation_value"
];
break;
case "not_ends_with":
if (str_ends_with($value, $validation_value)) return [
"success" => false,
"message" => $this->messages["not_ends_with"] ?? "$key should not end with $validation_value"
];
break;
case "date":
if (!strtotime($value)) return [
"success" => false,
"message" => $this->messages["date"] ?? "$key is not a valid date"
];
break;
case "time":
if (!strtotime($value)) return [
"success" => false,
"message" => $this->messages["time"] ?? "$key is not a valid time"
];
break;
case "datetime":
if (!strtotime($value)) return [
"success" => false,
"message" => $this->messages["datetime"] ?? "$key is not a valid datetime"
];
break;
}
}
return [
"success" => true
];
}
private function transform_string(string $value): string {
foreach ($this->transforms as $transform_name => $transform_value) {
switch ($transform_name) {
case "trim":
$value = trim($value);
break;
case "to_lower_case":
$value = strtolower($value);
break;
case "to_upper_case":
$value = strtoupper($value);
break;
}
}
return $value;
}
public function validate_number($value, $key = null): array {
if ($key == null) $key = $value;
foreach ($this->validations as $validation_name => $validation_value) {
switch ($validation_name) {
case "not_empty":
if (empty($value)) return [
"success" => false,
"message" => $this->messages["not_empty"] ?? "$key cannot be empty"
];
break;
case "max":
if ($value > $validation_value) return [
"success" => false,
"message" => $this->messages["max"] ?? "$key must be less than $validation_value"
];
break;
case "min":
if ($value < $validation_value) return [
"success" => false,
"message" => $this->messages["min"] ?? "$key must be greater than $validation_value"
];
break;
case "positive":
if (!($value > 0)) return [
"success" => false,
"message" => $this->messages["positive"] ?? "$key must be greater than 0"
];
break;
case "nonpositive":
if ($value > 0) return [
"success" => false,
"message" => $this->messages["nonpositive"] ?? "$key must be less than 0"
];
break;
case "negative":
if (!($value < 0)) return [
"success" => false,
"message" => $this->messages["negative"] ?? "$key must be less than 0"
];
break;
case "nonnegative":
if ($value < 0) return [
"success" => false,
"message" => $this->messages["nonnegative"] ?? "$key must be greater than 0"
];
break;
}
}
return [
"success" => true
];
}
public function validate_int($value, $key = null): array {
if ($key == null) $key = $value;
if (!is_int($value)) return [
"success" => false,
"message" => $this->message ?? "$key is not a valid int"
];
return $this->validate_number($value, $key);
}
public function validate_float($value, $key = null): array {
if ($key == null) $key = $value;
if (!is_int($value)) return [
"success" => false,
"message" => $this->message ?? "$key is not a valid float"
];
return $this->validate_number($value, $key);
}
public function transform_int(int $value): int {
foreach ($this->transforms as $transform_name => $transform_value) {
switch ($transform_name) {
case "to_max":
$value = max($value, $transform_value);
break;
case "to_min":
$value = min($value, $transform_value);
break;
}
}
return $value;
}
public function max($value, $message = null): self {
$this->validations['max'] = $value;
if ($message != null) $this->messages['max'] = $message;
return $this;
}
public function min($value, $message = null): self {
$this->validations['min'] = $value;
if ($message != null) $this->messages['min'] = $message;
return $this;
}
public function not_empty($message = null): self {
$this->validations['not_empty'] = true;
if ($message != null) $this->messages['not_empty'] = $message;
return $this;
}
public function required($message = null): self {
$this->validations['required'] = true;
if ($message != null) $this->messages['required'] = $message;
return $this;
}
public function max_length($value, $message = null): self {
$this->validations['max_length'] = $value;
if ($message != null) $this->messages['max_length'] = $message;
return $this;
}
public function min_length($value, $message = null): self {
$this->validations['min_length'] = $value;
if ($message != null) $this->messages['min_length'] = $message;
return $this;
}
public function length($value, $message = null): self {
$this->validations['length'] = $value;
if ($message != null) $this->messages['length'] = $message;
return $this;
}
public function email($message = null): self {
$this->validations['email'] = true;
if ($message != null) $this->messages['email'] = $message;
return $this;
}
public function url($message = null): self {
$this->validations['url'] = true;
if ($message != null) $this->messages['url'] = $message;
return $this;
}
// public function emoji(): self {
// $this->validations['emoji'] = true;
// return $this;
// }
public function uuid($message): self {
$this->validations['uuid'] = true;
if ($message != null) $this->messages['uuid'] = $message;
return $this;
}
public function ipv4($message = null): self {
$this->validations['ipv4'] = true;
if ($message != null) $this->messages['ipv4'] = $message;
return $this;
}
public function ipv6($message = null): self {
$this->validations['ipv4'] = true;
if ($message != null) $this->messages['ipv6'] = $message;
return $this;
}
public function regex($regex, $message = null): self {
$this->validations['regex'] = $regex;
if ($message != null) $this->messages['regex'] = $message;
return $this;
}
public function includes($value, $message = null): self {
$this->validations['includes'] = $value;
if ($message != null) $this->messages['includes'] = $message;
return $this;
}
public function not_includes($value, $message = null): self {
$this->validations['not_includes'] = $value;
if ($message != null) $this->messages['not_includes'] = $message;
return $this;
}
public function starts_with($value, $message = null): self {
$this->validations['starts_with'] = $value;
if ($message != null) $this->messages['starts_with'] = $message;
return $this;
}
public function not_starts_with($value, $message = null): self {
$this->validations['not_starts_with'] = $value;
if ($message != null) $this->messages['not_starts_with'] = $message;
return $this;
}
public function ends_with($value, $message = null): self {
$this->validations['ends_with'] = $value;
if ($message != null) $this->messages['ends_with'] = $message;
return $this;
}
public function not_ends_with($value, $message = null): self {
$this->validations['not_ends_with'] = $value;
if ($message != null) $this->messages['not_ends_with'] = $message;
return $this;
}
public function date($message = null): self {
$this->validations['date'] = true;
if ($message != null) $this->messages['date'] = $message;
return $this;
}
public function time($message = null): self {
$this->validations['time'] = true;
if ($message != null) $this->messages['time'] = $message;
return $this;
}
public function datetime($message = null): self {
$this->validations['datetime'] = true;
if ($message != null) $this->messages['datetime'] = $message;
return $this;
}
// public function base64($message = null) {
// $this->validations['base64'] = true;
// if ($message != null) $this->messages['base64'] = $message;
// return $this;
// }
public function positive($message = null): self {
$this->validations['positive'] = true;
if ($message != null) $this->messages['positive'] = $message;
return $this;
}
public function nonpositive($message = null): self {
$this->validations['nonpositive'] = true;
if ($message != null) $this->messages['nonpositive'] = $message;
return $this;
}
public function negative($message = null): self {
$this->validations['negative'] = true;
if ($message != null) $this->messages['negative'] = $message;
return $this;
}
public function nonnegative($message = null): self {
$this->validations['nonnegative'] = true;
if ($message != null) $this->messages['nonnegative'] = $message;
return $this;
}
public function trim(): self {
$this->transforms['trim'] = true;
return $this;
}
public function to_lower_case(): self {
$this->transforms['to_lower_case'] = true;
return $this;
}
public function to_upper_case(): self {
$this->transforms['to_upper_case'] = true;
return $this;
}
public function to_max($value): self {
$this->transforms['to_max'] = $value;
return $this;
}
public function to_min($value): self {
$this->transforms['to_min'] = $value;
return $this;
}
public function get_validations() {
return $this->validations;
}
public function get_messages() {
return $this->messages;
}
}