-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathResumableTokensProxy.php
49 lines (40 loc) · 1.2 KB
/
ResumableTokensProxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace JsonMachine;
use IteratorAggregate;
/**
* Allows to resume iteration of the inner IteratorAggregate via foreach, which would be otherwise impossible as
* foreach implicitly calls reset(). This Iterator does not pass the reset() call to the inner Iterator thus enabling
* to follow up on a previous iteation.
*/
class ResumableTokensProxy implements IteratorAggregate, PositionAware
{
/** @var \Iterator */
private $generator;
/** @var \Traversable|PositionAware */
private $tokens;
public function __construct(\Traversable $tokens, \Iterator $tokensGenerator)
{
$this->generator = $tokensGenerator;
$this->tokens = $tokens;
}
public function getIterator(): \Traversable
{
$generator = $this->generator;
while ($generator->valid()) {
yield $generator->key() => $generator->current();
$generator->next();
}
}
public function __call($name, $arguments)
{
return $this->generator->$name(...$arguments);
}
/**
* Returns JSON bytes read so far.
*/
public function getPosition()
{
return $this->tokens->getPosition();
}
}