Passed
Push — main ( 4b3015...a5f639 )
by Breno
01:50
created

reindex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
function some(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
7
{
8
    return at_least(1, $items, $callback, $mode);
9
}
10
11
function none(iterable $items, callable $callback, int $mode = 0): bool
12
{
13
    return ! some($items, $callback, $mode);
14
}
15
16
function at_least(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
17
{
18
    $count = 0;
19
    foreach ($items as $key => $value) {
20
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
21
            $count++;
22
            if ($count >= $n) {
23
                return true;
24
            }
25
        }
26
    }
27
28
    return $count >= $n;
29
}
30
31
function at_most(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
32
{
33
    $count = 0;
34
    foreach ($items as $key => $value) {
35
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
36
            $count++;
37
            if ($count > $n) {
38
                return false;
39
            }
40
        }
41
    }
42
43
    return $count <= $n;
44
}
45
46
function exactly(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
47
{
48
    $count = 0;
49
    foreach ($items as $key => $value) {
50
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
51
            $count++;
52
            if ($count > $n) {
53
                return false;
54
            }
55
        }
56
    }
57
58
    return $count === $n;
59
}
60
61
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
62
{
63
    foreach ($items as $key => $value) {
64
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
65
            return $value;
66
        }
67
    }
68
69
    return $default;
70
}
71
72
function head(iterable $items, $default = null)
73
{
74
    foreach ($items as $value) {
75
        return $value;
76
    }
77
78
    return $default;
79
}
80
81
function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
82
{
83
    $result = [];
84
    foreach ($items as $key => $value) {
85
        $result[$key] = call_user_func_array($callback, __args($mode, $key, $value));
86
    }
87
88
    return $result;
89
}
90
91
function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
92
{
93
    $result = [];
94
    foreach ($items as $key => $value) {
95
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
96
            $result[$key] = $value;
97
        }
98
    }
99
100
    return $result;
101
}
102
103
function reject(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
104
{
105
    $result = [];
106
    foreach ($items as $key => $value) {
107
        if (true !== call_user_func_array($callback, __args($mode, $key, $value))) {
108
            $result[$key] = $value;
109
        }
110
    }
111
112
    return $result;
113
}
114
115
function has(array $items, ...$keys): bool
116
{
117
    return ! array_diff($keys, array_keys($items));
118
}
119
120
function only(array $items, ...$keys): array
121
{
122
    return array_intersect_key($items, array_flip($keys));
123
}
124
125
function except(array $items, ...$keys): array
126
{
127
    return array_diff_key($items, array_flip($keys));
128
}
129
130
function column(iterable $items, $column): array
131
{
132
    $result = [];
133
    foreach ($items as $element) {
134
        if (is_array($element) && array_key_exists($column, $element)) {
135
            $result[] = $element[$column];
136
        }
137
    }
138
139
    return $result;
140
}
141
142
function paginate(array $items, int $page, int $per_page, bool $preserve_keys = true): array
143
{
144
    $offset = max(0, ($page - 1) * $per_page);
145
146
    return array_slice($items, $offset, $per_page, $preserve_keys);
147
}
148
149
function sum_values(iterable $items, callable $callback, int $mode = 0)
150
{
151
    $sum = 0;
152
    foreach ($items as $key => $value) {
153
        $sum += call_user_func_array($callback, __args($mode, $key, $value));
154
    }
155
156
    return $sum;
157
}
158
159
function count_values(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): int
160
{
161
    $count = 0;
162
    foreach ($items as $key => $value) {
163
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
164
            $count++;
165
        }
166
    }
167
168
    return $count;
169
}
170
171
function max_value(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE)
172
{
173
    $max = null;
174
    foreach ($items as $item) {
175
        $max = $item;
176
177
        break;
178
    }
179
180
    foreach ($items as $key => $value) {
181
        $value = call_user_func_array($callback, __args($mode, $key, $value));
182
        if ($value > $max) {
183
            $max = $value;
184
        }
185
    }
186
187
    return $max;
188
}
189
190
function min_value(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE)
191
{
192
    $min = null;
193
    foreach ($items as $item) {
194
        $min = $item;
195
196
        break;
197
    }
198
199
    foreach ($items as $key => $value) {
200
        $value = call_user_func_array($callback, __args($mode, $key, $value));
201
        if ($value < $min) {
202
            $min = $value;
203
        }
204
    }
205
206
    return $min;
207
}
208
209
function group_by(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
210
{
211
    $group = [];
212
    foreach ($items as $key => $value) {
213
        $group[call_user_func_array($callback, __args($mode, $key, $value))][] = $value;
214
    }
215
216
    return $group;
217
}
218