silverstripe-fulltextsearch/src/Utils/MultipleArrayIterator.php

67 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2017-04-21 02:27:01 +02:00
namespace SilverStripe\FullTextSearch\Utils;
2017-04-21 02:27:01 +02:00
use Iterator;
2015-11-21 07:19:20 +01:00
class MultipleArrayIterator implements Iterator
{
protected $arrays;
protected $active;
public function __construct()
{
$args = func_get_args();
$this->arrays = array();
foreach ($args as $arg) {
2022-04-13 01:24:03 +02:00
if (is_array($arg) && count($arg ?? [])) {
2015-11-21 07:19:20 +01:00
$this->arrays[] = $arg;
}
}
$this->rewind();
}
2022-04-13 01:24:03 +02:00
#[\ReturnTypeWillChange]
2015-11-21 07:19:20 +01:00
public function rewind()
{
$this->active = $this->arrays;
if ($this->active) {
reset($this->active[0]);
}
}
2022-04-13 01:24:03 +02:00
#[\ReturnTypeWillChange]
2015-11-21 07:19:20 +01:00
public function current()
{
return $this->active ? current($this->active[0]) : false;
}
2022-04-13 01:24:03 +02:00
#[\ReturnTypeWillChange]
2015-11-21 07:19:20 +01:00
public function key()
{
return $this->active ? key($this->active[0]) : false;
}
2022-04-13 01:24:03 +02:00
#[\ReturnTypeWillChange]
2015-11-21 07:19:20 +01:00
public function next()
{
if (!$this->active) {
return;
}
if (next($this->active[0]) === false) {
array_shift($this->active);
if ($this->active) {
reset($this->active[0]);
}
}
}
2022-04-13 01:24:03 +02:00
#[\ReturnTypeWillChange]
2015-11-21 07:19:20 +01:00
public function valid()
{
2022-04-13 01:24:03 +02:00
return $this->active && (current($this->active[0] ?? []) !== false);
2015-11-21 07:19:20 +01:00
}
}