|
1
|
|
|
<?php namespace Mbh\Iterator; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MBHFramework |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
|
7
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
|
8
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use ArrayAccess; |
|
12
|
|
|
use AppendIterator; |
|
13
|
|
|
use JsonSerializable; |
|
14
|
|
|
use Countable; |
|
15
|
|
|
use Iterator; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Iterator to allow multiple iterators to be concatenated |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
class ConcatIterator extends AppendIterator implements Countable, Iterator |
|
24
|
|
|
{ |
|
25
|
|
|
const INVALID_INDEX = 'Index invalid or out of range'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int $count Fast-lookup count for full set of iterators */ |
|
28
|
|
|
public $count = 0; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Build an iterator over multiple iterators |
|
32
|
|
|
* Unlike a LimitIterator, the $end defines the last index, not the count |
|
33
|
|
|
* |
|
34
|
|
|
* @param Iterator $iterator,... Concat iterators in order |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(...$args) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
foreach ($args as $i => $iterator) { |
|
40
|
|
|
if ( |
|
41
|
|
|
$iterator instanceof ArrayAccess && |
|
42
|
|
|
$iterator instanceof Countable |
|
43
|
|
|
) { |
|
44
|
|
|
// Unroll other ConcatIterators, so we avoid deep iterator stacks |
|
45
|
|
|
if ($iterator instanceof self) { |
|
46
|
|
|
foreach ($iterator->getArrayIterator() as $innerIt) { |
|
|
|
|
|
|
47
|
|
|
$this->append($innerIt); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$this->append($iterator); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->count += count($iterator); |
|
54
|
|
|
} else { |
|
55
|
|
|
throw new InvalidArgumentException( |
|
56
|
|
|
'Argument ' . $i . |
|
57
|
|
|
' passed to ' . __METHOD__ . |
|
58
|
|
|
' must be of type ArrayAccess, Countable, and Traversable. ' . |
|
59
|
|
|
gettype($iterator) . ' given.' |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Countable |
|
67
|
|
|
*/ |
|
68
|
|
|
public function count(): int |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->count; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function toArray(): array |
|
74
|
|
|
{ |
|
75
|
|
|
return iterator_to_array($this, false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Find which of the inner iterators an index corresponds to |
|
80
|
|
|
* |
|
81
|
|
|
* @param int $index |
|
82
|
|
|
* @return [ArrayAccess, int] The iterator and interior index |
|
83
|
|
|
*/ |
|
|
|
|
|
|
84
|
|
|
protected function getIteratorByIndex($index = 0) |
|
85
|
|
|
{ |
|
86
|
|
|
$runningCount = 0; |
|
87
|
|
|
foreach ($this->getArrayIterator() as $innerIt) { |
|
88
|
|
|
$count = count($innerIt); |
|
89
|
|
|
if ($index < $runningCount + $count) { |
|
90
|
|
|
return [$innerIt, $index - $runningCount]; |
|
91
|
|
|
} |
|
92
|
|
|
$runningCount += $count; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|