Completed
Push — master ( 56096b...601ee3 )
by Sergey
12s
created

Arr::flatten()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 2
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Larapulse\Support\Handlers;
4
5
use Larapulse\Support\Helpers\DataTypes;
6
7
class Arr
8
{
9
    /**
10
     * Flatten a multi-dimensional array into a single level
11
     *
12
     * @param  array  $array
13
     * @param  int  $depth
14
     * @return array
15
     */
16
    public static function flatten(array $array, $depth = INF) : array
17
    {
18
        $depth = is_int($depth) ? max($depth, 0) : INF;
19
20
        return array_reduce($array, function ($result, $item) use ($depth) {
21
            if (!is_array($item) || $depth === 0) {
22
                return array_merge($result, [$item]);
23
            } elseif ($depth === 1) {
24
                return array_merge($result, array_values($item));
25
            } else {
26
                return array_merge($result, self::flatten($item, $depth - 1));
27
            }
28
        }, []);
29
    }
30
31
    /**
32
     * Flatten a multi-dimensional array into a single level with saving keys
33
     *
34
     * @param  array $array
35
     * @param  int   $depth
36
     *
37
     * @return array
38
     */
39
    public static function flattenAssoc(array $array, $depth = INF) : array
40
    {
41
        $result = [];
42
        $depth = is_int($depth) ? max($depth, 0) : INF;
43
44
        foreach ($array as $key => $value) {
45
            if (is_array($value) && $depth >= 1) {
46
                $result = self::flattenAssoc($value, $depth - 1) + $result;
47
            } else {
48
                $result[$key] = $value;
49
            }
50
        }
51
52
        return $result;
53
    }
54
55
    /**
56
     * Get depth of array
57
     *
58
     * @param array $array
59
     *
60
     * @return int
61
     */
62
    public static function depth(array $array) : int
63
    {
64
        $maxDepth = 1;
65
66
        foreach ($array as $value) {
67
            if (is_array($value)) {
68
                $depth = self::depth($value) + 1;
69
70
                if ($depth > $maxDepth) {
71
                    $maxDepth = $depth;
72
                }
73
            }
74
        }
75
76
        return $maxDepth;
77
    }
78
79
    /**
80
     * Check if array contains only a specific type
81
     *
82
     * @param array  $array
83
     * @param string $type
84
     *
85
     * @return bool
86
     */
87
    public static function isTypeOf(array $array, string $type) : bool
88
    {
89
        $type = DataTypes::guessFromAlias($type);
90
91
        if (!in_array($type, DataTypes::TYPES)) {
92
            return false;
93
        }
94
95
        foreach ($array as $item) {
96
            if (gettype($item) !== $type) {
97
                return false;
98
            }
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * Check if array contains only a specific type
106
     *
107
     * @param array  $array
108
     * @param array  $types
109
     *
110
     * @return bool
111
     */
112
    public static function isTypesOf(array $array, array $types) : bool
113
    {
114
        if (!self::isTypeOf($types, 'string')) {
115
            return false;
116
        }
117
118
        $types = array_map(function ($type) {
119
            return DataTypes::guessFromAlias($type);
120
        }, $types);
121
122
        if (array_diff($types, DataTypes::TYPES)) {
123
            return false;
124
        }
125
126
        foreach ($array as $item) {
127
            if (!in_array(gettype($item), $types, true)) {
128
                return false;
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * Check if array contains instances of specified class/interface
137
     *
138
     * @param array  $array
139
     * @param string $className
140
     *
141
     * @return bool
142
     */
143
    public static function isInstancesOf(array $array, string $className) : bool
144
    {
145
        if (!class_exists($className) && !interface_exists($className)) {
146
            return false;
147
        }
148
149
        $result = true;
150
151
        foreach ($array as $item) {
152
            $result = is_object($item) && $item instanceof $className && $result;
153
        }
154
155
        return $result;
156
    }
157
}
158