Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
GeneratorIterator | |
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Collection; |
6 | |
7 | use Generator; |
8 | use Iterator; |
9 | use PeServer\Core\Throws\ArgumentException; |
10 | use PeServer\Core\Throws\CallbackTypeError; |
11 | |
12 | /** |
13 | * ジェネレータ イテレータ。 |
14 | * |
15 | * @template TKey of array-key |
16 | * @template TValue |
17 | * @implements Iterator<TKey,TValue> |
18 | */ |
19 | class GeneratorIterator implements Iterator |
20 | { |
21 | #region variable |
22 | |
23 | private Generator $generator; |
24 | |
25 | #endregion |
26 | |
27 | /** |
28 | * 生成。 |
29 | * |
30 | * @param mixed $factory |
31 | * @phpstan-param callable():Generator<TKey,TValue> $factory |
32 | */ |
33 | public function __construct( |
34 | private mixed $factory |
35 | ) { |
36 | if (!is_callable($factory)) { //@phpstan-ignore-line phpstan-param callable |
37 | throw new CallbackTypeError('$factory'); |
38 | } |
39 | $this->generator = call_user_func($this->factory); |
40 | } |
41 | |
42 | #region Iterator |
43 | |
44 | public function rewind(): void |
45 | { |
46 | $this->generator = call_user_func($this->factory); |
47 | } |
48 | |
49 | public function key(): mixed |
50 | { |
51 | return $this->generator->key(); |
52 | } |
53 | |
54 | public function current(): mixed |
55 | { |
56 | return $this->generator->current(); |
57 | } |
58 | |
59 | public function next(): void |
60 | { |
61 | $this->generator->next(); |
62 | } |
63 | |
64 | public function valid(): bool |
65 | { |
66 | return $this->generator->valid(); |
67 | } |
68 | |
69 | #endregion |
70 | } |