2014-07-30 03:18:48 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:26:48 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Iterator;
|
|
|
|
|
|
|
|
use ArrayIterator;
|
|
|
|
use Iterator;
|
|
|
|
use RecursiveIterator;
|
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
/**
|
|
|
|
* Presents a list of iterators as a single stream of results
|
|
|
|
* when wrapped with the RecursiveIteratorIterator.
|
|
|
|
*
|
|
|
|
* This differs from the SPL MultipleIterator in the following ways:
|
|
|
|
* * Does not return null for non-valid child iterators
|
|
|
|
* * implements RecursiveIterator
|
|
|
|
* * Lots less features(e.g. simple!)
|
|
|
|
*/
|
2022-11-02 20:26:48 +00:00
|
|
|
class MultipleIterator implements RecursiveIterator {
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var Iterator[] */
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $active = [];
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var array */
|
2014-07-30 03:18:48 +00:00
|
|
|
protected $children;
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var int */
|
2014-07-30 03:18:48 +00:00
|
|
|
protected $key = 0;
|
|
|
|
|
|
|
|
public function __construct( array $children ) {
|
|
|
|
$this->children = $children;
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function rewind(): void {
|
2014-07-30 03:18:48 +00:00
|
|
|
$this->active = $this->children;
|
|
|
|
$this->key = 0;
|
|
|
|
foreach ( $this->active as $key => $it ) {
|
|
|
|
$it->rewind();
|
|
|
|
if ( !$it->valid() ) {
|
|
|
|
unset( $this->active[$key] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function valid(): bool {
|
2014-07-30 03:18:48 +00:00
|
|
|
return (bool)$this->active;
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function next(): void {
|
2014-07-30 03:18:48 +00:00
|
|
|
$this->key++;
|
|
|
|
foreach ( $this->active as $key => $it ) {
|
|
|
|
$it->next();
|
|
|
|
if ( !$it->valid() ) {
|
|
|
|
unset( $this->active[$key] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
#[\ReturnTypeWillChange]
|
2014-07-30 03:18:48 +00:00
|
|
|
public function current() {
|
2016-12-05 18:51:07 +00:00
|
|
|
$result = [];
|
2014-07-30 03:18:48 +00:00
|
|
|
foreach ( $this->active as $it ) {
|
|
|
|
$result[] = $it->current();
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function key(): int {
|
2014-07-30 03:18:48 +00:00
|
|
|
return $this->key;
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function hasChildren(): bool {
|
2014-07-30 03:18:48 +00:00
|
|
|
return (bool)$this->active;
|
|
|
|
}
|
|
|
|
|
2022-07-10 02:47:24 +00:00
|
|
|
public function getChildren(): ?RecursiveIterator {
|
2014-07-30 03:18:48 +00:00
|
|
|
// The NotRecursiveIterator is used rather than a RecursiveArrayIterator
|
2022-11-02 20:26:48 +00:00
|
|
|
// so that nested arrays don't get recursed.
|
|
|
|
return new NotRecursiveIterator( new ArrayIterator( $this->current() ) );
|
2014-07-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
}
|