|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace midorikocak\nanodb; |
|
6
|
|
|
|
|
7
|
|
|
use function array_key_exists; |
|
8
|
|
|
use function array_keys; |
|
9
|
|
|
use function filter_var; |
|
10
|
|
|
use function is_numeric; |
|
11
|
|
|
use function is_string; |
|
12
|
|
|
use function sort; |
|
13
|
|
|
|
|
14
|
|
|
use const FILTER_VALIDATE_BOOLEAN; |
|
15
|
|
|
use const FILTER_VALIDATE_DOMAIN; |
|
16
|
|
|
use const FILTER_VALIDATE_EMAIL; |
|
17
|
|
|
use const FILTER_VALIDATE_FLOAT; |
|
18
|
|
|
use const FILTER_VALIDATE_INT; |
|
19
|
|
|
use const FILTER_VALIDATE_MAC; |
|
20
|
|
|
use const FILTER_VALIDATE_REGEXP; |
|
21
|
|
|
|
|
22
|
|
|
class ArrayValidator implements ValidableInterface, KeyValueValidableInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private array $schema = []; |
|
25
|
|
|
private array $keys = []; |
|
26
|
|
|
private bool $isValid = true; |
|
27
|
|
|
|
|
28
|
|
|
private array $validators = []; |
|
29
|
|
|
|
|
30
|
|
|
public function notEmpty(...$keys): self |
|
31
|
|
|
{ |
|
32
|
|
|
$this->validators[] = static function (array $array) use ($keys) { |
|
33
|
|
|
return self::validateNotEmpty($array, $keys); |
|
34
|
|
|
}; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function keys(...$keys): self |
|
40
|
|
|
{ |
|
41
|
|
|
$this->validators[] = static function (array $array) use ($keys) { |
|
42
|
|
|
return self::validateKeys($array, $keys); |
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function hasKeys(...$keys): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->validators[] = static function (array $array) use ($keys) { |
|
51
|
|
|
return self::validateHasKeys($array, $keys); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function key($key): self |
|
58
|
|
|
{ |
|
59
|
|
|
$this->validators[] = static function (array $array) use ($key) { |
|
60
|
|
|
return self::validateKey($array, $key); |
|
61
|
|
|
}; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function hasKey($key): self |
|
67
|
|
|
{ |
|
68
|
|
|
$this->validators[] = static function (array $array) use ($key) { |
|
69
|
|
|
return self::validateHasKey($array, $key); |
|
70
|
|
|
}; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function uValidate($array, callable $fn): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->validate($array) && $fn($array); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function validate($array): bool |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($this->validators as $validatorFn) { |
|
83
|
|
|
$this->isValid = $this->isValid && $validatorFn($array); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->isValid; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function schema(array $schema): self |
|
90
|
|
|
{ |
|
91
|
|
|
$this->schema = $schema; |
|
92
|
|
|
$this->keys = array_keys($schema); |
|
93
|
|
|
|
|
94
|
|
|
$this->validators[] = static function (array $array) use ($schema) { |
|
95
|
|
|
return self::validateSchema($array, $schema); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function validateSchema($array, $schema): bool |
|
102
|
|
|
{ |
|
103
|
|
|
$isValid = true; |
|
104
|
|
|
foreach ($schema as $key => $filter) { |
|
105
|
|
|
if ($filter === 'boolean' && filter_var($array[$key], FILTER_VALIDATE_BOOLEAN)) { |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($filter === 'domain' && filter_var($array[$key], FILTER_VALIDATE_DOMAIN)) { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($filter === 'int' && filter_var($array[$key], FILTER_VALIDATE_INT)) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($filter === 'email' && filter_var($array[$key], FILTER_VALIDATE_EMAIL)) { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($filter === 'mac' && filter_var($array[$key], FILTER_VALIDATE_MAC)) { |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($filter === 'float' && filter_var($array[$key], FILTER_VALIDATE_FLOAT)) { |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($filter === 'regexp' && filter_var($array[$key], FILTER_VALIDATE_REGEXP)) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($filter === 'string' && is_string($array[$key])) { |
|
134
|
|
|
continue; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($filter === 'numeric' && is_numeric($array[$key])) { |
|
138
|
|
|
continue; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$isValid = false; |
|
142
|
|
|
} |
|
143
|
|
|
return $isValid; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public static function validateNotEmpty($array, $keys): bool |
|
147
|
|
|
{ |
|
148
|
|
|
foreach ($keys as $key) { |
|
149
|
|
|
if (empty($array[$key])) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public static function validateHasKeys($array, $keys): bool |
|
157
|
|
|
{ |
|
158
|
|
|
foreach ($keys as $keyToCheck) { |
|
159
|
|
|
if (!array_key_exists($keyToCheck, $array)) { |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
return true; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public static function validateHasKey($array, $key): bool |
|
167
|
|
|
{ |
|
168
|
|
|
return array_key_exists($key, $array); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public static function validateKey($array, $key): bool |
|
172
|
|
|
{ |
|
173
|
|
|
return self::validateHasKey($array, $key); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public static function validateKeys($array, $keys): bool |
|
177
|
|
|
{ |
|
178
|
|
|
$keysToValidate = array_keys($array); |
|
179
|
|
|
|
|
180
|
|
|
sort($keys); |
|
181
|
|
|
sort($keysToValidate); |
|
182
|
|
|
|
|
183
|
|
|
return $keys === $keysToValidate; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|