ArrayValidator   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 77.55%

Importance

Changes 0
Metric Value
eloc 51
c 0
b 0
f 0
dl 0
loc 114
ccs 38
cts 49
cp 0.7755
rs 10
wmc 29

6 Methods

Rating   Name   Duplication   Size   Complexity  
A keys() 0 12 1
D schema() 0 49 20
A hasKey() 0 7 1
A hasKeys() 0 12 3
A notEmpty() 0 12 3
A key() 0 7 1
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