at_least()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 4
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Evaluates whether <b>at least n</b> match the specification
8
 *
9
 * @param int $n The number to check
10
 * @param iterable $items The collection
11
 * @param callable $callback Callable 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 bool
28
 */
29
function at_least(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
30
{
31
    $count = 0;
32
    foreach ($items as $key => $value) {
33
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
34
            $count++;
35
            if ($count >= $n) {
36
                return true;
37
            }
38
        }
39
    }
40
41
    return $count >= $n;
42
}
43