-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArr.php
More file actions
245 lines (205 loc) · 8.16 KB
/
Arr.php
File metadata and controls
245 lines (205 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
//
// +----------------------------------------------------------------------+
// | Arr.php |
// +----------------------------------------------------------------------+
// | Helper methods for arrays based on Generator functions. It also allow|
// | s using old methods when an iterator is not Traversable. |
// +----------------------------------------------------------------------+
// | Copyright (c) 2021 |
// +----------------------------------------------------------------------+
// | Authors: Mehernosh Mohta <mehernosh.mohta@gmail.com.au> |
// +----------------------------------------------------------------------+
//
namespace EM\Utils;
class Arr
{
public static function meminfo($peak, $raw = false)
{
$memory = 0;
if ($peak === true) {
$memory = memory_get_peak_usage($raw);
} else {
$memory = memory_get_usage($raw);
}
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
}
if ($memory >= 1024 * 1024) {
return sprintf('%.1f MiB', $memory / 1024 / 1024);
}
if ($memory >= 1024) {
return sprintf('%d KiB', $memory / 1024);
}
return sprintf('%d B', $memory);
}
public static function chunk(iterable $iterable, int $chunk_size = 1000, bool $preserve_keys = false)
{
$chunk_size = (int)$chunk_size;
if (!is_iterable($iterable) || $chunk_size <= 0) {
return false;
}
if ($iterable instanceof \Traversable) {
// for iterator
$generator = function ($iterable, $chunk_size, $preserve_keys) {
$count = 0;
$chunk = [];
foreach ($iterable as $key => $value) {
if ($preserve_keys) {
$chunk[$key] = $value;
} else {
$chunk[] = $value;
}
$count++;
if ($count === $chunk_size) {
yield $chunk;
$count = 0;
$chunk = [];
}
}
if ($count !== 0) {
yield $chunk;
}
};
return $generator($iterable, $chunk_size, $preserve_keys);
}
// preserve the old functionality when iterators are not passed
return array_chunk($iterable, $chunk_size, $preserve_keys);
}
public static function map(callable $function, iterable $iterable)
{
if (!is_iterable($iterable)) {
return false;
}
if ($iterable instanceof \Traversable) {
$generator = function ($function, $iterable) {
foreach ($iterable as $key => $value) {
yield $key => $function($value);
}
};
return $generator($function, $iterable);
}
return array_map($function, $iterable);
}
public static function apply(callable $function, iterable $iterable)
{
if (!is_iterable($iterable)) {
return false;
}
if ($iterable instanceof \Traversable) {
$generator = function ($function, $iterable) {
foreach ($iterable as $key => $value) {
$function($value);
}
};
return $generator($function, $iterable);
}
return array_map($function, $iterable);
}
public static function flatten(iterable $iterable)
{
if (!is_iterable($iterable)) {
return false;
}
if ($iterable instanceof \Traversable) {
foreach ($iterable as $key => $value) {
if (is_array($value)) {
yield from flatten($value);
} else {
yield $key => $value;
}
}
} else {
$return = [];
array_walk_recursive($iterable,
function($a) use (&$return) {
$return[] = $a;
}
);
return $return;
}
}
public static function duplicates(iterable $iterable, string $column_name = '', $case_sensitive = true)
{
// Find duplicate values for a given column name
$duplicates = [];
if (!is_iterable($iterable)
|| empty($column_name)
) {
return $duplicates;
}
if ($iterable instanceof \Traversable) {
foreach ($iterable as $row) {
$data = trim($row[$column_name]);
if ($case_sensitive === true) {
$data = strtolower($data);
}
if ($data === strtolower($column_name)) {
// we need to exclude column name if its in the header rows
continue;
}
if (!isset($duplicates[$data])) {
$duplicates[$data] = 1;
} else {
$duplicates[$data]++;
}
}
$duplicates = array_filter($duplicates, function($key, $value) {
return $key > 1;
}, ARRAY_FILTER_USE_BOTH);
return $duplicates;
}
return array_unique($iterable);
}
public static function range($start, $stop, $step)
{
if ($start > $stop) {
if ($step >= 0) {
throw new \InvalidArgumentException('step must be negative when start > stop');
}
for ($i = $start; $i >= $stop; $i += $step) {
yield $i;
}
} else if ($start < $stop) {
if ($step <= 0) {
throw new \InvalidArgumentException('step must be positive when start < stop');
}
for ($i = $start; $i <= $stop; $i += $step) {
yield $i;
}
} else {
// both start and stop are same
yield $start;
}
}
public static function apply(iterable $iterable, callable $function)
{
if (!is_iterable($iterable)) {
return false;
}
if ($iterable instanceof Traversable) {
$generator = function ($iterable, $function) {
foreach ($iterable as $key => $value) {
$function($value);
}
};
return $generator($iterable, $function);
}
return array_map($function, $iterable);
}
public static function map(iterable $iterable, callable $function)
{
if (!is_iterable($iterable)) {
return false;
}
if ($iterable instanceof Traversable) {
$generator = function ($iterable, $function) {
foreach ($iterable as $key => $value) {
yield $key => $function($value);
}
};
return $generator($iterable, $function);
}
return array_map($function, $iterable);
}
}