ArrayValidator::schema()   D
last analyzed

Complexity

Conditions 20
Paths 1

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 28.7808

Importance

Changes 0
Metric Value
cc 20
eloc 25
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 49
ccs 18
cts 25
cp 0.72
crap 28.7808
rs 4.1666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\arraytools;
6
7
use function array_key_exists;
8
use function array_keys;
9
use function filter_var;
10
use function is_numeric;
11
use function is_string;
12
use function sort;
13
14
use const FILTER_VALIDATE_BOOLEAN;
15
use const FILTER_VALIDATE_DOMAIN;
16
use const FILTER_VALIDATE_EMAIL;
17
use const FILTER_VALIDATE_FLOAT;
18
use const FILTER_VALIDATE_INT;
19
use const FILTER_VALIDATE_MAC;
20
use const FILTER_VALIDATE_REGEXP;
21
22
class ArrayValidator extends AbstractValidator
23
{
24
    private array $schema = [];
25
    private array $keys = [];
26
27 2
    public function notEmpty(...$keys): self
28
    {
29
        $this->validators[] = static function (array $array) use ($keys) {
30 2
            foreach ($keys as $key) {
31 2
                if (empty($array[$key])) {
32
                    return false;
33
                }
34
            }
35 2
            return true;
36
        };
37
38 2
        return $this;
39
    }
40
41 1
    public function keys(...$keys): self
42
    {
43
        $this->validators[] = static function (array $array) use ($keys) {
44 1
            $keysToValidate = array_keys($array);
45
46 1
            sort($keys);
47 1
            sort($keysToValidate);
48
49 1
            return $keys === $keysToValidate;
50
        };
51
52 1
        return $this;
53
    }
54
55 1
    public function hasKeys(...$keys): self
56
    {
57
        $this->validators[] = static function (array $array) use ($keys) {
58 1
            foreach ($keys as $keyToCheck) {
59 1
                if (!array_key_exists($keyToCheck, $array)) {
60 1
                    return false;
61
                }
62
            }
63 1
            return true;
64
        };
65
66 1
        return $this;
67
    }
68
69
    public function key($key): self
70
    {
71
        $this->validators[] = static function (array $array) use ($key) {
72
            return array_key_exists($key, $array);
73
        };
74
75
        return $this;
76
    }
77
78 1
    public function hasKey($key): self
79
    {
80
        $this->validators[] = static function (array $array) use ($key) {
81 1
            return array_key_exists($key, $array);
82
        };
83
84 1
        return $this;
85
    }
86
87 4
    public function schema(array $schema): self
88
    {
89 4
        $this->schema = $schema;
90 4
        $this->keys = array_keys($schema);
91
92
        $this->validators[] = static function (array $array) use ($schema) {
93 4
            foreach ($schema as $key => $filter) {
94 4
                if ($filter === 'boolean' && filter_var($array[$key], FILTER_VALIDATE_BOOLEAN)) {
95
                    continue;
96
                }
97
98 4
                if ($filter === 'domain' && filter_var($array[$key], FILTER_VALIDATE_DOMAIN)) {
99
                    continue;
100
                }
101
102 4
                if ($filter === 'int' && filter_var($array[$key], FILTER_VALIDATE_INT)) {
103
                    continue;
104
                }
105
106 4
                if ($filter === 'email' && filter_var($array[$key], FILTER_VALIDATE_EMAIL)) {
107 1
                    continue;
108
                }
109
110 4
                if ($filter === 'mac' && filter_var($array[$key], FILTER_VALIDATE_MAC)) {
111
                    continue;
112
                }
113
114 4
                if ($filter === 'float' && filter_var($array[$key], FILTER_VALIDATE_FLOAT)) {
115
                    continue;
116
                }
117
118 4
                if ($filter === 'regexp' && filter_var($array[$key], FILTER_VALIDATE_REGEXP)) {
119
                    continue;
120
                }
121
122 4
                if ($filter === 'string' && is_string($array[$key])) {
123 3
                    continue;
124
                }
125
126 2
                if ($filter === 'numeric' && is_numeric($array[$key])) {
127
                    continue;
128
                }
129
130 2
                return false;
131
            }
132 2
            return true;
133
        };
134
135 4
        return $this;
136
    }
137
}
138