ChainIterableIterator
An iterator that chains multiple iterable sources together into a single unified iterator.
Description
This iterator SHALL accept any number of iterable values (arrays, Traversables, or Iterators) and iterate over them in order. When the current iterator is exhausted, it proceeds to the next.
The class MUST ensure all incoming values are wrapped as \Iterator instances, either natively or by converting Traversables or arrays using standard SPL iterators.
Example usage:
$it = new ChainIterableIterator([1, 2], new ArrayIterator([3, 4]));
foreach ($it as $value) {
echo $value;
}
// Output: 1234
Interfaces
Properties
Methods
Constructs a ChainIterableIterator with one or more iterable sources.
Counts the total number of elements across all chained iterators.
Returns the current element from the active iterator.
Moves the pointer of the active iterator forward.
Rewinds all underlying iterators and resets the position.
Checks whether the current position is valid across chained iterators.
private
int
$currentIndex
=
0
Description
the index of the currently active iterator
private
array<string|int, Iterator>
$iterators
Description
a list of iterators chained in sequence
private
int
$position
=
0
Description
the normalized sequential key across all chained iterators
Constructs a ChainIterableIterator with one or more iterable sources.
public
__construct(
iterable<string|int, mixed>
...$iterables) : mixed
Description
Each iterable SHALL be normalized to a \Iterator instance using:
- \ArrayIterator for arrays
- \IteratorIterator for Traversable objects
- Directly used if already an \Iterator
Parameters
$iterables
:
iterable<string|int, mixed>
Description
One or more iterable data sources to chain.
Counts the total number of elements across all chained iterators.
public
count() : int
Description
This method iterates through each underlying iterator and sums their counts.
Return values
Description
the total count of elements in all chained iterators
Returns the current element from the active iterator.
public
current() : mixed|null
Return values
Description
the current element or NULL if iteration is invalid
public
key() : int|null
Return values
Moves the pointer of the active iterator forward.
public
next() : void
Rewinds all underlying iterators and resets the position.
public
rewind() : void
Description
Each chained iterator SHALL be rewound to its beginning.
Checks whether the current position is valid across chained iterators.
public
valid() : bool
Description
Iteration continues until the current iterator is valid or all are exhausted.
Return values
Description
TRUE if there are more elements to iterate; FALSE otherwise