-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.php
54 lines (46 loc) · 1.35 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
<?php
use Shop\Components\Validator\Rules\Email;
use Shop\Components\Validator\Rules\Gt;
use Shop\Components\Validator\Rules\Gte;
use Shop\Components\Validator\Rules\In;
use Shop\Components\Validator\Rules\LengthMax;
use Shop\Components\Validator\Rules\LengthMin;
use Shop\Components\Validator\Rules\NotIn;
use Shop\Components\Validator\Rules\Required;
use Shop\Components\Validator\ValidationScope;
require __DIR__ . '/vendor/autoload.php';
$data = [
'name' => 'dfsdddfs',
'age' => null,
'email' => 'joeexample.com',
'gender'=> 'male'
];
$toValidate = [
'name' => [
new LengthMin(5),
new LengthMax(10),
new Required(),
],
'email' => [
new Email('ошибка Email')
],
'age' => [
new Gte(18),
],
'gender' => [
new In('male', 'female'),
new NotIn('a', 'b', 'c')
]
// 'age' => [
// function ($value) {
// $error = new \Shop\Components\Validator\Error();
// if ($value <= 0) {
// return $error->addError('Введите положительное ненулевое число');
// }
// return $error;
// }
// ]
];
$validator = new \Shop\Components\Validator\Validator();
$errorCollection = $validator->validate($data, ValidationScope::fromArray($toValidate));
var_dump($errorCollection);