Debian Codegraph @

extensions/Echo/includes/Iterator/MultipleIterator.php

raw symbols SCIP
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 }