Completed
Pull Request — master (#74)
by Julien
07:02
created

ArrayHelper::arrayHas()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 2
dl 0
loc 19
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Helper;
4
5
/**
6
 * Some array helpers.
7
 * Greatly inspired by Laravel's helper: https://github.com/rappasoft/laravel-helpers
8
 *
9
 * @author Julien Deniau <[email protected]>
10
 */
11
class ArrayHelper
12
{
13
    /**
14
     * Get an item from an array using "dot" notation.
15
     *
16
     * @param  array   $array
17
     * @param  ?string  $key
18
     * @param  mixed   $default
19
     *
20
     * @return mixed
21
     */
22
    public static function arrayGet($array, $key, $default = null)
23
    {
24
        if (null === $key) {
25
            return $array;
26
        }
27
28
        if (isset($array[$key])) {
29
            return $array[$key];
30
        }
31
32
        foreach (explode('.', $key) as $segment) {
33
            if (!is_array($array) || !array_key_exists($segment, $array)) {
34
                return self::value($default);
35
            }
36
            $array = $array[$segment];
37
        }
38
39
        return $array;
40
    }
41
42
    /**
43
     * Check if an item exists in an array using "dot" notation.
44
     *
45
     * @param  array   $array
46
     * @param  ?string  $key
47
     *
48
     * @return bool
49
     */
50
    public static function arrayHas($array, $key)
51
    {
52
        if (empty($array) || null === $key) {
53
            return false;
54
        }
55
56
        if (array_key_exists($key, $array)) {
57
            return true;
58
        }
59
60
        foreach (explode('.', $key) as $segment) {
61
            if (!is_array($array) || !array_key_exists($segment, $array)) {
62
                return false;
63
            }
64
65
            $array = $array[$segment];
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * Flatten a multi-dimensional associative array with dots.
73
     *
74
     * @param  array   $array
75
     * @param  string  $prepend
76
     *
77
     * @return array
78
     */
79
    public static function arrayDot($array, $prepend = '')
80
    {
81
        $results = [];
82
        foreach ($array as $key => $value) {
83
            if (is_array($value) && !empty($value)) {
84
                $results = array_merge(
85
                    $results,
86
                    static::arrayDot($value, $prepend . $key . '.')
87
                );
88
            } else {
89
                $results[$prepend . $key] = $value;
90
            }
91
        }
92
93
        return $results;
94
    }
95
96
    public static function arrayDiffAssocRecursive($array1, $array2)
97
    {
98
        return array_diff_assoc(
99
            static::arrayDot($array1),
100
            static::arrayDot($array2)
101
        );
102
    }
103
104
    public static function arraySame($array1, $array2)
105
    {
106
        return empty(static::arrayDiffAssocRecursive($array1, $array2));
107
    }
108
109
    /**
110
     * Return the default value of the given value.
111
     *
112
     * @param  mixed  $value
113
     *
114
     * @return mixed
115
     */
116
    public static function value($value)
117
    {
118
        return $value instanceof \Closure ? $value() : $value;
119
    }
120
}
121