accept()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Returns a subset of the collection with elements that match the specification.
8
 * The keys are preserved
9
 *
10
 * @param iterable $items The collection
11
 * @param callable $callback The specification is a callable that must return a boolean
12
 * @param int $mode [optional] <p>
13
 * Flag determining what arguments are sent to <i>callback</i>:
14
 * </p><ul>
15
 * <li>
16
 * <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
17
 * </li>
18
 * <li>
19
 * <b>CALLBACK_USE_KEY</b> - pass key as the only argument
20
 * to <i>callback</i> instead of the value</span>
21
 * </li>
22
 * <li>
23
 * <b>CALLBACK_USE_BOTH</b> - pass both value and key as
24
 * arguments to <i>callback</i></span>
25
 * </li>
26
 * </ul>
27
 * @return array
28
 */
29
function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
30
{
31
    $result = [];
32
    foreach ($items as $key => $value) {
33
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
34
            $result[$key] = $value;
35
        }
36
    }
37
38
    return $result;
39
}
40