|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt; |
|
5
|
|
|
|
|
6
|
|
|
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE) |
|
7
|
|
|
{ |
|
8
|
|
|
foreach ($items as $key => $value) { |
|
9
|
|
|
if (true === call_user_func_array($callback, __args($mode, $key, $value))) { |
|
10
|
|
|
return $value; |
|
11
|
|
|
} |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
return $default; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
function head(iterable $items, $default = null) |
|
18
|
|
|
{ |
|
19
|
|
|
foreach ($items as $value) { |
|
20
|
|
|
return $value; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $default; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array |
|
27
|
|
|
{ |
|
28
|
|
|
$result = []; |
|
29
|
|
|
foreach ($items as $key => $value) { |
|
30
|
|
|
$result[$key] = call_user_func_array($callback, __args($mode, $key, $value)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $result; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array |
|
37
|
|
|
{ |
|
38
|
|
|
$result = []; |
|
39
|
|
|
foreach ($items as $key => $value) { |
|
40
|
|
|
if (true === call_user_func_array($callback, __args($mode, $key, $value))) { |
|
41
|
|
|
$result[$key] = $value; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $result; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
function reject(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array |
|
49
|
|
|
{ |
|
50
|
|
|
$result = []; |
|
51
|
|
|
foreach ($items as $key => $value) { |
|
52
|
|
|
if (true !== call_user_func_array($callback, __args($mode, $key, $value))) { |
|
53
|
|
|
$result[$key] = $value; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $result; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function has(array $items, ...$keys): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return ! array_diff($keys, array_keys($items)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function only(array $items, ...$keys): array |
|
66
|
|
|
{ |
|
67
|
|
|
return array_intersect_key($items, array_flip($keys)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function except(array $items, ...$keys): array |
|
71
|
|
|
{ |
|
72
|
|
|
return array_diff_key($items, array_flip($keys)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
function column(iterable $items, $column): array |
|
76
|
|
|
{ |
|
77
|
|
|
$result = []; |
|
78
|
|
|
foreach ($items as $element) { |
|
79
|
|
|
if (is_array($element) && array_key_exists($column, $element)) { |
|
80
|
|
|
$result[] = $element[$column]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function paginate(array $items, int $page, int $per_page, bool $preserve_keys = true): array |
|
88
|
|
|
{ |
|
89
|
|
|
$offset = max(0, ($page - 1) * $per_page); |
|
90
|
|
|
|
|
91
|
|
|
return array_slice($items, $offset, $per_page, $preserve_keys); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
function sum(iterable $items, callable $callback, int $mode = 0) |
|
95
|
|
|
{ |
|
96
|
|
|
$sum = 0; |
|
97
|
|
|
foreach ($items as $key => $value) { |
|
98
|
|
|
$sum += call_user_func_array($callback, __args($mode, $key, $value)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $sum; |
|
102
|
|
|
} |
|
103
|
|
|
|