| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Iterator; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use Iterator; |
| 7 |
use RecursiveIterator; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class MultipleIterator implements RecursiveIterator { |
| 19 |
|
| 20 |
protected $active = []; |
| 21 |
|
| 22 |
protected $children; |
| 23 |
|
| 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 |
|
| 75 |
|
| 76 |
return new NotRecursiveIterator( new ArrayIterator( $this->current() ) ); |
| 77 |
} |
| 78 |
} |