Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Helper   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 19.57%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 1
dl 0
loc 119
ccs 9
cts 46
cp 0.1957
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteArrayValue() 0 18 3
A setArrayValue() 0 6 1
A pathArrayToAssociative() 0 18 4
A arrayEqual() 0 4 2
A searchArray() 0 10 3
A getArrayValue() 0 17 4
A associativeArrayToPath() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee;
13
14
class Helper
15
{
16
    /**
17
     * Get array value by string path.
18
     */
19 22
    public static function getArrayValue(Iterable $array, string $path, string $separator = '.')
20
    {
21 22
        if (isset($array[$path])) {
22 16
            return $array[$path];
23
        }
24 6
        $keys = explode($separator, $path);
25
26 6
        foreach ($keys as $key) {
27 6
            if (!isset($array[$key])) {
28 4
                throw new Exception('array path not found');
29
            }
30
31 3
            $array = $array[$key];
32
        }
33
34 2
        return $array;
35
    }
36
37
    /**
38
     * Remove array value by string path.
39
     */
40
    public static function deleteArrayValue(array $array, string $path, string $separator = '.')
41
    {
42
        $nodes = explode($separator, $path);
43
        $last = null;
44
        $element = &$array;
45
        $node = null;
46
47
        foreach ($nodes as &$node) {
48
            $last = &$element;
49
            $element = &$element[$node];
50
        }
51
52
        if ($last !== null) {
53
            unset($last[$node]);
54
        }
55
56
        return $array;
57
    }
58
59
    /**
60
     * Set array value via string path.
61
     */
62
    public static function setArrayValue(Iterable $array, string $path, $value, string $separator = '.')
63
    {
64
        $result = self::pathArrayToAssociative([$path => $value], $separator);
65
66
        return array_replace_recursive($array, $result);
67
    }
68
69
    /**
70
     * Convert assoc array to single array.
71
     */
72
    public static function associativeArrayToPath(Iterable $arr, Iterable $narr = [], $nkey = ''): array
73
    {
74
        /*if ($nkey !== '') {
75
            $narr[substr($nkey, 0, -1)] = $arr;
76
        }*/
77
78
        foreach ($arr as $key => $value) {
79
            if (is_array($value)) {
80
                $narr = array_merge($narr, self::associativeArrayToPath($value, $narr, $nkey.$key.'.'));
81
            } else {
82
                $narr[$nkey.$key] = $value;
83
            }
84
        }
85
86
        return $narr;
87
    }
88
89
    /**
90
     * Convert array with keys like a.b to associative array.
91
     */
92
    public static function pathArrayToAssociative(Iterable $array, string $separator = '.'): array
93
    {
94
        $out = [];
95
        foreach ($array as $key => $val) {
96
            $r = &$out;
97
            foreach (explode($separator, $key) as $key) {
98
                if (!isset($r[$key])) {
99
                    $r[$key] = [];
100
                }
101
102
                $r = &$r[$key];
103
            }
104
105
            $r = $val;
106
        }
107
108
        return $out;
109
    }
110
111
    /**
112
     * Compare array.
113
     */
114
    public static function arrayEqual(array $a1, array $a2): bool
115
    {
116
        return !array_diff($a1, $a2) && !array_diff($a2, $a1);
117
    }
118
119
    /**
120
     * Search array element.
121
     */
122
    public static function searchArray($value, $key, array $array)
123
    {
124
        foreach ($array as $k => $val) {
125
            if ($val[$key] == $value) {
126
                return $k;
127
            }
128
        }
129
130
        return null;
131
    }
132
}
133