|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Factories\Checks; |
|
4
|
|
|
|
|
5
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
|
6
|
|
|
use Smoren\Validator\Helpers\ContainerAccessHelper; |
|
7
|
|
|
use Smoren\Validator\Helpers\TypeHelper; |
|
8
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
|
9
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
|
10
|
|
|
use Smoren\Validator\Interfaces\MixedRuleInterface; |
|
11
|
|
|
use Smoren\Validator\Structs\CheckName; |
|
12
|
|
|
use Smoren\Validator\Structs\Param; |
|
13
|
|
|
|
|
14
|
|
|
class ContainerCheckFactory |
|
15
|
|
|
{ |
|
16
|
56 |
|
public static function getNumericCheck(): CheckInterface |
|
17
|
|
|
{ |
|
18
|
56 |
|
return CheckBuilder::create(CheckName::CONTAINER) |
|
19
|
56 |
|
->withPredicate(fn ($value) => \is_array($value) || \is_object($value)) |
|
20
|
56 |
|
->build(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
12 |
|
public static function getArrayCheck(): CheckInterface |
|
24
|
|
|
{ |
|
25
|
12 |
|
return CheckBuilder::create(CheckName::ARRAY) |
|
26
|
12 |
|
->withPredicate(fn ($value) => \is_array($value)) |
|
27
|
12 |
|
->build(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
public static function getIndexedArrayCheck(): CheckInterface |
|
31
|
|
|
{ |
|
32
|
2 |
|
return CheckBuilder::create(CheckName::INDEXED_ARRAY) |
|
33
|
2 |
|
->withPredicate(fn ($value) => \array_values($value) === $value) |
|
34
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getArrayCheck()]) |
|
35
|
2 |
|
->build(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public static function getAssociativeArrayCheck(): CheckInterface |
|
39
|
|
|
{ |
|
40
|
1 |
|
return CheckBuilder::create(CheckName::ASSOCIATIVE_ARRAY) |
|
41
|
1 |
|
->withPredicate(fn ($value) => \array_values($value) !== $value) |
|
42
|
1 |
|
->withDependOnChecks([ContainerCheckFactory::getArrayCheck()]) |
|
43
|
1 |
|
->build(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public static function getArrayAccessibleCheck(): CheckInterface |
|
47
|
|
|
{ |
|
48
|
2 |
|
return CheckBuilder::create(CheckName::ARRAY_ACCESSIBLE) |
|
49
|
2 |
|
->withPredicate(fn ($value) => \is_array($value) || $value instanceof \ArrayAccess) |
|
50
|
2 |
|
->build(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public static function getObjectCheck(): CheckInterface |
|
54
|
|
|
{ |
|
55
|
2 |
|
return CheckBuilder::create(CheckName::OBJECT) |
|
56
|
2 |
|
->withPredicate(fn ($value) => \is_object($value)) |
|
57
|
2 |
|
->build(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public static function getStdObjectCheck(): CheckInterface |
|
61
|
|
|
{ |
|
62
|
3 |
|
return CheckBuilder::create(CheckName::STD_OBJECT) |
|
63
|
3 |
|
->withPredicate(fn ($value) => $value instanceof \stdClass) |
|
64
|
3 |
|
->build(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
public static function getInstanceOfCheck(string $class): CheckInterface |
|
68
|
|
|
{ |
|
69
|
3 |
|
return CheckBuilder::create(CheckName::INSTANCE_OF) |
|
70
|
3 |
|
->withPredicate(fn ($value) => $value instanceof $class) |
|
71
|
3 |
|
->withCalculatedParams([Param::GIVEN_TYPE => fn ($value) => TypeHelper::getType($value)]) |
|
72
|
3 |
|
->build(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
15 |
|
public static function getCountableCheck(): CheckInterface |
|
76
|
|
|
{ |
|
77
|
15 |
|
return CheckBuilder::create(CheckName::COUNTABLE) |
|
78
|
15 |
|
->withPredicate(fn ($value) => \is_countable($value)) |
|
79
|
15 |
|
->build(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
16 |
|
public static function getIterableCheck(): CheckInterface |
|
83
|
|
|
{ |
|
84
|
16 |
|
return CheckBuilder::create(CheckName::ITERABLE) |
|
85
|
16 |
|
->withPredicate(fn ($value) => \is_iterable($value)) |
|
86
|
16 |
|
->build(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public static function getEmptyCheck(): CheckInterface |
|
90
|
|
|
{ |
|
91
|
2 |
|
return CheckBuilder::create(CheckName::EMPTY) |
|
92
|
2 |
|
->withPredicate(fn ($value) => \count($value) === 0) |
|
93
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
|
94
|
2 |
|
->build(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public static function getNotEmptyCheck(): CheckInterface |
|
98
|
|
|
{ |
|
99
|
2 |
|
return CheckBuilder::create(CheckName::NOT_EMPTY) |
|
100
|
2 |
|
->withPredicate(fn ($value) => \count($value) > 0) |
|
101
|
2 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
|
102
|
2 |
|
->build(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
9 |
|
public static function getLengthIsCheck(IntegerRuleInterface $rule): CheckInterface |
|
106
|
|
|
{ |
|
107
|
9 |
|
return CheckBuilder::create(CheckName::LENGTH_IS) |
|
108
|
9 |
|
->withPredicate(static function ($value) use ($rule) { |
|
109
|
|
|
/** @var \Countable $value */ |
|
110
|
8 |
|
$rule->validate(\count($value)); |
|
111
|
7 |
|
return true; |
|
112
|
9 |
|
}) |
|
113
|
9 |
|
->withDependOnChecks([ContainerCheckFactory::getCountableCheck()]) |
|
114
|
9 |
|
->build(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
21 |
|
public static function getHasAttributeCheck(string $name): CheckInterface |
|
118
|
|
|
{ |
|
119
|
21 |
|
return CheckBuilder::create(CheckName::HAS_ATTRIBUTE) |
|
120
|
21 |
|
->withPredicate(fn ($value) => ContainerAccessHelper::hasAccessibleAttribute($value, $name)) |
|
121
|
21 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
122
|
21 |
|
->build(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
public static function getHasOptionalAttributeCheck(string $name, MixedRuleInterface $rule): CheckInterface |
|
126
|
|
|
{ |
|
127
|
2 |
|
return CheckBuilder::create(CheckName::HAS_ATTRIBUTE) |
|
128
|
2 |
|
->withPredicate(static function ($value) use ($name, $rule) { |
|
129
|
2 |
|
if (!ContainerAccessHelper::hasAccessibleAttribute($value, $name)) { |
|
130
|
1 |
|
return true; |
|
131
|
|
|
} |
|
132
|
2 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
|
133
|
1 |
|
return true; |
|
134
|
2 |
|
}) |
|
135
|
2 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
136
|
2 |
|
->build(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
19 |
|
public static function getAttributeIsCheck(string $name, MixedRuleInterface $rule): CheckInterface |
|
140
|
|
|
{ |
|
141
|
19 |
|
return CheckBuilder::create(CheckName::ATTRIBUTE_IS) |
|
142
|
19 |
|
->withPredicate(static function ($value, string $name) use ($rule) { |
|
143
|
11 |
|
$rule->validate(ContainerAccessHelper::getAttributeValue($value, $name)); |
|
144
|
8 |
|
return true; |
|
145
|
19 |
|
}) |
|
146
|
19 |
|
->withParams([Param::ATTRIBUTE => $name]) |
|
147
|
19 |
|
->withDependOnChecks([ContainerCheckFactory::getHasAttributeCheck($name)]) |
|
148
|
19 |
|
->build(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
5 |
|
public static function getAllKeysAreCheck(MixedRuleInterface $rule): CheckInterface |
|
152
|
|
|
{ |
|
153
|
5 |
|
return CheckBuilder::create(CheckName::ALL_KEYS_ARE) |
|
154
|
5 |
|
->withPredicate(static function ($value) use ($rule) { |
|
155
|
3 |
|
foreach ($value as $k => $v) { |
|
156
|
3 |
|
$rule->validate($k); |
|
157
|
|
|
} |
|
158
|
1 |
|
return true; |
|
159
|
5 |
|
}) |
|
160
|
5 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
|
161
|
5 |
|
->build(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
11 |
|
public static function getAllValuesAreCheck(MixedRuleInterface $rule): CheckInterface |
|
165
|
|
|
{ |
|
166
|
11 |
|
return CheckBuilder::create(CheckName::ALL_VALUES_ARE) |
|
167
|
11 |
|
->withPredicate(static function ($value) use ($rule) { |
|
168
|
9 |
|
foreach ($value as $v) { |
|
169
|
9 |
|
$rule->validate($v); |
|
170
|
|
|
} |
|
171
|
6 |
|
return true; |
|
172
|
11 |
|
}) |
|
173
|
11 |
|
->withDependOnChecks([ContainerCheckFactory::getIterableCheck()]) |
|
174
|
11 |
|
->build(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|