|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Model\Collection; |
|
16
|
|
|
|
|
17
|
|
|
use ArrayIterator; |
|
18
|
|
|
use Iterator; |
|
19
|
|
|
use IteratorAggregate; |
|
20
|
|
|
use DomainException; |
|
21
|
|
|
use OuterIterator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Acts as a generic iterator for anything iterable |
|
25
|
|
|
* |
|
26
|
|
|
* This is useful as the "iterable" type hint accepts both Traversable |
|
27
|
|
|
* objects and native arrays, but there is no uniform way to iterate |
|
28
|
|
|
* over these in the language. This class accepts iterable values and |
|
29
|
|
|
* finds the relavent Iterator for any Iterator, IteratorAggregate or |
|
30
|
|
|
* array. |
|
31
|
|
|
*/ |
|
32
|
|
|
class IterableIterator implements OuterIterator |
|
33
|
|
|
{ |
|
34
|
|
|
use OuterIteratorTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The inner iterator that was picked |
|
38
|
|
|
* |
|
39
|
|
|
* @var Iterator |
|
40
|
|
|
*/ |
|
41
|
|
|
private $iterator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Accepts any iterable value and creates / obtains an inner |
|
45
|
|
|
* Iterator for them |
|
46
|
|
|
* |
|
47
|
|
|
* @param iterable $iterator |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function __construct(iterable $iterator) |
|
50
|
|
|
{ |
|
51
|
6 |
|
if (is_array($iterator)) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->iterator = new ArrayIterator($iterator); |
|
54
|
|
|
} |
|
55
|
5 |
|
elseif ($iterator instanceof Iterator) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->iterator = $iterator; |
|
58
|
|
|
} |
|
59
|
4 |
|
elseif ($iterator instanceof IteratorAggregate) |
|
60
|
|
|
{ |
|
61
|
4 |
|
while (true) |
|
62
|
|
|
{ |
|
63
|
4 |
|
$newIterator = $iterator->getIterator(); |
|
64
|
|
|
|
|
65
|
4 |
|
if ($newIterator instanceof Iterator) |
|
66
|
|
|
{ |
|
67
|
3 |
|
$this->iterator = $newIterator; |
|
68
|
3 |
|
break; |
|
69
|
|
|
} |
|
70
|
2 |
|
elseif ($newIterator instanceof IteratorAggregate) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$iterator = $newIterator; |
|
73
|
|
|
} |
|
74
|
|
|
else |
|
75
|
|
|
{ |
|
76
|
1 |
|
throw new DomainException |
|
77
|
|
|
( |
|
78
|
|
|
'The given IteratorAggregate\'s ' . |
|
79
|
|
|
'getIterator() method should return an ' . |
|
80
|
1 |
|
'Iterator or IteratorAggregate, but it did not' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
else |
|
86
|
|
|
{ |
|
87
|
|
|
throw new DomainException('Unknown type of iterator'); |
|
88
|
|
|
} |
|
89
|
5 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|