Arr   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A only() 0 4 1
A except() 0 4 1
B forget() 0 33 7
A exists() 0 8 2
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use ArrayAccess;
6
7
class Arr
8
{
9
    public static function only($array, $keys)
10
    {
11
        return array_intersect_key($array, array_flip((array) $keys));
12
    }
13
14
    public static function except($array, $keys)
15
    {
16
        return static::forget($array, $keys);
17
    }
18
19
    public static function forget($array, $keys)
20
    {
21
        $keys = (array) $keys;
22
23
        if (count($keys) === 0) {
24
            return $array;
25
        }
26
27
        foreach ($keys as $key) {
28
            // If the exact key exists in the top-level, remove it
29
            if (static::exists($array, $key)) {
30
                unset($array[$key]);
31
32
                continue;
33
            }
34
35
            $parts = explode('.', $key);
36
37
            while (count($parts) > 1) {
38
                $part = array_shift($parts);
39
40
                if (isset($array[$part]) && is_array($array[$part])) {
41
                    $array = &$array[$part];
42
                } else {
43
                    continue 2;
44
                }
45
            }
46
47
            unset($array[array_shift($parts)]);
48
        }
49
50
        return $array;
51
    }
52
53
    public static function exists($array, $key)
54
    {
55
        if ($array instanceof ArrayAccess) {
56
            return $array->offsetExists($key);
57
        }
58
59
        return array_key_exists($key, $array);
60
    }
61
}
62