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

ArrayH   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 8
Bugs 3 Features 1
Metric Value
c 8
b 3
f 1
dl 0
loc 94
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
    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