*/ private $stream; /** * @param Stream $stream */ public function __construct(Stream $stream) { $this->stream = $stream; } /** * @return Stream */ public function stream(): Stream { return $this->stream; } public function apply(callable $operator): self { $clone = clone $this; $clone->stream = new Stream\ApplyStream($clone->stream, $operator); return $clone; } /** * @template TMap * * @param callable(TValue, int):TMap $onYield * * @return self */ public function map(callable $onYield): self { $clone = clone $this; $clone->stream = new Stream\MapStream($clone->stream, $onYield); return $clone; } /** * @param callable(TValue, int):bool $filter * * @return self */ public function filter(callable $filter): self { $clone = clone $this; $clone->stream = new Stream\FilterStream($clone->stream, $filter); return $clone; } /** * @param callable(TValue, int):void $onYield * * @return Promise */ public function each(callable $onYield): Promise { return (new EachOperator($this->stream, $onYield))->promise(); } /** * @param int $count * * @return self */ public function drop(int $count): self { $clone = clone $this; $clone->stream = new Stream\DropStream($clone->stream, $count); return $clone; } /** * @param int $limit * * @return self */ public function limit(int $limit): self { $clone = clone $this; $clone->stream = new Stream\LimitStream($clone->stream, $limit); return $clone; } /** * @return Promise> */ public function toArray(): Promise { return (new Stream\ToArrayOperator($this->stream))->promise(); } }