1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Factories\Checks; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Exceptions\ValidationError; |
8
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
9
|
|
|
use Smoren\Validator\Helpers\ContainerAccessHelper; |
10
|
|
|
use Smoren\Validator\Helpers\TypeHelper; |
11
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
12
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
13
|
|
|
use Smoren\Validator\Interfaces\MixedRuleInterface; |
14
|
|
|
use Smoren\Validator\Structs\CheckName; |
15
|
|
|
use Smoren\Validator\Structs\Param; |
16
|
|
|
|
17
|
|
|
class ContainerCheckFactory |
18
|
|
|
{ |
19
|
68 |
|
public static function getNumericCheck(): CheckInterface |
20
|
|
|
{ |
21
|
68 |
|
return CheckBuilder::create(CheckName::CONTAINER) |
22
|
68 |
|
->withPredicate(fn ($value) => \is_array($value) || \is_object($value)) |
23
|
68 |
|
->build(); |
24
|
|
|
} |
25
|
|
|
|
26
|
12 |
|
public static function getArrayCheck(): CheckInterface |
27
|
|
|
{ |
28
|
12 |
|
return CheckBuilder::create(CheckName::ARRAY) |
29
|
12 |
|
->withPredicate(fn ($value) => \is_array($value)) |
30
|
12 |
|
->build(); |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public static function getIndexedArrayCheck(): CheckInterface |
34
|
|
|
{ |
35
|
2 |
|
return CheckBuilder::create(CheckName::INDEXED_ARRAY) |
36
|
2 |
|
->withPredicate(fn ($value) => \array_values($value) === $value) |
37
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getArrayCheck()]) |
38
|
2 |
|
->build(); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public static function getAssociativeArrayCheck(): CheckInterface |
42
|
|
|
{ |
43
|
1 |
|
return CheckBuilder::create(CheckName::ASSOCIATIVE_ARRAY) |
44
|
1 |
|
->withPredicate(fn ($value) => \array_values($value) !== $value) |
45
|
1 |
|
->withDependOnChecks([ContainerCheckFactory::getArrayCheck()]) |
46
|
1 |
|
->build(); |
47
|
|
|
} |
48
|
|
|
|
49
|
7 |
|
public static function getArrayAccessibleCheck(): CheckInterface |
50
|
|
|
{ |
51
|
7 |
|
return CheckBuilder::create(CheckName::ARRAY_ACCESSIBLE) |
52
|
7 |
|
->withPredicate(fn ($value) => \is_array($value) || $value instanceof \ArrayAccess) |
53
|
7 |
|
->build(); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public static function getObjectCheck(): CheckInterface |
57
|
|
|
{ |
58
|
2 |
|
return CheckBuilder::create(CheckName::OBJECT) |
59
|
2 |
|
->withPredicate(fn ($value) => \is_object($value)) |
60
|
2 |
|
->build(); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public static function getStdObjectCheck(): CheckInterface |
64
|
|
|
{ |
65
|
3 |
|
return CheckBuilder::create(CheckName::STD_OBJECT) |
66
|
3 |
|
->withPredicate(fn ($value) => $value instanceof \stdClass) |
67
|
3 |
|
->build(); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
public static function getInstanceOfCheck(string $class): CheckInterface |
71
|
|
|
{ |
72
|
3 |
|
return CheckBuilder::create(CheckName::INSTANCE_OF) |
73
|
3 |
|
->withPredicate(fn ($value) => $value instanceof $class) |
74
|
3 |
|
->withCalculatedParams([Param::GIVEN_TYPE => fn ($value) => TypeHelper::getType($value)]) |
75
|
3 |
|
->build(); |
76
|
|
|
} |
77
|
|
|
|
78
|
15 |
|
public static function getCountableCheck(): CheckInterface |
79
|
|
|
{ |
80
|
15 |
|
return CheckBuilder::create(CheckName::COUNTABLE) |
81
|
15 |
|
->withPredicate(fn ($value) => \is_countable($value)) |
82
|
15 |
|
->build(); |
83
|
|
|
} |
84
|
|
|
|
85
|
23 |
|
public static function getIterableCheck(): CheckInterface |
86
|
|
|
{ |
87
|
23 |
|
return CheckBuilder::create(CheckName::ITERABLE) |
88
|
23 |
|
->withPredicate(fn ($value) => \is_iterable($value)) |
89
|
23 |
|
->build(); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public static function getEmptyCheck(): CheckInterface |
93
|
|
|
{ |
94
|
2 |
|
return CheckBuilder::create(CheckName::EMPTY) |
95
|
2 |
|
->withPredicate(fn ($value) => \count($value) === 0) |
96
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
97
|
2 |
|
->build(); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public static function getNotEmptyCheck(): CheckInterface |
101
|
|
|
{ |
102
|
2 |
|
return CheckBuilder::create(CheckName::NOT_EMPTY) |
103
|
2 |
|
->withPredicate(fn ($value) => \count($value) > 0) |
104
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
105
|
2 |
|
->build(); |
106
|
|
|
} |
107
|
|
|
|
108
|
9 |
|
public static function getLengthIsCheck(IntegerRuleInterface $rule): CheckInterface |
109
|
|
|
{ |
110
|
9 |
|
return CheckBuilder::create(CheckName::LENGTH_IS) |
111
|
9 |
|
->withPredicate(static function ($value) use ($rule) { |
112
|
|
|
/** @var \Countable $value */ |
113
|
8 |
|
$rule->validate(\count($value)); |
114
|
7 |
|
return true; |
115
|
9 |
|
}) |
116
|
9 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
117
|
9 |
|
->build(); |
118
|
|
|
} |
119
|
|
|
|
120
|
21 |
|
public static function getHasAttributeCheck(string $name): CheckInterface |
121
|
|
|
{ |
122
|
21 |
|
return CheckBuilder::create(CheckName::HAS_ATTRIBUTE) |
123
|
21 |
|
->withPredicate(fn ($value) => ContainerAccessHelper::hasAccessibleAttribute($value, $name)) |
124
|
21 |
|
->withParams([Param::ATTRIBUTE => $name]) |
125
|
21 |
|
->build(); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
public static function getHasOptionalAttributeCheck(string $name, MixedRuleInterface $rule): CheckInterface |
129
|
|
|
{ |
130
|
2 |
|
return CheckBuilder::create(CheckName::ATTRIBUTE_IS) |
131
|
2 |
|
->withPredicate(static function ($value, string $name) use ($rule) { |
132
|
2 |
|
if (!ContainerAccessHelper::hasAccessibleAttribute($value, $name)) { |
133
|
1 |
|
return true; |
134
|
|
|
} |
135
|
2 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
136
|
1 |
|
return true; |
137
|
2 |
|
}) |
138
|
2 |
|
->withParams([Param::ATTRIBUTE => $name]) |
139
|
2 |
|
->build(); |
140
|
|
|
} |
141
|
|
|
|
142
|
19 |
|
public static function getAttributeIsCheck(string $name, MixedRuleInterface $rule): CheckInterface |
143
|
|
|
{ |
144
|
19 |
|
return CheckBuilder::create(CheckName::ATTRIBUTE_IS) |
145
|
19 |
|
->withPredicate(static function ($value, string $name) use ($rule) { |
146
|
11 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
147
|
8 |
|
return true; |
148
|
19 |
|
}) |
149
|
19 |
|
->withParams([Param::ATTRIBUTE => $name]) |
150
|
19 |
|
->withDependOnChecks([ContainerCheckFactory::getHasAttributeCheck($name)]) |
151
|
19 |
|
->build(); |
152
|
|
|
} |
153
|
|
|
|
154
|
5 |
|
public static function getHasIndexCheck(int $index): CheckInterface |
155
|
|
|
{ |
156
|
5 |
|
return CheckBuilder::create(CheckName::HAS_INDEX) |
157
|
5 |
|
->withPredicate(fn ($value, $index) => isset($value[$index])) |
158
|
5 |
|
->withParams([Param::INDEX => $index]) |
159
|
5 |
|
->withDependOnChecks([ContainerCheckFactory::getArrayAccessibleCheck()]) |
160
|
5 |
|
->build(); |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
public static function getValueByIndexIsCheck(int $index, MixedRuleInterface $rule): CheckInterface |
164
|
|
|
{ |
165
|
3 |
|
return CheckBuilder::create(CheckName::VALUE_BY_INDEX_IS) |
166
|
3 |
|
->withPredicate(static function ($value, int $index) use ($rule) { |
167
|
2 |
|
$rule->validate($value[$index]); |
168
|
1 |
|
return true; |
169
|
3 |
|
}) |
170
|
3 |
|
->withParams([Param::INDEX => $index]) |
171
|
3 |
|
->withDependOnChecks([ContainerCheckFactory::getHasIndexCheck($index)]) |
172
|
3 |
|
->build(); |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
public static function getAllKeysAreCheck(MixedRuleInterface $rule): CheckInterface |
176
|
|
|
{ |
177
|
6 |
|
return CheckBuilder::create(CheckName::ALL_KEYS_ARE) |
178
|
6 |
|
->withPredicate(static function ($value) use ($rule) { |
179
|
3 |
|
foreach ($value as $k => $v) { |
180
|
3 |
|
$rule->validate($k); |
181
|
|
|
} |
182
|
1 |
|
return true; |
183
|
6 |
|
}) |
184
|
6 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
185
|
6 |
|
->build(); |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
public static function getSomeKeyIsCheck(MixedRuleInterface $rule): CheckInterface |
189
|
|
|
{ |
190
|
3 |
|
return CheckBuilder::create(CheckName::SOME_KEY_IS) |
191
|
3 |
|
->withPredicate(static function ($value) use ($rule) { |
192
|
3 |
|
$errorMap = []; |
193
|
3 |
|
foreach ($value as $k => $v) { |
194
|
|
|
try { |
195
|
3 |
|
$rule->validate($k); |
196
|
1 |
|
return true; |
197
|
3 |
|
} catch (ValidationError $e) { |
198
|
3 |
|
foreach ($e->getViolatedRestrictions() as $restriction) { |
199
|
3 |
|
$errorMap[serialize($restriction)] = $restriction; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
2 |
|
if (\count($errorMap) === 0) { |
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
throw new ValidationError($rule->getName(), $value, \array_values($errorMap)); |
209
|
3 |
|
}) |
210
|
3 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
211
|
3 |
|
->build(); |
212
|
|
|
} |
213
|
|
|
|
214
|
12 |
|
public static function getAllValuesAreCheck(MixedRuleInterface $rule): CheckInterface |
215
|
|
|
{ |
216
|
12 |
|
return CheckBuilder::create(CheckName::ALL_VALUES_ARE) |
217
|
12 |
|
->withPredicate(static function ($value) use ($rule) { |
218
|
10 |
|
foreach ($value as $v) { |
219
|
10 |
|
$rule->validate($v); |
220
|
|
|
} |
221
|
7 |
|
return true; |
222
|
12 |
|
}) |
223
|
12 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
224
|
12 |
|
->build(); |
225
|
|
|
} |
226
|
|
|
|
227
|
3 |
|
public static function getSomeValueIsCheck(MixedRuleInterface $rule): CheckInterface |
228
|
|
|
{ |
229
|
3 |
|
return CheckBuilder::create(CheckName::SOME_VALUE_IS) |
230
|
3 |
|
->withPredicate(static function ($value) use ($rule) { |
231
|
3 |
|
$errorMap = []; |
232
|
3 |
|
foreach ($value as $v) { |
233
|
|
|
try { |
234
|
3 |
|
$rule->validate($v); |
235
|
1 |
|
return true; |
236
|
3 |
|
} catch (ValidationError $e) { |
237
|
3 |
|
foreach ($e->getViolatedRestrictions() as $restriction) { |
238
|
3 |
|
$errorMap[serialize($restriction)] = $restriction; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
2 |
|
if (\count($errorMap) === 0) { |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
|
247
|
2 |
|
throw new ValidationError($rule->getName(), $value, \array_values($errorMap)); |
248
|
3 |
|
}) |
249
|
3 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
250
|
3 |
|
->build(); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|