loophp /
collection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace loophp\collection\Operation; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use Generator; |
||
| 9 | use loophp\iterators\MultipleIterableAggregate; |
||
| 10 | use MultipleIterator; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @immutable |
||
| 14 | * |
||
| 15 | * @template TKey |
||
| 16 | * @template T |
||
| 17 | */ |
||
| 18 | final class Transpose extends AbstractOperation |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @return Closure(iterable<TKey, T>): Generator<TKey, list<T>> |
||
| 22 | */ |
||
| 23 | 8 | public function __invoke(): Closure |
|
| 24 | { |
||
| 25 | 8 | $callbackForKeys = |
|
| 26 | /** |
||
| 27 | * @param non-empty-array<int, TKey> $key |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 28 | * |
||
| 29 | * @return TKey |
||
| 30 | */ |
||
| 31 | 8 | static fn (array $key): mixed => reset($key); |
|
| 32 | |||
| 33 | 8 | $callbackForValues = |
|
| 34 | /** |
||
| 35 | * @param array<int, T> $value |
||
| 36 | * |
||
| 37 | * @return array<int, T> |
||
| 38 | */ |
||
| 39 | 8 | static fn (array $value): array => $value; |
|
| 40 | |||
| 41 | /** @var Closure(iterable<TKey, T>): Generator<TKey, list<T>> $pipe */ |
||
| 42 | 8 | $pipe = (new Pipe())()( |
|
| 43 | 8 | static fn (iterable $iterables): MultipleIterableAggregate => new MultipleIterableAggregate($iterables, MultipleIterator::MIT_NEED_ANY), |
|
| 44 | 8 | (new Associate())()($callbackForKeys)($callbackForValues) |
|
| 45 | 8 | ); |
|
| 46 | |||
| 47 | // Point free style. |
||
| 48 | 8 | return $pipe; |
|
| 49 | } |
||
| 50 | } |
||
| 51 |