|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of php-fast-forward/iterators. |
| 7 | + * |
| 8 | + * This source file is subject to the license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @link https://github.com/php-fast-forward/iterators |
| 12 | + * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <[email protected]> |
| 13 | + * @license https://opensource.org/licenses/MIT MIT License |
| 14 | + */ |
| 15 | + |
| 16 | +namespace FastForward\Iterator; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class ChainIterableIterator. |
| 20 | + * |
| 21 | + * An iterator that chains multiple iterable sources together into a single unified iterator. |
| 22 | + * |
| 23 | + * This iterator SHALL accept any number of iterable values (arrays, Traversables, or Iterators) |
| 24 | + * and iterate over them in order. When the current iterator is exhausted, it proceeds to the next. |
| 25 | + * |
| 26 | + * The class MUST ensure all incoming values are wrapped as \Iterator instances, either natively |
| 27 | + * or by converting Traversables or arrays using standard SPL iterators. |
| 28 | + * |
| 29 | + * Example usage: |
| 30 | + * |
| 31 | + * ```php |
| 32 | + * $it = new ChainIterableIterator([1, 2], new ArrayIterator([3, 4])); |
| 33 | + * foreach ($it as $value) { |
| 34 | + * echo $value; |
| 35 | + * } |
| 36 | + * // Output: 1234 |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * @package FastForward\Iterator |
| 40 | + * |
| 41 | + * @since 1.1.0 |
| 42 | + */ |
| 43 | +final class ChainIterableIterator implements \Iterator |
| 44 | +{ |
| 45 | + /** |
| 46 | + * @var \Iterator[] a list of iterators chained in sequence |
| 47 | + */ |
| 48 | + private array $iterators; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var int the index of the currently active iterator |
| 52 | + */ |
| 53 | + private int $currentIndex = 0; |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructs a ChainIterableIterator with one or more iterable sources. |
| 57 | + * |
| 58 | + * Each iterable SHALL be normalized to a \Iterator instance using: |
| 59 | + * - \ArrayIterator for arrays |
| 60 | + * - \IteratorIterator for Traversable objects |
| 61 | + * - Directly used if already an \Iterator |
| 62 | + * |
| 63 | + * @param iterable ...$iterables One or more iterable data sources to chain. |
| 64 | + */ |
| 65 | + public function __construct(iterable ...$iterables) |
| 66 | + { |
| 67 | + $this->iterators = array_map( |
| 68 | + static fn (iterable $iterable) => new IterableIterator($iterable), |
| 69 | + $iterables |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Rewinds all underlying iterators and resets the position. |
| 75 | + * |
| 76 | + * Each chained iterator SHALL be rewound to its beginning. |
| 77 | + */ |
| 78 | + public function rewind(): void |
| 79 | + { |
| 80 | + foreach ($this->iterators as $iterable) { |
| 81 | + $iterable->rewind(); |
| 82 | + } |
| 83 | + |
| 84 | + $this->currentIndex = 0; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks whether the current position is valid across chained iterators. |
| 89 | + * |
| 90 | + * Iteration continues until the current iterator is valid or all are exhausted. |
| 91 | + * |
| 92 | + * @return bool TRUE if there are more elements to iterate; FALSE otherwise |
| 93 | + */ |
| 94 | + public function valid(): bool |
| 95 | + { |
| 96 | + while (isset($this->iterators[$this->currentIndex])) { |
| 97 | + if ($this->iterators[$this->currentIndex]->valid()) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + ++$this->currentIndex; |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the current element from the active iterator. |
| 108 | + * |
| 109 | + * @return null|mixed the current element or NULL if iteration is invalid |
| 110 | + */ |
| 111 | + public function current(): mixed |
| 112 | + { |
| 113 | + if (!$this->valid()) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + return $this->iterators[$this->currentIndex]->current(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the current key from the active iterator. |
| 122 | + * |
| 123 | + * @return null|mixed the current key or NULL if iteration is invalid |
| 124 | + */ |
| 125 | + public function key(): mixed |
| 126 | + { |
| 127 | + if (!$this->valid()) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + return $this->iterators[$this->currentIndex]->key(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Moves the pointer of the active iterator forward. |
| 136 | + */ |
| 137 | + public function next(): void |
| 138 | + { |
| 139 | + if (!$this->valid()) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + $this->iterators[$this->currentIndex]->next(); |
| 144 | + } |
| 145 | +} |
0 commit comments