Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
MultipleIterator | |
0.00% |
0 / 20 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rewind | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
valid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
next | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
current | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasChildren | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getChildren | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Notifications\Iterator; |
4 | |
5 | use ArrayIterator; |
6 | use Iterator; |
7 | use RecursiveIterator; |
8 | |
9 | /** |
10 | * Presents a list of iterators as a single stream of results |
11 | * when wrapped with the RecursiveIteratorIterator. |
12 | * |
13 | * This differs from the SPL MultipleIterator in the following ways: |
14 | * * Does not return null for non-valid child iterators |
15 | * * implements RecursiveIterator |
16 | * * Lots less features(e.g. simple!) |
17 | */ |
18 | class MultipleIterator implements RecursiveIterator { |
19 | /** @var Iterator[] */ |
20 | protected $active = []; |
21 | /** @var array */ |
22 | protected $children; |
23 | /** @var int */ |
24 | protected $key = 0; |
25 | |
26 | public function __construct( array $children ) { |
27 | $this->children = $children; |
28 | } |
29 | |
30 | public function rewind(): void { |
31 | $this->active = $this->children; |
32 | $this->key = 0; |
33 | foreach ( $this->active as $key => $it ) { |
34 | $it->rewind(); |
35 | if ( !$it->valid() ) { |
36 | unset( $this->active[$key] ); |
37 | } |
38 | } |
39 | } |
40 | |
41 | public function valid(): bool { |
42 | return (bool)$this->active; |
43 | } |
44 | |
45 | public function next(): void { |
46 | $this->key++; |
47 | foreach ( $this->active as $key => $it ) { |
48 | $it->next(); |
49 | if ( !$it->valid() ) { |
50 | unset( $this->active[$key] ); |
51 | } |
52 | } |
53 | } |
54 | |
55 | #[\ReturnTypeWillChange] |
56 | public function current() { |
57 | $result = []; |
58 | foreach ( $this->active as $it ) { |
59 | $result[] = $it->current(); |
60 | } |
61 | |
62 | return $result; |
63 | } |
64 | |
65 | public function key(): int { |
66 | return $this->key; |
67 | } |
68 | |
69 | public function hasChildren(): bool { |
70 | return (bool)$this->active; |
71 | } |
72 | |
73 | public function getChildren(): ?RecursiveIterator { |
74 | // The NotRecursiveIterator is used rather than a RecursiveArrayIterator |
75 | // so that nested arrays don't get recursed. |
76 | return new NotRecursiveIterator( new ArrayIterator( $this->current() ) ); |
77 | } |
78 | } |