Passed
Push — master ( 151935...bf98f6 )
by Smoren
02:25
created

ContainerRule::everyValueIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use ArrayAccess;
8
use Smoren\Validator\Exceptions\ValidationError;
9
use Smoren\Validator\Interfaces\BaseRuleInterface;
10
use Smoren\Validator\Interfaces\ContainerRuleInterface;
11
use Smoren\Validator\Interfaces\IntegerRuleInterface;
12
use Smoren\Validator\Structs\Check;
13
14
class ContainerRule extends Rule implements ContainerRuleInterface
15
{
16
    public const ERROR_NOT_CONTAINER = 'not_container';
17
    public const ERROR_NOT_ARRAY = 'not_array';
18
    public const ERROR_NOT_INDEXED_ARRAY = 'not_array';
19
    public const ERROR_NOT_ASSOCIATIVE_ARRAY = 'not_array';
20
    public const ERROR_NOT_ITERABLE = 'not_iterable';
21
    public const ERROR_NOT_COUNTABLE = 'not_countable';
22
    public const ERROR_NOT_EMPTY = 'not_empty';
23
    public const ERROR_EMPTY = 'not_empty';
24
    public const ERROR_NOT_ARRAY_ACCESSIBLE = 'not_array_accessible';
25
    public const ERROR_NOT_OBJECT = 'not_object';
26
    public const ERROR_NOT_STD_OBJECT = 'not_std_object';
27
    public const ERROR_NOT_INSTANCE_OF = 'not_instance_of';
28
    public const ERROR_LENGTH_IS_NOT = 'length_is_not';
29
30
    /**
31
     * ContainerRule constructor.
32
     */
33 11
    public function __construct()
34
    {
35 11
        $this->addCheck(new Check(
36 11
            self::ERROR_NOT_CONTAINER,
37 11
            fn ($value) => is_array($value) || is_object($value)
38 11
        ));
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     *
44
     * @return static
45
     */
46 2
    public function array(): self
47
    {
48 2
        return $this->addCheck(new Check(
49 2
            self::ERROR_NOT_ARRAY,
50 2
            fn ($value) => is_array($value)
51 2
        ));
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     *
57
     * @return static
58
     */
59 2
    public function indexedArray(): self
60
    {
61 2
        return $this->addCheck(new Check(
62 2
            self::ERROR_NOT_INDEXED_ARRAY,
63 2
            fn ($value) => is_array($value) && (array_values($value) === $value)
64 2
        ));
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     *
70
     * @return static
71
     */
72 1
    public function associativeArray(): self
73
    {
74 1
        return $this->addCheck(new Check(
75 1
            self::ERROR_NOT_ASSOCIATIVE_ARRAY,
76 1
            fn ($value) => is_array($value) && (array_values($value) !== $value)
77 1
        ));
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     *
83
     * @return static
84
     */
85
    public function arrayAccessible(): self
86
    {
87
        return $this->addCheck(new Check(
88
            self::ERROR_NOT_ARRAY_ACCESSIBLE,
89
            fn ($value) => is_array($value) || $value instanceof ArrayAccess
90
        ));
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @return static
97
     */
98
    public function iterable(): self
99
    {
100
        return $this->addCheck(new Check(
101
            self::ERROR_NOT_ITERABLE,
102
            fn ($value) => is_iterable($value)
103
        ));
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     *
109
     * @return static
110
     */
111 2
    public function countable(): self
112
    {
113 2
        return $this->addCheck(new Check(
114 2
            self::ERROR_NOT_COUNTABLE,
115 2
            fn ($value) => is_countable($value)
116 2
        ));
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     *
122
     * @return static
123
     */
124
    public function empty(): self
125
    {
126
        if (!$this->isCheckNameUsed(self::ERROR_NOT_COUNTABLE)) {
127
            $this->countable();
128
        }
129
130
        return $this->addCheck(new Check(
131
            self::ERROR_NOT_EMPTY,
132
            fn ($value) => count($value) === 0
133
        ));
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     *
139
     * @return static
140
     */
141
    public function notEmpty(): self
142
    {
143
        if (!$this->isCheckNameUsed(self::ERROR_NOT_COUNTABLE)) {
144
            $this->countable();
145
        }
146
147
        return $this->addCheck(new Check(
148
            self::ERROR_EMPTY,
149
            fn ($value) => count($value) > 0
150
        ));
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     *
156
     * @return static
157
     */
158 2
    public function object(): self
159
    {
160 2
        return $this->addCheck(new Check(
161 2
            self::ERROR_NOT_OBJECT,
162 2
            fn ($value) => is_object($value)
163 2
        ));
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     *
169
     * @return static
170
     */
171
    public function stdObject(): self
172
    {
173
        return $this->addCheck(new Check(
174
            self::ERROR_NOT_STD_OBJECT,
175
            fn ($value) => $value instanceof \stdClass
176
        ));
177
    }
178
179
    /**
180
     * {@inheritDoc}
181
     *
182
     * @return static
183
     */
184
    public function instanceOf(string $class): self
185
    {
186
        return $this->addCheck(new Check(
187
            self::ERROR_NOT_INSTANCE_OF,
188
            fn ($value) => $value instanceof $class
189
        ));
190
    }
191
192 2
    public function lengthIs(IntegerRuleInterface $rule): self
193
    {
194
        if (!$this->isCheckNameUsed(self::ERROR_NOT_COUNTABLE)) {
195
            $this->countable();
196
        }
197
198
        $violations = [];
199 2
        return $this->addCheck(new Check(
200 2
            self::ERROR_LENGTH_IS_NOT,
201 2
            static function ($value) use ($rule, &$violations) {
202
                try {
203
                    /** @var \Countable $value */
204 2
                    $rule->validate(count($value));
205 1
                    return true;
206 1
                } catch (ValidationError $e) {
207 1
                    $violations = $e->getSummary();
208 1
                    return false;
209
                }
210 2
            },
211 2
            ['violations' => &$violations]
212 2
        ));
213
    }
214
215
    public function hasAttribute(string $attribute, ?BaseRuleInterface $rule = null): self
216
    {
217
        // TODO: Implement hasAttribute() method.
218
        return $this;
219
    }
220
221
    public function everyKeyIs(BaseRuleInterface $rule): self
222
    {
223
        // TODO: Implement everyKeyIs() method.
224
        return $this;
225
    }
226
227
    public function everyValueIs(BaseRuleInterface $rule): self
228
    {
229
        // TODO: Implement everyValueIs() method.
230
        return $this;
231
    }
232
}
233