Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.12% |
16 / 17 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
ZipIterator | |
94.12% |
16 / 17 |
|
83.33% |
5 / 6 |
11.02 | |
0.00% |
0 / 1 |
__construct | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
rewind | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
next | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
valid | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Collection; |
6 | |
7 | use Iterator; |
8 | use PeServer\Core\Throws\ArgumentException; |
9 | use PeServer\Core\Throws\CallbackTypeError; |
10 | |
11 | /** |
12 | * zip イテレータ。 |
13 | * |
14 | * @template TKey of array-key |
15 | * @template TValue1 |
16 | * @template TValue2 |
17 | * @template TResult |
18 | * @implements Iterator<TKey,TResult> |
19 | */ |
20 | class ZipIterator implements Iterator |
21 | { |
22 | #region variable |
23 | |
24 | /** |
25 | * @var Iterator[] |
26 | */ |
27 | private array $iterators; |
28 | |
29 | #endregion |
30 | |
31 | /** |
32 | * 生成。 |
33 | * |
34 | * @param Iterator $first |
35 | * @phpstan-param Iterator<TKey,TValue1> $first |
36 | * @param Iterator $second |
37 | * @phpstan-param Iterator<array-key,TValue2> $second |
38 | * @param mixed $callback |
39 | * @phpstan-param callable(array{0:TValue1,1:TValue2},TKey):(TResult) $callback |
40 | */ |
41 | public function __construct(Iterator $first, Iterator $second, private mixed $callback) |
42 | { |
43 | if (!is_callable($callback)) { //@phpstan-ignore-line phpstan-param callable |
44 | throw new CallbackTypeError('$callback'); |
45 | } |
46 | |
47 | $this->iterators = [ |
48 | $first, |
49 | $second, |
50 | ]; |
51 | } |
52 | |
53 | #region Iterator |
54 | |
55 | public function rewind(): void |
56 | { |
57 | foreach ($this->iterators as $iterator) { |
58 | $iterator->rewind(); |
59 | } |
60 | } |
61 | |
62 | public function key(): mixed |
63 | { |
64 | return $this->iterators[0]->key(); |
65 | } |
66 | |
67 | public function current(): mixed |
68 | { |
69 | $items = array_map(fn ($i) => $i->current(), $this->iterators); |
70 | //@phpstan-ignore-next-line TValue1, TValue2 |
71 | return call_user_func($this->callback, $items, $this->key()); |
72 | } |
73 | |
74 | public function next(): void |
75 | { |
76 | foreach ($this->iterators as $iterator) { |
77 | $iterator->next(); |
78 | } |
79 | } |
80 | |
81 | public function valid(): bool |
82 | { |
83 | foreach ($this->iterators as $iterator) { |
84 | if (!$iterator->valid()) { |
85 | return false; |
86 | } |
87 | } |
88 | |
89 | return true; |
90 | } |
91 | |
92 | #endregion |
93 | } |