1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\collection\Operation; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Generator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @immutable |
12
|
|
|
* |
13
|
|
|
* @template TKey |
14
|
|
|
* @template T |
15
|
|
|
*/ |
16
|
|
|
final class Associate extends AbstractOperation |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @template NewTKey |
20
|
|
|
* @template NewT |
21
|
|
|
* |
22
|
|
|
* @return Closure(callable(mixed, mixed, iterable<mixed, mixed>): mixed): Closure(callable(mixed, mixed, iterable<mixed, mixed>): mixed): Closure(iterable<mixed, mixed>): Generator<mixed, mixed> |
23
|
|
|
*/ |
24
|
40 |
|
public function __invoke(): Closure |
25
|
|
|
{ |
26
|
40 |
|
return |
27
|
|
|
/** |
28
|
|
|
* @param callable(TKey, T, iterable<TKey, T>): NewTKey $callbackForKeys |
29
|
|
|
* |
30
|
|
|
* @return Closure((callable(T, TKey, iterable<TKey, T>): NewT)): Closure(iterable<TKey, T>): Generator<NewTKey, NewT> |
31
|
|
|
*/ |
32
|
40 |
|
static fn (callable $callbackForKeys): Closure => |
33
|
|
|
/** |
34
|
|
|
* @param callable(T, TKey, iterable<TKey, T>): NewT $callbackForValues |
35
|
|
|
* |
36
|
|
|
* @return Closure(iterable<TKey, T>): Generator<NewTKey, NewT> |
37
|
|
|
*/ |
38
|
40 |
|
static fn (callable $callbackForValues): Closure => |
39
|
|
|
/** |
40
|
|
|
* @param iterable<TKey, T> $iterable |
41
|
|
|
* |
42
|
|
|
* @return Generator<NewTKey, NewT> |
43
|
|
|
*/ |
44
|
40 |
|
static function (iterable $iterable) use ($callbackForKeys, $callbackForValues): Generator { |
45
|
40 |
|
foreach ($iterable as $key => $value) { |
46
|
36 |
|
yield $callbackForKeys($key, $value, $iterable) => $callbackForValues($value, $key, $iterable); |
47
|
|
|
} |
48
|
40 |
|
}; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|