mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-30 04:19:23 +01:00
Reject non-strings in IteratorStream
This commit is contained in:
parent
2049e76771
commit
6b369657a1
@ -3,11 +3,13 @@
|
||||
namespace Amp\ByteStream;
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Failure;
|
||||
use Amp\Iterator;
|
||||
use Amp\Promise;
|
||||
|
||||
class IteratorStream implements InputStream {
|
||||
private $iterator;
|
||||
private $exception;
|
||||
|
||||
public function __construct(Iterator $iterator) {
|
||||
$this->iterator = $iterator;
|
||||
@ -15,13 +17,31 @@ class IteratorStream implements InputStream {
|
||||
|
||||
/** @inheritdoc */
|
||||
public function read(): Promise {
|
||||
if ($this->exception) {
|
||||
return new Failure($this->exception);
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
|
||||
$this->iterator->advance()->onResolve(function ($error, $hasNextElement) use ($deferred) {
|
||||
if ($error) {
|
||||
$this->exception = $error;
|
||||
$deferred->fail($error);
|
||||
} elseif ($hasNextElement) {
|
||||
$deferred->resolve($this->iterator->getCurrent());
|
||||
$chunk = $this->iterator->getCurrent();
|
||||
|
||||
if (!\is_string($chunk)) {
|
||||
$this->exception = new StreamException(\sprintf(
|
||||
"Unexpected iterator value of type '%s', expected string",
|
||||
\is_object($chunk) ? \get_class($chunk) : \gettype($chunk)
|
||||
));
|
||||
|
||||
$deferred->fail($this->exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$deferred->resolve($chunk);
|
||||
} else {
|
||||
$deferred->resolve(null);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user