Dig   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 32
c 3
b 0
f 0
dl 0
loc 100
rs 10
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 7 2
A value() 0 3 2
B get() 0 27 8
A accessible() 0 3 2
A set() 0 25 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JustSteveKing\Config\Support;
6
7
use Closure;
8
use ArrayAccess;
9
10
class Dig
11
{
12
    /**
13
     * @param array|ArrayAccess $value
14
     * @return bool
15
     */
16
    public static function accessible($value): bool
17
    {
18
        return is_array($value) || $value instanceof ArrayAccess;
19
    }
20
21
    /**
22
     * @param array|ArrayAccess $array
23
     * @param mixed $key
24
     * @return bool
25
     */
26
    public static function exists($array, mixed $key): bool
27
    {
28
        if ($array instanceof ArrayAccess) {
29
            return $array->offsetExists($key);
30
        }
31
32
        return array_key_exists($key, $array);
33
    }
34
35
    /**
36
     * @param array|ArrayAccess $array
37
     * @param mixed $key
38
     * @param mixed $default
39
     * @return mixed
40
     */
41
    public static function get($array, mixed $key, mixed $default = null): mixed
42
    {
43
        if (! static::accessible($array)) {
44
            return static::value($default);
45
        }
46
47
        if (is_null($key)) {
48
            return $array;
49
        }
50
51
        if (static::exists($array, $key)) {
52
            return $array[$key];
53
        }
54
55
        if (strpos($key, '.') === false) {
56
            return $array[$key] ?? static::value($default);
57
        }
58
59
        foreach (explode('.', $key) as $segment) {
60
            if (static::accessible($array) && static::exists($array, $segment)) {
61
                $array = $array[$segment];
62
            } else {
63
                return static::value($default);
64
            }
65
        }
66
67
        return $array;
68
    }
69
70
    /**
71
     * @param array|ArrayAccess $array
72
     * @param mixed $key
73
     * @param mixed $value
74
     * @return mixed
75
     */
76
    public static function set(&$array, mixed $key, mixed $value): mixed
77
    {
78
        if (is_null($key)) {
79
            return $array = $value;
80
        }
81
82
        $keys = explode('.', $key);
83
84
        foreach ($keys as $index => $item) {
85
            if (count($keys) === 1) {
86
                break;
87
            }
88
89
            unset($keys[$index]);
90
91
            if (! isset($array[$item]) || ! is_array($array[$item])) {
92
                $array[$item] = [];
93
            }
94
95
            $array = &$array[$item];
96
        }
97
98
        $array[array_shift($keys)] = $value;
99
100
        return $array;
101
    }
102
103
    /**
104
     * @param mixed $value
105
     * @return mixed
106
     */
107
    public static function value(mixed $value): mixed
108
    {
109
        return $value instanceof Closure ? $value() : $value;
110
    }
111
}
112