Completed
Push — master ( 87a229...1012d8 )
by Restu
15:25
created

ArrayH::exploreAtPath()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

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
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 $arr
17
     * @param $path
18
     * @param null $default
19
     * @return null
20
     */
21
    public static function get($arr, $path, $default = null)
22
    {
23
        $val = $default;
24
        self::exploreAtPath($arr, $path, function (&$prosArr) use (&$val) {
25
            $val = $prosArr;
26
        });
27
28
        return $val ? $val : $default;
29
    }
30
31
    /**
32
     * @param array $arr
33
     * @param $path
34
     * @param callable $callback
35
     * @param array|null $prosArr
36
     */
37
    private static function exploreAtPath(array &$arr, $path, callable $callback, array &$prosArr = null)
38
    {
39
        if ($prosArr === null) {
40
            $prosArr = &$arr;
41
            if (is_string($path) && $path == '') {
42
                $callback($prosArr);
43
                return;
44
            }
45
        }
46
47
        $explodedPath = explode(self::$pathSeparator, $path);
48
        $nextPath = array_shift($explodedPath);
49
50
        if (count($explodedPath) > 0) {
51
            self::exploreAtPath(
52
                $arr,
53
                implode(self::$pathSeparator, $explodedPath),
54
                $callback,
55
                $prosArr[$nextPath]
56
            );
57
        } else {
58
            $callback($prosArr[$nextPath]);
59
        }
60
    }
61
62
    /**
63
     * @param $arr
64
     * @return array
65
     */
66
    public static function arrPush(&$arr)
67
    {
68
        foreach (func_get_args() as $arg) {
69
            if (is_array($arg)) {
70
                foreach ($arg as $key => $val) {
71
                    if (!is_numeric($key)) {
72
                        $arr[$key] = $val;
73
                    } else {
74
                        $arr[] = $val;
75
                    }
76
                }
77
            } else {
78
                $arr[] = $arg;
79
            }
80
        }
81
82
        return $arr;
83
    }
84
}
85