ArrayHelper::arrayDiffAssocRecursive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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