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