Completed
Push — master ( 5a5aff...6d6efd )
by Joram van den
04:02
created

Ajde_Core_Array   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B mergeRecursive() 0 18 7
B get() 0 16 6
1
<?php
2
3
class Ajde_Core_Array
4
{
5
    /**
6
     * TODO
7
     *
8
     * @param array $array1
9
     * @param array $array2
10
     * @return array
11
     */
12
    public static function mergeRecursive(array $array1, array $array2)
13
    {
14
        $merged = $array1;
15
16
        foreach ($array2 as $key => &$value) {
17
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
18
                $merged[$key] = self::mergeRecursive($merged[$key], $value);
19
            } else if (is_numeric($key)) {
20
                if (!in_array($value, $merged)) {
21
                    $merged[] = $value;
22
                }
23
            } else {
24
                $merged[$key] = $value;
25
            }
26
        }
27
28
        return $merged;
29
    }
30
31
    /**
32
     * Get an item from an array using "dot" notation.
33
     *
34
     * @param  array $array
35
     * @param  string $key
36
     * @param  mixed $default
37
     * @return mixed
38
     */
39
    public static function get($array, $key, $default = null)
40
    {
41
        if (is_null($key)) return $array;
42
43
        if (isset($array[$key])) return $array[$key];
44
45
        foreach (explode('.', $key) as $segment) {
46
            if (!is_array($array) || !array_key_exists($segment, $array)) {
47
                return $default;
48
            }
49
50
            $array = $array[$segment];
51
        }
52
53
        return $array;
54
    }
55
}
56