Passed
Push — master ( dad483...8852ad )
by Stephen
02:13
created

ArrayHelpers::arrayValuesEqual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 4
rs 10
1
<?php
2
3
4
namespace Sfneal\Helpers\Arrays;
5
6
7
use ErrorException;
8
9
class ArrayHelpers
10
{
11
    /**
12
     * @var array
13
     */
14
    private $array;
15
16
    /**
17
     * ArrayHelpers constructor.
18
     *
19
     * @param array $array
20
     */
21
    public function __construct(array $array)
22
    {
23
        $this->array = $array;
24
    }
25
26
    /**
27
     * Returns a chunked array with calculated chunk size.
28
     *
29
     * @param int $min
30
     * @param int|null $max
31
     * @param bool $no_remainders
32
     * @param bool $preserve_keys
33
     * @return array
34
     */
35
    public function arrayChunks($min = 0, $max = null, $no_remainders = false, $preserve_keys = true): array
36
    {
37
        $chunks = array_chunk($this->array, chunkSizer(count($this->array), $min, $max), $preserve_keys);
0 ignored issues
show
Bug introduced by
It seems like $max can also be of type integer; however, parameter $max of chunkSizer() does only seem to accept null, 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

37
        $chunks = array_chunk($this->array, chunkSizer(count($this->array), $min, /** @scrutinizer ignore-type */ $max), $preserve_keys);
Loading history...
38
39
        // Check if the first chunk is the same length as the last chunk
40
        if ($no_remainders && count($chunks[0]) != count(array_reverse($chunks)[0])) {
41
            $remainder = array_pop($chunks);
42
            $last_chunk = array_pop($chunks);
43
44
            // Add the remainder chunk to the last equal sized chunk
45
            $chunks[] = array_merge($last_chunk, $remainder);
46
        }
47
48
        return $chunks;
49
    }
50
51
    /**
52
     * Flatten a multidimensional array into a 2D array without nested keys.
53
     *
54
     * @param bool $nest_keys
55
     * @return array
56
     */
57
    public function arrayFlattenKeys($nest_keys = true): array
58
    {
59
        $flat = [];
60
        foreach (array_keys($this->array) as $key) {
61
            if (is_array($this->array[$key])) {
62
                // If the key is an array, add each children keys to flattened array
63
                foreach ($this->array[$key] as $k => $v) {
64
                    if ($nest_keys) {
65
                        $flat[$key.'_'.$k] = $v;
66
                    } else {
67
                        $flat[$k] = $v;
68
                    }
69
                }
70
            } else {
71
                $flat[$key] = $this->array[$key];
72
            }
73
        }
74
75
        return $flat;
76
    }
77
78
    /**
79
     * Remove particular keys from a multidimensional array.
80
     *
81
     * @param array|string $keys
82
     * @return array
83
     */
84
    public function arrayRemoveKeys($keys): array
85
    {
86
        $all_keys = array_keys($this->array);
87
        foreach ((array) $keys as $key) {
88
            if (in_array($key, $all_keys)) {
89
                unset($this->array[$key]);
90
            }
91
        }
92
93
        return $this->array;
94
    }
95
96
    /**
97
     * Sum the values of two arrays.
98
     *
99
     * @param array $array2
100
     * @return array
101
     */
102
    public function sumArrays(array $array2): array
103
    {
104
        $array = [];
105
        foreach ($this->array as $index => $value) {
106
            $array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
107
        }
108
109
        return $array;
110
    }
111
112
    /**
113
     * Determine if all values in an array of key => value pairs are unique.
114
     *
115
     * @return bool
116
     */
117
    public function arrayValuesUnique(): bool
118
    {
119
        // Count the number of unique array values
120
        // Check to see if there is more than unique array_value
121
        return count(array_unique(array_values($this->array))) > 1;
122
    }
123
124
    /**
125
     * Determine if all array_values are equal to a certain value.
126
     *
127
     * @param mixed $value
128
     * @return bool
129
     */
130
    public function arrayValuesEqual($value): bool
131
    {
132
        // Check if all array values are equal to a certain value
133
        return count(array_keys($this->array, $value)) == count($this->array);
134
    }
135
136
    /**
137
     * Determine if an array is multidimensional and has keys.
138
     *
139
     * @return bool
140
     */
141
    public function arrayHasKeys(): bool
142
    {
143
        return count($this->array) == count($this->array, COUNT_RECURSIVE);
144
    }
145
146
    /**
147
     * Remove specific arrays of keys without modifying the original array.
148
     *
149
     * @param array $except
150
     * @return array
151
     */
152
    public function array_except(array $except): array
153
    {
154
        return array_diff_key($this->array, array_flip((array) $except));
155
    }
156
157
    /**
158
     * Remove a key from an array & return the key's value.
159
     *
160
     * @param string $key
161
     * @return mixed
162
     */
163
    public function arrayUnset(string $key)
164
    {
165
        // Get the value
166
        try {
167
            $value = $this->array[$key];
168
        } catch (ErrorException $exception) {
169
            $value = null;
170
        }
171
172
        // Remove the value from the array
173
        unset($this->array[$key]);
174
175
        // Return the value
176
        return $value;
177
    }
178
179
    /**
180
     * Determine if all values in an array are null.
181
     *
182
     * @return bool
183
     */
184
    public function arrayValuesNull(): bool
185
    {
186
        return $this->arrayValuesEqual(null);
187
    }
188
}
189