Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| IterableIterator | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/iterators |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Iterator; |
| 20 | |
| 21 | use Traversable; |
| 22 | use ArrayIterator; |
| 23 | |
| 24 | /** |
| 25 | * A normalized iterator wrapper that ensures any iterable (array or Traversable) |
| 26 | * is treated as a standard \Iterator. |
| 27 | * |
| 28 | * This utility class simplifies iterator interoperability by converting arrays |
| 29 | * into \ArrayIterator and wrapping \Traversable instances as needed. It SHALL |
| 30 | * be used when an \Iterator is expected but the input MAY be any iterable. |
| 31 | * |
| 32 | * Example usage: |
| 33 | * |
| 34 | * ```php |
| 35 | * $items = new IterableIterator([1, 2, 3]); |
| 36 | * |
| 37 | * foreach ($items as $item) { |
| 38 | * echo $item; |
| 39 | * } |
| 40 | * // Output: 123 |
| 41 | * ``` |
| 42 | * |
| 43 | * @since 1.1.0 |
| 44 | */ |
| 45 | class IterableIterator extends CountableIteratorIterator |
| 46 | { |
| 47 | /** |
| 48 | * Constructs an IterableIterator from any iterable input. |
| 49 | * |
| 50 | * Arrays are converted to \ArrayIterator; Traversables are passed directly. |
| 51 | * |
| 52 | * @param iterable $iterable the iterable to wrap as an \Iterator |
| 53 | */ |
| 54 | public function __construct(iterable $iterable) |
| 55 | { |
| 56 | parent::__construct( |
| 57 | $iterable instanceof Traversable ? $iterable : new ArrayIterator($iterable) |
| 58 | ); |
| 59 | } |
| 60 | } |