| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Iterator; |
| 4: | |
| 5: | use Salient\Contract\Iterator\FluentIteratorInterface; |
| 6: | use ArrayIterator; |
| 7: | use IteratorIterator; |
| 8: | use Traversable; |
| 9: | |
| 10: | /** |
| 11: | * Iterates over an iterable |
| 12: | * |
| 13: | * @api |
| 14: | * |
| 15: | * @template TKey of array-key |
| 16: | * @template TValue |
| 17: | * |
| 18: | * @extends IteratorIterator<TKey,TValue,Traversable<TKey,TValue>> |
| 19: | * @implements FluentIteratorInterface<TKey,TValue> |
| 20: | */ |
| 21: | class IterableIterator extends IteratorIterator implements FluentIteratorInterface |
| 22: | { |
| 23: | /** @use FluentIteratorTrait<TKey,TValue> */ |
| 24: | use FluentIteratorTrait; |
| 25: | |
| 26: | /** |
| 27: | * @api |
| 28: | * |
| 29: | * @param iterable<TKey,TValue> $iterable |
| 30: | */ |
| 31: | final public function __construct(iterable $iterable) |
| 32: | { |
| 33: | parent::__construct(is_array($iterable) |
| 34: | ? new ArrayIterator($iterable) |
| 35: | : $iterable); |
| 36: | } |
| 37: | |
| 38: | /** |
| 39: | * @template T0 of array-key |
| 40: | * @template T1 |
| 41: | * |
| 42: | * @param iterable<T0,T1> $iterable |
| 43: | * @return static<T0,T1> |
| 44: | */ |
| 45: | public static function from(iterable $iterable): self |
| 46: | { |
| 47: | // @phpstan-ignore return.type |
| 48: | return $iterable instanceof static |
| 49: | ? $iterable |
| 50: | : new static($iterable); |
| 51: | } |
| 52: | |
| 53: | /** |
| 54: | * @template T |
| 55: | * |
| 56: | * @param iterable<T> $iterable |
| 57: | * @return static<int,T> |
| 58: | */ |
| 59: | public static function fromValues(iterable $iterable): self |
| 60: | { |
| 61: | return new static(is_array($iterable) |
| 62: | ? array_values($iterable) |
| 63: | : self::generateValues($iterable)); |
| 64: | } |
| 65: | |
| 66: | /** |
| 67: | * @template T |
| 68: | * |
| 69: | * @param iterable<T> $iterable |
| 70: | * @return iterable<int,T> |
| 71: | */ |
| 72: | private static function generateValues(iterable $iterable): iterable |
| 73: | { |
| 74: | foreach ($iterable as $value) { |
| 75: | yield $value; |
| 76: | } |
| 77: | } |
| 78: | } |
| 79: |