mirror of
https://github.com/danog/amp.git
synced 2024-12-13 18:07:30 +01:00
42 lines
735 B
PHP
42 lines
735 B
PHP
|
<?php
|
||
|
|
||
|
namespace Amp\Stream;
|
||
|
|
||
|
use Amp\Promise;
|
||
|
use Amp\Stream;
|
||
|
use function Amp\call;
|
||
|
|
||
|
/**
|
||
|
* @template TValue
|
||
|
*/
|
||
|
final class ToArrayOperator
|
||
|
{
|
||
|
/** @var Promise<list<TValue>> */
|
||
|
private $promise;
|
||
|
|
||
|
/**
|
||
|
* @param Stream<TValue> $stream
|
||
|
*/
|
||
|
public function __construct(Stream $stream)
|
||
|
{
|
||
|
$this->promise = call(function () use ($stream) {
|
||
|
/** @psalm-var list $array */
|
||
|
$array = [];
|
||
|
|
||
|
while (list($value) = yield $stream->continue()) {
|
||
|
$array[] = $value;
|
||
|
}
|
||
|
|
||
|
return $array;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return Promise<list<TValue>>
|
||
|
*/
|
||
|
public function promise(): Promise
|
||
|
{
|
||
|
return $this->promise;
|
||
|
}
|
||
|
}
|