b2pweb /
bdf-collections
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\Collection\Stream; |
||
| 4 | |||
| 5 | use AppendIterator; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Concatenate two or more Streams into one stream |
||
| 9 | * |
||
| 10 | * The streams will be iterated consecutively (The first iterator is the first iterated) |
||
| 11 | * |
||
| 12 | * @template T |
||
| 13 | * @implements StreamInterface<T, mixed> |
||
| 14 | * |
||
| 15 | * @see StreamInterface::concat() |
||
| 16 | */ |
||
| 17 | final class ConcatStream extends AppendIterator implements StreamInterface |
||
| 18 | { |
||
| 19 | use StreamTrait; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private $preserveKeys; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | private $key = 0; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * ConcatStream constructor. |
||
| 34 | * |
||
| 35 | * @param array<StreamInterface<T, mixed>> $streams |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 36 | * @param bool $preserveKeys Preserve the base stream keys |
||
| 37 | */ |
||
| 38 | 32 | public function __construct(array $streams, bool $preserveKeys = true) |
|
| 39 | { |
||
| 40 | 32 | parent::__construct(); |
|
| 41 | |||
| 42 | 32 | $this->preserveKeys = $preserveKeys; |
|
| 43 | |||
| 44 | 32 | foreach ($streams as $stream) { |
|
| 45 | 32 | $this->append($stream); |
|
| 46 | } |
||
| 47 | 32 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | 3 | public function concat(StreamInterface $stream, bool $preserveKeys = true): StreamInterface |
|
| 53 | { |
||
| 54 | // Do not preserve key for previous streams, but keep for new stream |
||
| 55 | // => Create a new ContactStream for keeping new stream keys |
||
| 56 | 3 | if (!$this->preserveKeys && $preserveKeys) { |
|
| 57 | 1 | return new ConcatStream([$this, $stream], true); |
|
| 58 | } |
||
| 59 | |||
| 60 | // If keys are not preserved for the last stream |
||
| 61 | // All the previous streams wil also loose their keys |
||
| 62 | 2 | if (!$preserveKeys) { |
|
| 63 | 1 | $this->preserveKeys = false; |
|
| 64 | } |
||
| 65 | |||
| 66 | 2 | $this->append($stream); |
|
| 67 | |||
| 68 | 2 | return $this; |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | #[\ReturnTypeWillChange] |
||
| 75 | 27 | public function key() |
|
| 76 | { |
||
| 77 | 27 | if ($this->preserveKeys) { |
|
| 78 | 17 | return parent::key(); |
|
| 79 | } |
||
| 80 | |||
| 81 | 12 | return $this->key; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 30 | public function next(): void |
|
| 88 | { |
||
| 89 | 30 | parent::next(); |
|
| 90 | |||
| 91 | 30 | ++$this->key; |
|
| 92 | 30 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | 31 | public function rewind(): void |
|
| 98 | { |
||
| 99 | 31 | parent::rewind(); |
|
| 100 | |||
| 101 | 31 | $this->key = 0; |
|
| 102 | 31 | } |
|
| 103 | } |
||
| 104 |