|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Rules; |
|
6
|
|
|
|
|
7
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
|
8
|
|
|
use Smoren\Validator\Helpers\ContainerAccessHelper; |
|
9
|
|
|
use Smoren\Validator\Helpers\TypeHelper; |
|
10
|
|
|
use Smoren\Validator\Interfaces\RuleInterface; |
|
11
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
|
12
|
|
|
use Smoren\Validator\Interfaces\ContainerRuleInterface; |
|
13
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
|
14
|
|
|
use Smoren\Validator\Structs\CheckName; |
|
15
|
|
|
use Smoren\Validator\Structs\Param; |
|
16
|
|
|
|
|
17
|
|
|
class ContainerMixedRule extends MixedRule implements ContainerRuleInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* ContainerRule constructor. |
|
21
|
|
|
*/ |
|
22
|
50 |
|
public function __construct(string $name) |
|
23
|
|
|
{ |
|
24
|
50 |
|
parent::__construct($name); |
|
25
|
50 |
|
$this->check( |
|
26
|
50 |
|
CheckBuilder::create(CheckName::CONTAINER) |
|
27
|
50 |
|
->withPredicate(fn ($value) => \is_array($value) || \is_object($value)) |
|
28
|
50 |
|
->build(), |
|
29
|
50 |
|
true |
|
30
|
50 |
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritDoc} |
|
35
|
|
|
* |
|
36
|
|
|
* @return static |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public function array(): self |
|
39
|
|
|
{ |
|
40
|
5 |
|
return $this->check($this->getArrayCheck()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
* |
|
46
|
|
|
* @return static |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function indexedArray(): self |
|
49
|
|
|
{ |
|
50
|
2 |
|
return $this->check( |
|
51
|
2 |
|
CheckBuilder::create(CheckName::INDEXED_ARRAY) |
|
52
|
2 |
|
->withPredicate(fn ($value) => \array_values($value) === $value) |
|
53
|
2 |
|
->withDependOnChecks([$this->getArrayCheck()]) |
|
54
|
2 |
|
->build() |
|
55
|
2 |
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
* |
|
61
|
|
|
* @return static |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function associativeArray(): self |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->check( |
|
66
|
1 |
|
CheckBuilder::create(CheckName::ASSOCIATIVE_ARRAY) |
|
67
|
1 |
|
->withPredicate(fn ($value) => \array_values($value) !== $value) |
|
68
|
1 |
|
->withDependOnChecks([$this->getArrayCheck()]) |
|
69
|
1 |
|
->build() |
|
70
|
1 |
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
* |
|
76
|
|
|
* @return static |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function arrayAccessible(): self |
|
79
|
|
|
{ |
|
80
|
2 |
|
return $this->check( |
|
81
|
2 |
|
CheckBuilder::create(CheckName::ARRAY_ACCESSIBLE) |
|
82
|
2 |
|
->withPredicate(fn ($value) => \is_array($value) || $value instanceof \ArrayAccess) |
|
83
|
2 |
|
->build() |
|
84
|
2 |
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
* |
|
90
|
|
|
* @return static |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function iterable(): self |
|
93
|
|
|
{ |
|
94
|
2 |
|
return $this->check($this->getIterableCheck()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritDoc} |
|
99
|
|
|
* |
|
100
|
|
|
* @return static |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function countable(): self |
|
103
|
|
|
{ |
|
104
|
2 |
|
return $this->check($this->getCountableCheck()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritDoc} |
|
109
|
|
|
* |
|
110
|
|
|
* @return static |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function empty(): self |
|
113
|
|
|
{ |
|
114
|
2 |
|
return $this->check( |
|
115
|
2 |
|
CheckBuilder::create(CheckName::EMPTY) |
|
116
|
2 |
|
->withPredicate(fn ($value) => \count($value) === 0) |
|
117
|
2 |
|
->withDependOnChecks([$this->getCountableCheck()]) |
|
118
|
2 |
|
->build() |
|
119
|
2 |
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritDoc} |
|
124
|
|
|
* |
|
125
|
|
|
* @return static |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function notEmpty(): self |
|
128
|
|
|
{ |
|
129
|
2 |
|
return $this->check( |
|
130
|
2 |
|
CheckBuilder::create(CheckName::NOT_EMPTY) |
|
131
|
2 |
|
->withPredicate(fn ($value) => \count($value) > 0) |
|
132
|
2 |
|
->withDependOnChecks([$this->getCountableCheck()]) |
|
133
|
2 |
|
->build() |
|
134
|
2 |
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritDoc} |
|
139
|
|
|
* |
|
140
|
|
|
* @return static |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function object(): self |
|
143
|
|
|
{ |
|
144
|
2 |
|
return $this->check( |
|
145
|
2 |
|
CheckBuilder::create(CheckName::OBJECT) |
|
146
|
2 |
|
->withPredicate(fn ($value) => \is_object($value)) |
|
147
|
2 |
|
->build() |
|
148
|
2 |
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritDoc} |
|
153
|
|
|
* |
|
154
|
|
|
* @return static |
|
155
|
|
|
*/ |
|
156
|
2 |
|
public function stdObject(): self |
|
157
|
|
|
{ |
|
158
|
2 |
|
return $this->check( |
|
159
|
2 |
|
CheckBuilder::create(CheckName::STD_OBJECT) |
|
160
|
2 |
|
->withPredicate(fn ($value) => $value instanceof \stdClass) |
|
161
|
2 |
|
->build() |
|
162
|
2 |
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritDoc} |
|
167
|
|
|
* |
|
168
|
|
|
* @return static |
|
169
|
|
|
*/ |
|
170
|
3 |
|
public function instanceOf(string $class): self |
|
171
|
|
|
{ |
|
172
|
3 |
|
return $this->check( |
|
173
|
3 |
|
CheckBuilder::create(CheckName::INSTANCE_OF) |
|
174
|
3 |
|
->withPredicate(fn ($value) => $value instanceof $class) |
|
175
|
3 |
|
->withCalculatedParams([Param::GIVEN_TYPE => fn ($value) => TypeHelper::getType($value)]) |
|
176
|
3 |
|
->build() |
|
177
|
3 |
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inheritDoc} |
|
182
|
|
|
* |
|
183
|
|
|
* @return static |
|
184
|
|
|
*/ |
|
185
|
5 |
|
public function lengthIs(IntegerRuleInterface $rule): self |
|
186
|
|
|
{ |
|
187
|
5 |
|
return $this->check( |
|
188
|
5 |
|
CheckBuilder::create(CheckName::LENGTH_IS) |
|
189
|
5 |
|
->withPredicate(static function ($value) use ($rule) { |
|
190
|
|
|
/** @var \Countable $value */ |
|
191
|
5 |
|
$rule->validate(\count($value)); |
|
192
|
4 |
|
return true; |
|
193
|
5 |
|
}) |
|
194
|
5 |
|
->withDependOnChecks([$this->getCountableCheck()]) |
|
195
|
5 |
|
->build() |
|
196
|
5 |
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritDoc} |
|
201
|
|
|
* |
|
202
|
|
|
* @return static |
|
203
|
|
|
*/ |
|
204
|
17 |
|
public function hasAttribute(string $name, ?RuleInterface $rule = null): self |
|
205
|
|
|
{ |
|
206
|
17 |
|
if ($rule === null) { |
|
207
|
2 |
|
return $this->check($this->getHasAttributeCheck($name)); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
15 |
|
return $this->check( |
|
211
|
15 |
|
CheckBuilder::create(CheckName::ATTRIBUTE_IS) |
|
212
|
15 |
|
->withPredicate(static function ($value, string $name) use ($rule) { |
|
213
|
8 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
|
214
|
5 |
|
return true; |
|
215
|
15 |
|
}) |
|
216
|
15 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
217
|
15 |
|
->withDependOnChecks([$this->getHasAttributeCheck($name)]) |
|
218
|
15 |
|
->build() |
|
219
|
15 |
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* {@inheritDoc} |
|
224
|
|
|
* |
|
225
|
|
|
* @return static |
|
226
|
|
|
*/ |
|
227
|
2 |
|
public function hasOptionalAttribute(string $name, RuleInterface $rule): self |
|
228
|
|
|
{ |
|
229
|
2 |
|
return $this->check( |
|
230
|
2 |
|
CheckBuilder::create(CheckName::HAS_ATTRIBUTE) |
|
231
|
2 |
|
->withPredicate(static function ($value) use ($name, $rule) { |
|
232
|
2 |
|
if (!ContainerAccessHelper::hasAccessibleAttribute($value, $name)) { |
|
233
|
1 |
|
return true; |
|
234
|
|
|
} |
|
235
|
2 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
|
236
|
1 |
|
return true; |
|
237
|
2 |
|
}) |
|
238
|
2 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
239
|
2 |
|
->build() |
|
240
|
2 |
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* {@inheritDoc} |
|
245
|
|
|
* |
|
246
|
|
|
* @return static |
|
247
|
|
|
*/ |
|
248
|
3 |
|
public function allKeysAre(RuleInterface $rule): self |
|
249
|
|
|
{ |
|
250
|
3 |
|
return $this->check( |
|
251
|
3 |
|
CheckBuilder::create(CheckName::ALL_KEYS_ARE) |
|
252
|
3 |
|
->withPredicate(static function ($value) use ($rule) { |
|
253
|
3 |
|
foreach ($value as $k => $v) { |
|
254
|
3 |
|
$rule->validate($k); |
|
255
|
|
|
} |
|
256
|
1 |
|
return true; |
|
257
|
3 |
|
}) |
|
258
|
3 |
|
->withDependOnChecks([$this->getIterableCheck()]) |
|
259
|
3 |
|
->build() |
|
260
|
3 |
|
); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* {@inheritDoc} |
|
265
|
|
|
* |
|
266
|
|
|
* @return static |
|
267
|
|
|
*/ |
|
268
|
5 |
|
public function allValuesAre(RuleInterface $rule): self |
|
269
|
|
|
{ |
|
270
|
5 |
|
return $this->check( |
|
271
|
5 |
|
CheckBuilder::create(CheckName::ALL_VALUES_ARE) |
|
272
|
5 |
|
->withPredicate(static function ($value) use ($rule) { |
|
273
|
5 |
|
foreach ($value as $v) { |
|
274
|
5 |
|
$rule->validate($v); |
|
275
|
|
|
} |
|
276
|
2 |
|
return true; |
|
277
|
5 |
|
}) |
|
278
|
5 |
|
->withDependOnChecks([$this->getIterableCheck()]) |
|
279
|
5 |
|
->build() |
|
280
|
5 |
|
); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
8 |
|
protected function getArrayCheck(): CheckInterface |
|
284
|
|
|
{ |
|
285
|
8 |
|
return CheckBuilder::create(CheckName::ARRAY) |
|
286
|
8 |
|
->withPredicate(fn ($value) => \is_array($value)) |
|
287
|
8 |
|
->build(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
11 |
|
protected function getCountableCheck(): CheckInterface |
|
291
|
|
|
{ |
|
292
|
11 |
|
return CheckBuilder::create(CheckName::COUNTABLE) |
|
293
|
11 |
|
->withPredicate(fn ($value) => \is_countable($value)) |
|
294
|
11 |
|
->build(); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
10 |
|
protected function getIterableCheck(): CheckInterface |
|
298
|
|
|
{ |
|
299
|
10 |
|
return CheckBuilder::create(CheckName::ITERABLE) |
|
300
|
10 |
|
->withPredicate(fn ($value) => \is_iterable($value)) |
|
301
|
10 |
|
->build(); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
17 |
|
protected function getHasAttributeCheck(string $name): CheckInterface |
|
305
|
|
|
{ |
|
306
|
17 |
|
return CheckBuilder::create(CheckName::HAS_ATTRIBUTE) |
|
307
|
17 |
|
->withPredicate(fn ($value) => ContainerAccessHelper::hasAccessibleAttribute($value, $name)) |
|
308
|
17 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
309
|
17 |
|
->build(); |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|