Passed
Push — master ( c0cf33...158c78 )
by Smoren
02:24
created

ContainerCheckFactory::getAnyValueIsCheck()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 1
nop 1
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 30
rs 9.4555
c 0
b 0
f 0
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 61
    public static function getNumericCheck(): CheckInterface
20
    {
21 61
        return CheckBuilder::create(CheckName::CONTAINER)
22 61
            ->withPredicate(fn ($value) => \is_array($value) || \is_object($value))
23 61
            ->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 16
    public static function getIterableCheck(): CheckInterface
86
    {
87 16
        return CheckBuilder::create(CheckName::ITERABLE)
88 16
            ->withPredicate(fn ($value) => \is_iterable($value))
89 16
            ->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 5
    public static function getAllKeysAreCheck(MixedRuleInterface $rule): CheckInterface
176
    {
177 5
        return CheckBuilder::create(CheckName::ALL_KEYS_ARE)
178 5
            ->withPredicate(static function ($value) use ($rule) {
179 3
                foreach ($value as $k => $v) {
180 3
                    $rule->validate($k);
181
                }
182 1
                return true;
183 5
            })
184 5
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
185 5
            ->build();
186
    }
187
188
    public static function getAnyKeyIsCheck(MixedRuleInterface $rule): CheckInterface
189
    {
190
        return CheckBuilder::create(CheckName::ANY_KEY_IS)
191
            ->withPredicate(static function ($value) use ($rule) {
192
                $errorMap = [];
193
                foreach ($value as $k => $v) {
194
                    try {
195
                        $rule->validate($k);
196
                        return true;
197
                    } catch (ValidationError $e) {
198
                        foreach ($e->getViolatedRestrictions() as $restriction) {
199
                            $errorMap[serialize($restriction)] = $restriction;
200
                        }
201
                    }
202
                }
203
204
                if (\count($errorMap) === 0) {
205
                    return true;
206
                }
207
208
                throw new ValidationError($rule->getName(), $value, \array_values($errorMap));
209
            })
210
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
211
            ->build();
212
    }
213
214 11
    public static function getAllValuesAreCheck(MixedRuleInterface $rule): CheckInterface
215
    {
216 11
        return CheckBuilder::create(CheckName::ALL_VALUES_ARE)
217 11
            ->withPredicate(static function ($value) use ($rule) {
218 9
                foreach ($value as $v) {
219 9
                    $rule->validate($v);
220
                }
221 6
                return true;
222 11
            })
223 11
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
224 11
            ->build();
225
    }
226
227
    public static function getAnyValueIsCheck(MixedRuleInterface $rule): CheckInterface
228
    {
229
        return CheckBuilder::create(CheckName::ANY_VALUE_IS)
230
            ->withPredicate(static function ($value) use ($rule) {
231
                $errorMap = [];
232
                foreach ($value as $v) {
233
                    try {
234
                        $rule->validate($v);
235
                        return true;
236
                    } catch (ValidationError $e) {
237
                        foreach ($e->getViolatedRestrictions() as $restriction) {
238
                            $errorMap[serialize($restriction)] = $restriction;
239
                        }
240
                    }
241
                }
242
243
                if (\count($errorMap) === 0) {
244
                    return true;
245
                }
246
247
                throw new ValidationError($rule->getName(), $value, \array_values($errorMap));
248
            })
249
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
250
            ->build();
251
    }
252
}
253