Passed
Push — master ( 1d6156...c0cf33 )
by Smoren
03:04
created

ContainerCheckFactory::getValueByIndexIsCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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