ArrayH   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.02%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
c 8
b 3
f 1
dl 0
loc 94
ccs 40
cts 43
cp 0.9302
rs 10
wmc 15
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 4
B exploreAtPath() 0 24 5
A arrMergeAll() 0 12 3
A arrExclude() 0 10 3
1
<?php
2
namespace JayaCode\Framework\Core\Helper\HelperArray;
3
4
/**
5
 * Class ArrayH
6
 * @package JayaCode\Framework\Core\Helper\HelperArray
7
 */
8
class ArrayH
9
{
10
    /**
11
     * @var string
12
     */
13
    public static $pathSeparator = ".";
14
15
    /**
16
     * @param array $arr
17
     * @param $path
18
     * @param null $default
19
     * @return null
20
     * @throws \Exception
21
     */
22 99
    public static function get(array $arr, $path, $default = null)
23
    {
24 99
        if (!is_array($arr) || !isset($arr)) {
25
            throw new \Exception("Argument 1 passed to arr_get must be of the type array");
26
        }
27 99
        $val = $default;
28 99
        self::exploreAtPath($arr, $path, function (&$prosArr) use (&$val) {
29 99
            $val = $prosArr;
30 99
        });
31
32 99
        return $val ? $val : $default;
33
    }
34
35
    /**
36
     * @param array $arr
37
     * @param $path
38
     * @param callable $callback
39
     * @param array|null $prosArr
40
     */
41 99
    private static function exploreAtPath(array &$arr, $path, callable $callback, array &$prosArr = null)
42
    {
43 99
        if ($prosArr === null) {
44 99
            $prosArr = &$arr;
45 99
            if (is_string($path) && $path == '') {
46
                $callback($prosArr);
47
                return;
48
            }
49 99
        }
50
51 99
        $explodedPath = explode(self::$pathSeparator, $path);
52 99
        $nextPath = array_shift($explodedPath);
53
54 99
        if (count($explodedPath) > 0) {
55 15
            self::exploreAtPath(
56 15
                $arr,
57 15
                implode(self::$pathSeparator, $explodedPath),
58 15
                $callback,
59 15
                $prosArr[$nextPath]
60 15
            );
61 15
        } else {
62 99
            $callback($prosArr[$nextPath]);
63
        }
64 99
    }
65
66
    /**
67
     * merge all array, if it has the same key number, the value will override
68
     * @param array $arr
69
     * @param $arr2
70
     * @return array
71
     */
72 3
    public static function arrMergeAll(array &$arr, $arr2)
73
    {
74 3
        if (is_array($arr2)) {
75 3
            foreach ($arr2 as $key => $val) {
76 3
                $arr[$key] = $val;
77 3
            }
78 3
        } else {
79 3
            $arr[] = $arr2;
80
        }
81
82 3
        return $arr;
83
    }
84
85
    /**
86
     * Return all array elements except for a given key
87
     * @param array $arr
88
     * @param array $excludeKeys
89
     * @return array
90
     */
91 3
    public static function arrExclude(array $arr, array $excludeKeys)
92
    {
93 3
        $result = [];
94 3
        foreach ($arr as $key => $val) {
95 3
            if (!in_array($key, $excludeKeys)) {
96 3
                $result[$key] = $val;
97 3
            }
98 3
        }
99 3
        return $result;
100
    }
101
}
102