Completed
Push — master ( 7b8826...d0bba6 )
by Woody
21s
created

filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Dryist;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Count the number of items.
9
 *
10
 * @see https://php.net/iterator_count
11
 */
12
function count(iterable $items): int
13
{
14 6
    $total = 0;
15 6
    foreach ($items as $_) {
16 6
        $total++;
17
    }
18 6
    return $total;
19
}
20
21
/**
22
 * Combine a list of keys and a list of values into a map.
23
 */
24
function combine(iterable $keys, iterable $values): iterable
25
{
26 2
    if (count($keys) !== count($values)) {
27 1
        throw new InvalidArgumentException("Count of keys and values do not match");
28
    }
29
30 1
    foreach ($keys as $key) {
31 1
        yield $key => \current($values);
32 1
        \next($values);
33
    }
34 1
}
35
36
/**
37
 * Limit items by a predicate applied to value.
38
 *
39
 * Example predicate:
40
 *
41
 *     function ($value): bool {
42
 *         return $value > 1;
43
 *     }
44
 */
45
function filter(iterable $items, callable $accept): iterable
46
{
47 1
    foreach ($items as $key => $item) {
48 1
        if ($accept($item)) {
49 1
            yield $key => $item;
50
        }
51
    }
52 1
}
53
54
/**
55
 * Limit items by a predicate applied to key.
56
 *
57
 * Example predicate:
58
 *
59
 *     function ($key): bool {
60
 *         return $key % 2 === 0;
61
 *     }
62
 */
63
function filterKey(iterable $items, callable $accept): iterable
64
{
65 1
    foreach ($items as $key => $item) {
66 1
        if ($accept($key)) {
67 1
            yield $key => $item;
68
        }
69
    }
70 1
}
71
72
/**
73
 * Resolve a list of keys from a map.
74
 */
75
function keys(iterable $items): iterable
76
{
77 4
    foreach ($items as $key => $value) {
78 4
        yield $key;
79
    }
80 4
}
81
82
/**
83
 * Apply a modifier to every item.
84
 *
85
 * Example modifier:
86
 *
87
 *     function ($value) {
88
 *         return $value;
89
 *     }
90
 */
91
function map(iterable $items, callable $modify): iterable
92
{
93 4
    foreach ($items as $key => $value) {
94 4
        yield $key => $modify($value);
95
    }
96 4
}
97
98
/**
99
 * Apply a modifier to every item with the key.
100
 *
101
 * Example modifier:
102
 *
103
 *     function ($key, $value) {
104
 *         return $value;
105
 *     }
106
 */
107
function mapBoth(iterable $items, callable $modify): iterable
108
{
109 3
    foreach ($items as $key => $value) {
110 3
        yield $key => $modify($key, $value);
111
    }
112 3
}
113
114
/**
115
 * Apply a modifier to every item key.
116
 *
117
 * Example modifier:
118
 *
119
 *     function ($key) {
120
 *         return $key;
121
 *     }
122
 */
123
function mapKey(iterable $items, callable $modify): iterable
124
{
125 3
    foreach ($items as $key => $value) {
126 3
        yield $modify($key) => $value;
127
    }
128 3
}
129
130
/**
131
 * Resolve an iterable to an array.
132
 *
133
 * @link https://php.net/iterator_to_array
134
 */
135
function resolve(iterable $items): array
136
{
137 14
    if (is_array($items)) {
138
        return $items;
139
    }
140
141 14
    return \iterator_to_array($items);
142
}
143
144
/**
145
 * Limit items in a map by a list of keys.
146
 */
147
function take(iterable $items, array $keys): iterable
148
{
149
    return filterKey($items, function ($key) use ($keys): bool {
150 1
        return \in_array($key, $keys, true);
151 1
    });
152
}
153
154
/**
155
 * Resolve a map into a list.
156
 */
157
function values(iterable $items): iterable
158
{
159 1
    foreach ($items as $value) {
160 1
        yield $value;
161
    }
162
}
163