Passed
Push — main ( 323548...13fbcb )
by Breno
01:28
created

head()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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