Passed
Push — master ( 358ba6...c88190 )
by Stephen
57s queued 12s
created

ArrayHelpers::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Arrays;
4
5
class ArrayHelpers
6
{
7
    // todo: remove 'array' prefix from method names
8
9
    /**
10
     * @var array
11
     */
12
    private $array;
13
14
    /**
15
     * ArrayHelpers constructor.
16
     *
17
     * @param array $array
18
     */
19
    public function __construct(array $array)
20
    {
21
        $this->array = $array;
22
    }
23
24
    /**
25
     * Returns a chunked array with calculated chunk size.
26
     *
27
     * @param int $min
28
     * @param int|null $max
29
     * @param bool $no_remainders
30
     * @param bool $preserve_keys
31
     * @return array
32
     */
33
    public function arrayChunks($min = 0, $max = null, $no_remainders = false, $preserve_keys = true): array
34
    {
35
        $chunks = array_chunk(
36
            $this->array,
37
            (new ChunkSizer(count($this->array), $min, $max))->execute(),
38
            $preserve_keys
39
        );
40
41
        // Check if the first chunk is the same length as the last chunk
42
        if ($no_remainders && count($chunks[0]) != count(array_reverse($chunks)[0])) {
43
            $remainder = array_pop($chunks);
44
            $last_chunk = array_pop($chunks);
45
46
            // Add the remainder chunk to the last equal sized chunk
47
            $chunks[] = array_merge($last_chunk, $remainder);
48
        }
49
50
        return $chunks;
51
    }
52
53
    /**
54
     * Flatten a multidimensional array into a 2D array without nested keys.
55
     *
56
     * @param bool $nest_keys
57
     * @return array
58
     */
59
    public function arrayFlattenKeys($nest_keys = true): array
60
    {
61
        $flat = [];
62
        foreach (array_keys($this->array) as $key) {
63
            if (is_array($this->array[$key])) {
64
                // If the key is an array, add each children keys to flattened array
65
                foreach ($this->array[$key] as $k => $v) {
66
                    if ($nest_keys) {
67
                        $flat[$key.'_'.$k] = $v;
68
                    } else {
69
                        $flat[$k] = $v;
70
                    }
71
                }
72
            } else {
73
                $flat[$key] = $this->array[$key];
74
            }
75
        }
76
77
        return $flat;
78
    }
79
80
    /**
81
     * Remove particular keys from a multidimensional array.
82
     *
83
     * @param array|string $keys
84
     * @return array
85
     */
86
    public function arrayRemoveKeys($keys): array
87
    {
88
        $all_keys = array_keys($this->array);
89
        foreach ((array) $keys as $key) {
90
            if (in_array($key, $all_keys)) {
91
                unset($this->array[$key]);
92
            }
93
        }
94
95
        return $this->array;
96
    }
97
98
    /**
99
     * Sum the values of two arrays.
100
     *
101
     * @param array $array2
102
     * @return array
103
     */
104
    public function sumArrays(array $array2): array
105
    {
106
        $array = [];
107
        foreach ($this->array as $index => $value) {
108
            $array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
109
        }
110
111
        return $array;
112
    }
113
114
    /**
115
     * Determine if all values in an array of key => value pairs are unique.
116
     *
117
     * @return bool
118
     */
119
    public function arrayValuesUnique(): bool
120
    {
121
        // Count the number of unique array values
122
        // Check to see if there is more than unique array_value
123
        return count(array_unique(array_values($this->array))) > 1;
124
    }
125
126
    /**
127
     * Determine if all array_values are equal to a certain value.
128
     *
129
     * @param mixed $value
130
     * @return bool
131
     */
132
    public function arrayValuesEqual($value): bool
133
    {
134
        // Check if all array values are equal to a certain value
135
        return count(array_keys($this->array, $value)) == count($this->array);
136
    }
137
138
    /**
139
     * Determine if an array is multidimensional and has keys.
140
     *
141
     * @return bool
142
     */
143
    public function arrayHasKeys(): bool
144
    {
145
        return count($this->array) == count($this->array, COUNT_RECURSIVE);
146
    }
147
148
    /**
149
     * Remove specific arrays of keys without modifying the original array.
150
     *
151
     * @param array $except
152
     * @return array
153
     */
154
    public function array_except(array $except): array
155
    {
156
        return array_diff_key($this->array, array_flip((array) $except));
157
    }
158
159
    /**
160
     * Remove a key from an array & return the key's value.
161
     *
162
     * @param string $key
163
     * @return mixed
164
     */
165
    public function arrayUnset(string $key)
166
    {
167
        // Get the value
168
        $value = $this->array[$key];
169
170
        // Remove the value from the array
171
        unset($this->array[$key]);
172
173
        // Return the value
174
        return $value;
175
    }
176
177
    /**
178
     * Determine if all values in an array are null.
179
     *
180
     * @return bool
181
     */
182
    public function arrayValuesNull(): bool
183
    {
184
        return $this->arrayValuesEqual(null);
185
    }
186
187
    /**
188
     * Retrieve a random array of elements.
189
     *
190
     * @param int $items
191
     * @return array
192
     */
193
    public function random(int $items): array
194
    {
195
        $keys = array_rand($this->array, $items);
196
197
        return array_filter($this->array, function ($value, $key) use ($keys) {
198
            return in_array($key, $keys);
0 ignored issues
show
Bug introduced by
It seems like $keys can also be of type integer and string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            return in_array($key, /** @scrutinizer ignore-type */ $keys);
Loading history...
199
        }, ARRAY_FILTER_USE_BOTH);
200
    }
201
}
202