Completed
Push — master ( 77a63f...cef321 )
by Restu
11:38
created

ArrayH::arrMergeAll()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 9.4285
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
    public static function get(array $arr, $path, $default = null)
23
    {
24
        if (!is_array($arr) || !isset($arr)) {
25
            throw new \Exception("Argument 1 passed to arr_get must be of the type array");
26
        }
27
        $val = $default;
28
        self::exploreAtPath($arr, $path, function (&$prosArr) use (&$val) {
29
            $val = $prosArr;
30
        });
31
32
        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
    private static function exploreAtPath(array &$arr, $path, callable $callback, array &$prosArr = null)
42
    {
43
        if ($prosArr === null) {
44
            $prosArr = &$arr;
45
            if (is_string($path) && $path == '') {
46
                $callback($prosArr);
47
                return;
48
            }
49
        }
50
51
        $explodedPath = explode(self::$pathSeparator, $path);
52
        $nextPath = array_shift($explodedPath);
53
54
        if (count($explodedPath) > 0) {
55
            self::exploreAtPath(
56
                $arr,
57
                implode(self::$pathSeparator, $explodedPath),
58
                $callback,
59
                $prosArr[$nextPath]
60
            );
61
        } else {
62
            $callback($prosArr[$nextPath]);
63
        }
64
    }
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
    public static function arrMergeAll(array &$arr, $arr2)
73
    {
74
        if (is_array($arr2)) {
75
            foreach ($arr2 as $key => $val) {
76
                $arr[$key] = $val;
77
            }
78
        } else {
79
            $arr[] = $arr2;
80
        }
81
82
        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
    public static function arrExclude(array $arr, array $excludeKeys)
92
    {
93
        $result = [];
94
        foreach ($arr as $key => $val) {
95
            if (!in_array($key, $excludeKeys)) {
96
                $result[$key] = $val;
97
            }
98
        }
99
        return $result;
100
    }
101
}
102