|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\TraversableIterator; |
|
6
|
|
|
|
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
use IteratorIterator; |
|
10
|
|
|
use Traversable; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @license GNU GPL v2+ |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class TraversableIterator implements Iterator { |
|
17
|
|
|
|
|
18
|
|
|
private $traversable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Iterator |
|
22
|
|
|
*/ |
|
23
|
|
|
private $innerIterator; |
|
24
|
|
|
|
|
25
|
9 |
|
public function __construct( Traversable $traversable ) { |
|
26
|
9 |
|
$this->traversable = $traversable; |
|
27
|
9 |
|
$this->innerIterator = $this->buildInnerIterator(); |
|
28
|
9 |
|
} |
|
29
|
|
|
|
|
30
|
9 |
|
private function buildInnerIterator(): Iterator { |
|
31
|
9 |
|
if ( $this->traversable instanceof Iterator ) { |
|
32
|
7 |
|
return $this->traversable; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
if ( $this->traversable instanceof IteratorAggregate ) { |
|
36
|
3 |
|
return new TraversableIterator( $this->traversable->getIterator() ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return new IteratorIterator( $this->traversable ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return the current element |
|
44
|
|
|
* @see Iterator::current |
|
45
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function current() { |
|
49
|
6 |
|
return $this->innerIterator->current(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Move forward to next element |
|
54
|
|
|
* @see Iterator::next |
|
55
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
56
|
|
|
*/ |
|
57
|
7 |
|
public function next() { |
|
58
|
7 |
|
$this->innerIterator->next(); |
|
59
|
7 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the key of the current element |
|
63
|
|
|
* @see Iterator::key |
|
64
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
65
|
|
|
* @return mixed scalar on success, or null on failure. |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function key() { |
|
68
|
5 |
|
return $this->innerIterator->key(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Checks if current position is valid |
|
73
|
|
|
* @see Iterator::rewind |
|
74
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public function valid() { |
|
78
|
9 |
|
return $this->innerIterator->valid(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Rewind the Iterator to the first element |
|
83
|
|
|
* @see Iterator::rewind |
|
84
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
85
|
|
|
*/ |
|
86
|
9 |
|
public function rewind() { |
|
87
|
9 |
|
$this->innerIterator = $this->buildInnerIterator(); |
|
88
|
9 |
|
$this->innerIterator->rewind(); |
|
89
|
9 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |