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

chunkSizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 1
eloc 1
c 5
b 2
f 0
nc 1
nop 4
dl 0
loc 3
rs 10
1
<?php
2
3
use Sfneal\Helpers\Arrays\ArrayHelpers;
4
use Sfneal\Helpers\Arrays\ChunkSizer;
5
6
/**
7
 * Returns a chunked array with calculated chunk size.
8
 *
9
 * @param array $array
10
 * @param int $min
11
 * @param int|null $max
12
 * @param bool $no_remainders
13
 * @param bool $preserve_keys
14
 * @return array
15
 */
16
function arrayChunks(array $array, $min = 0, $max = null, $no_remainders = false, $preserve_keys = true): array
17
{
18
    return (new ArrayHelpers($array))->arrayChunks($min, $max, $no_remainders, $preserve_keys);
19
}
20
21
/**
22
 * Flatten a multidimensional array into a 2D array without nested keys.
23
 *
24
 * @param array $array
25
 * @param bool $nest_keys
26
 * @return array
27
 */
28
function arrayFlattenKeys(array $array, $nest_keys = true): array
29
{
30
    return (new ArrayHelpers($array))->arrayFlattenKeys($nest_keys);
31
}
32
33
/**
34
 * Remove particular keys from a multidimensional array.
35
 *
36
 * @param array $array
37
 * @param array|string $keys
38
 * @return array
39
 */
40
function arrayRemoveKeys(array $array, $keys): array
41
{
42
    return (new ArrayHelpers($array))->arrayRemoveKeys($keys);
43
}
44
45
/**
46
 * Sum the values of two arrays.
47
 *
48
 * @param array $array1
49
 * @param array $array2
50
 * @return array
51
 */
52
function sumArrays(array $array1, array $array2): array
53
{
54
    return (new ArrayHelpers($array1))->sumArrays($array2);
55
}
56
57
/**
58
 * Determine if all values in an array of key => value pairs are unique.
59
 *
60
 * @param array $array
61
 * @return bool
62
 */
63
function arrayValuesUnique(array $array): bool
64
{
65
    return (new ArrayHelpers($array))->arrayValuesUnique();
66
}
67
68
/**
69
 * Determine if all array_values are equal to a certain value.
70
 *
71
 * @param array $array
72
 * @param mixed $value
73
 * @return bool
74
 */
75
function arrayValuesEqual(array $array, $value): bool
76
{
77
    return (new ArrayHelpers($array))->arrayValuesEqual($value);
78
}
79
80
/**
81
 * Determine if an array is multidimensional and has keys.
82
 *
83
 * @param array $array
84
 * @return bool
85
 */
86
function arrayHasKeys(array $array): bool
87
{
88
    return (new ArrayHelpers($array))->arrayHasKeys();
89
}
90
91
if (! function_exists('array_except')) {
92
    /**
93
     * Remove specific arrays of keys without modifying the original array.
94
     *
95
     * @param array $original
96
     * @param array $except
97
     * @return array
98
     */
99
    function array_except(array $original, array $except): array
100
    {
101
        return (new ArrayHelpers($original))->array_except($except);
102
    }
103
}
104
105
/**
106
 * Return a best fit chunk size to be passed to array_chunks functions.
107
 *
108
 * Calculates the remainder of array sizes divided by the divisor
109
 * using modulus division.  Continues to calculate remainders until
110
 * the remainder is zero, signifying evenly sized chunks, or the
111
 * divisor is equal to the array size.  If a remainder of zero is not
112
 * found the lowest remainder is returned.
113
 *
114
 * @param int $array_size
115
 * @param int $min minimum chunk size
116
 * @param null $max maximum chunk size
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $max is correct as it would always require null to be passed?
Loading history...
117
 * @param int $divisor
118
 * @return int $remainder lowest calculated remainder
119
 */
120
function chunkSizer(int $array_size, $min = 0, $max = null, $divisor = 2): int
121
{
122
    return ChunkSizer::execute($array_size, $min, $max, $divisor);
123
}
124
125
/**
126
 * Remove a key from an array & return the key's value.
127
 *
128
 * @param array $array
129
 * @param string $key
130
 * @return mixed
131
 */
132
function arrayUnset(array $array, string $key)
133
{
134
    return (new ArrayHelpers($array))->arrayUnset($key);
135
}
136
137
/**
138
 * Determine if all values in an array are null.
139
 *
140
 * @param array $array
141
 * @return bool
142
 */
143
function arrayValuesNull(array $array): bool
144
{
145
    return (new ArrayHelpers($array))->arrayValuesNull();
146
}
147
148
/**
149
 * Only declare function if Illuminate/Collection is installed.
150
 */
151
if (function_exists('collect')) {
152
    /**
153
     * Return a flat array of values found in the $first array that are not found in the $second.
154
     *
155
     * @param array $first
156
     * @param array $second
157
     * @param bool $toArray
158
     * @return Illuminate\Support\Collection|array
159
     */
160
    function array_diff_flat(array $first, array $second, bool $toArray = true)
161
    {
162
        $collection = collect($first)
163
            ->diff($second)
164
            ->flatten();
165
166
        // Return as array
167
        if ($toArray) {
168
            return $collection->toArray();
169
        }
170
171
        // Return as Collection
172
        return $collection;
173
    }
174
}
175
176
/**
177
 * @deprecated
178
 *
179
 * Sum the values of two arrays
180
 *
181
 * @param $array1
182
 * @param $array2
183
 * @return array
184
 */
185
function sum_arrays(array $array1, array $array2): array
186
{
187
    return sumArrays($array1, $array2);
188
}
189
190
/**
191
 * @deprecated
192
 *
193
 * Remove a key from an array & return the key's value.
194
 *
195
 * @param array $array
196
 * @param string $key
197
 * @return mixed
198
 */
199
function array_unset(array $array, string $key)
200
{
201
    return arrayUnset($array, $key);
202
}
203