ArrayH::exploreAtPath()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0291

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 16
c 1
b 1
f 0
nc 5
nop 4
dl 0
loc 24
ccs 17
cts 19
cp 0.8947
crap 5.0291
rs 8.5125
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