1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PTS\Validator; |
4
|
|
|
|
5
|
|
|
use PTS\Validator\Validators\AlphaDashValidator; |
6
|
|
|
use PTS\Validator\Validators\AlphaNumValidator; |
7
|
|
|
use PTS\Validator\Validators\AlphaValidator; |
8
|
|
|
use PTS\Validator\Validators\BetweenIntValidator; |
9
|
|
|
use PTS\Validator\Validators\BoolValidator; |
10
|
|
|
use PTS\Validator\Validators\DateTimeValidator; |
11
|
|
|
use PTS\Validator\Validators\DateValidator; |
12
|
|
|
use PTS\Validator\Validators\InArrayValidator; |
13
|
|
|
use PTS\Validator\Validators\MaxValidator; |
14
|
|
|
use PTS\Validator\Validators\MinValidator; |
15
|
|
|
use PTS\Validator\Validators\RequiredValidator; |
16
|
|
|
|
17
|
|
|
class Validator |
18
|
|
|
{ |
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $paramDelimiter = ':'; |
21
|
|
|
|
22
|
|
|
/** @var callable[] */ |
23
|
|
|
protected $rulesHandlers = []; |
24
|
|
|
|
25
|
37 |
|
public function __construct() |
26
|
|
|
{ |
27
|
37 |
|
$this->registerRule('string', 'is_string'); |
28
|
37 |
|
$this->registerRule('int', 'is_int'); |
29
|
37 |
|
$this->registerRule('array', 'is_array'); |
30
|
37 |
|
$this->registerRule('required', new RequiredValidator); |
31
|
37 |
|
$this->registerRule('betweenInt', new BetweenIntValidator); |
32
|
37 |
|
$this->registerRule('strictBool', 'is_bool'); |
33
|
37 |
|
$this->registerRule('bool', new BoolValidator); |
34
|
37 |
|
$this->registerRule('alpha', new AlphaValidator); |
35
|
37 |
|
$this->registerRule('alphaDash', new AlphaDashValidator); |
36
|
37 |
|
$this->registerRule('alphaNum', new AlphaNumValidator); |
37
|
37 |
|
$this->registerRule('date', new DateValidator); |
38
|
37 |
|
$this->registerRule('dateTime', new DateTimeValidator); |
39
|
37 |
|
$this->registerRule('inArray', new InArrayValidator); |
40
|
37 |
|
$this->registerRule('min', new MinValidator); |
41
|
37 |
|
$this->registerRule('max', new MaxValidator); |
42
|
37 |
|
} |
43
|
|
|
|
44
|
37 |
|
public function registerRule(string $name, callable $handler) |
45
|
|
|
{ |
46
|
37 |
|
$this->rulesHandlers[$name] = $handler; |
47
|
37 |
|
} |
48
|
|
|
|
49
|
2 |
|
public function getRules(): array |
50
|
|
|
{ |
51
|
2 |
|
return $this->rulesHandlers; |
52
|
|
|
} |
53
|
|
|
|
54
|
27 |
|
public function validate(array $data, array $rules): array |
55
|
|
|
{ |
56
|
27 |
|
$errors = []; |
57
|
|
|
|
58
|
27 |
|
foreach ($rules as $name => $attrRules) { |
59
|
27 |
|
if (!array_key_exists($name, $data)) { |
60
|
1 |
|
$errors[$name] = 'Value is not exists: ' . $name; |
61
|
1 |
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
27 |
|
$value = $data[$name] ?? null; |
65
|
|
|
|
66
|
27 |
|
$errors[$name] = $this->validateValue($value, $attrRules); |
67
|
|
|
} |
68
|
|
|
|
69
|
26 |
|
return array_filter($errors); |
70
|
|
|
} |
71
|
|
|
|
72
|
22 |
|
public function validateIfExists(array $data, array $rules): array |
73
|
|
|
{ |
74
|
22 |
|
$errors = []; |
75
|
|
|
|
76
|
22 |
|
foreach ($data as $name => $value) { |
77
|
22 |
|
$attrRules = $rules[$name] ?? []; |
78
|
22 |
|
$errors[$name] = $this->validateValue($value, $attrRules); |
79
|
|
|
} |
80
|
|
|
|
81
|
22 |
|
return array_filter($errors); |
82
|
|
|
} |
83
|
|
|
|
84
|
29 |
|
protected function validateValue($value, array $rules): array |
85
|
|
|
{ |
86
|
29 |
|
$errors = []; |
87
|
|
|
|
88
|
29 |
|
foreach ($rules as $rule) { |
89
|
29 |
|
[$handlerAlias, $params] = is_string($rule) |
|
|
|
|
90
|
27 |
|
? $this->extractStringRule($rule) |
91
|
29 |
|
: $this->extractArrayRule($rule); |
92
|
|
|
|
93
|
29 |
|
$handler = $this->rulesHandlers[$handlerAlias] ?? null; |
94
|
|
|
|
95
|
29 |
|
if (!$handler) { |
96
|
1 |
|
throw new ValidatorRuleException("Handler not found for alias: {$handlerAlias}"); |
97
|
|
|
} |
98
|
|
|
|
99
|
29 |
|
if (!$handler($value, ...$params)) { |
100
|
10 |
|
$errors[] = $handlerAlias; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
28 |
|
return $errors; |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
public function extractArrayRule(array $rule): array |
108
|
|
|
{ |
109
|
5 |
|
return [key($rule), current($rule)]; |
110
|
|
|
} |
111
|
|
|
|
112
|
30 |
|
public function extractStringRule(string $rule): array |
113
|
|
|
{ |
114
|
30 |
|
$params = explode($this->paramDelimiter, $rule); |
115
|
30 |
|
$handlerAlias = array_shift($params); |
116
|
|
|
|
117
|
30 |
|
return [$handlerAlias, (array)$params]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.