Passed
Push — master ( 17adcb...9acb8c )
by Smoren
12:18
created

ContainerRule::empty()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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