Passed
Push — master ( 724f49...5f1981 )
by Smoren
02:38
created

ContainerCheckFactory::getHasIndexCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 61
    public static function getNumericCheck(): CheckInterface
17
    {
18 61
        return CheckBuilder::create(CheckName::CONTAINER)
19 61
            ->withPredicate(fn ($value) => \is_array($value) || \is_object($value))
20 61
            ->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 7
    public static function getArrayAccessibleCheck(): CheckInterface
47
    {
48 7
        return CheckBuilder::create(CheckName::ARRAY_ACCESSIBLE)
49 7
            ->withPredicate(fn ($value) => \is_array($value) || $value instanceof \ArrayAccess)
50 7
            ->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, string $name) use ($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 getHasIndexCheck(int $index): CheckInterface
152
    {
153 5
        return CheckBuilder::create(CheckName::HAS_INDEX)
154 5
            ->withPredicate(fn ($value, $index) => isset($value[$index]))
155 5
            ->withParams([Param::INDEX => $index])
156 5
            ->withDependOnChecks([ContainerCheckFactory::getArrayAccessibleCheck()])
157 5
            ->build();
158
    }
159
160 3
    public static function getValueByIndexCheck(int $index, MixedRuleInterface $rule): CheckInterface
161
    {
162 3
        return CheckBuilder::create(CheckName::VALUE_BY_INDEX)
163 3
            ->withPredicate(static function ($value, int $index) use ($rule) {
164 2
                $rule->validate($value[$index]);
165 1
                return true;
166 3
            })
167 3
            ->withParams([Param::INDEX => $index])
168 3
            ->withDependOnChecks([ContainerCheckFactory::getHasIndexCheck($index)])
169 3
            ->build();
170
    }
171
172 5
    public static function getAllKeysAreCheck(MixedRuleInterface $rule): CheckInterface
173
    {
174 5
        return CheckBuilder::create(CheckName::ALL_KEYS_ARE)
175 5
            ->withPredicate(static function ($value) use ($rule) {
176 3
                foreach ($value as $k => $v) {
177 3
                    $rule->validate($k);
178
                }
179 1
                return true;
180 5
            })
181 5
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
182 5
            ->build();
183
    }
184
185 11
    public static function getAllValuesAreCheck(MixedRuleInterface $rule): CheckInterface
186
    {
187 11
        return CheckBuilder::create(CheckName::ALL_VALUES_ARE)
188 11
            ->withPredicate(static function ($value) use ($rule) {
189 9
                foreach ($value as $v) {
190 9
                    $rule->validate($v);
191
                }
192 6
                return true;
193 11
            })
194 11
            ->withDependOnChecks([ContainerCheckFactory::getIterableCheck()])
195 11
            ->build();
196
    }
197
}
198