Skip to content

Commit

Permalink
Restore Pipeline\toIterator()
Browse files Browse the repository at this point in the history
Useful for implementing IteratorAggregate in classes implementing Pipeline but not using either AsyncGenerator or PipelineSource.
  • Loading branch information
trowski committed Nov 10, 2020
1 parent fa31b4b commit e99a7f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 1 addition & 3 deletions lib/AsyncGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public function getReturn(): mixed
*/
public function getIterator(): \Iterator
{
while (null !== $value = $this->continue()) {
yield $value;
}
return Pipeline\toIterator($this);
}
}
4 changes: 1 addition & 3 deletions lib/Internal/AutoDisposingPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function dispose(): void
*/
public function getIterator(): \Iterator
{
while (null !== $value = $this->continue()) {
yield $value;
}
return Pipeline\toIterator($this);
}
}
20 changes: 20 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,24 @@ function toArray(Pipeline $pipeline): array

return $array;
}

/**
* Converts the given pipeline to an object implementing \Iterator.
*
* @template TValue
*
* @param Pipeline $pipeline
*
* @psalm-param Pipeline<TValue> $pipeline
*
* @return \Iterator
*
* @psalm-return \Iterator<TValue>
*/
function toIterator(Pipeline $pipeline): \Iterator
{
while (null !== $value = $pipeline->continue()) {
yield $value;
}
}
}

0 comments on commit e99a7f3

Please sign in to comment.