1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Accessories; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Rewindable |
9
|
|
|
*/ |
10
|
|
|
class Rewindable implements \Iterator |
11
|
|
|
{ |
12
|
|
|
/** @var callable */ |
13
|
|
|
private $generatorFunction; |
14
|
|
|
|
15
|
|
|
/** @var \Generator */ |
16
|
|
|
private $generator; |
17
|
|
|
|
18
|
|
|
/** @var null|callable */ |
19
|
|
|
private $onRewind; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $args; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Rewindable constructor. |
26
|
|
|
* @param callable $generatorFunction |
27
|
|
|
* @param mixed ...$args |
28
|
|
|
*/ |
29
|
21 |
|
public function __construct(callable $generatorFunction, ...$args) |
30
|
|
|
{ |
31
|
21 |
|
$this->generatorFunction = $generatorFunction; |
32
|
21 |
|
$this->args = $args; |
33
|
21 |
|
$this->createGenerator(...$args); |
34
|
20 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed ...$args |
38
|
|
|
*/ |
39
|
21 |
|
private function createGenerator(...$args): void |
40
|
|
|
{ |
41
|
21 |
|
$this->generator = \call_user_func($this->generatorFunction, ...$args); |
42
|
|
|
|
43
|
21 |
|
if (!($this->generator instanceof \Generator)) { |
44
|
1 |
|
throw new \InvalidArgumentException('Return type of your generator function MUST be a \Generator.'); |
45
|
|
|
} |
46
|
20 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param callable $onRewind |
50
|
|
|
* @return Rewindable |
51
|
|
|
*/ |
52
|
3 |
|
public function setOnRewind(callable $onRewind): Rewindable |
53
|
|
|
{ |
54
|
3 |
|
if (null !== $this->onRewind) { |
55
|
1 |
|
throw new \InvalidArgumentException('onRewind handler can be set only once.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
$this->onRewind = $onRewind; |
59
|
3 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
15 |
|
public function current() |
66
|
|
|
{ |
67
|
|
|
return |
68
|
15 |
|
$this->generator->current(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
15 |
|
public function next(): void |
75
|
|
|
{ |
76
|
15 |
|
$this->generator->next(); |
77
|
15 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
14 |
|
public function key() |
83
|
|
|
{ |
84
|
14 |
|
return $this->generator->key(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
17 |
|
public function valid(): bool |
91
|
|
|
{ |
92
|
17 |
|
return $this->generator->valid(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
19 |
|
public function rewind(): void |
99
|
|
|
{ |
100
|
19 |
|
$this->createGenerator(...$this->args); |
101
|
19 |
|
if ($this->onRewind) { |
102
|
2 |
|
\call_user_func($this->onRewind); |
103
|
|
|
} |
104
|
19 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $start |
108
|
|
|
* @param int $stop |
109
|
|
|
* @param int|float $step |
110
|
|
|
* @return Rewindable |
111
|
|
|
*/ |
112
|
12 |
|
public static function range(int $start, int $stop, $step = 1): Rewindable |
113
|
|
|
{ |
114
|
|
|
return |
115
|
12 |
|
new Rewindable([G::class, 'range'], $start, $stop, $step); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|