| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Decorator used to return only a subset of a stream. |
| 9 |
*/ |
| 10 |
final class LimitStream implements StreamInterface |
| 11 |
{ |
| 12 |
use StreamDecoratorTrait; |
| 13 |
use NonSerializableStreamTrait; |
| 14 |
/** @var int Offset to start reading from */ |
| 15 |
private int $offset; |
| 16 |
/** @var int Limit the number of bytes that can be read */ |
| 17 |
private int $limit; |
| 18 |
private StreamInterface $stream; |
| 19 |
/** |
| 20 |
* @param StreamInterface $stream Stream to wrap |
| 21 |
* @param int $limit Total number of bytes to allow to be read |
| 22 |
* from the stream. Pass -1 for no limit. |
| 23 |
* @param int $offset Position to seek to before reading (only |
| 24 |
* works on seekable streams). |
| 25 |
*/ |
| 26 |
public function __construct(StreamInterface $stream, int $limit = -1, int $offset = 0) |
| 27 |
{ |
| 28 |
$this->stream = $stream; |
| 29 |
$this->setLimit($limit); |
| 30 |
$this->setOffset($offset); |
| 31 |
} |
| 32 |
public function eof() : bool |
| 33 |
{ |
| 34 |
// Always return true if the underlying stream is EOF |
| 35 |
if ($this->stream->eof()) { |
| 36 |
return \true; |
| 37 |
} |
| 38 |
// No limit and the underlying stream is not at EOF |
| 39 |
if ($this->limit === -1) { |
| 40 |
return \false; |
| 41 |
} |
| 42 |
return $this->stream->tell() >= Integers::add($this->offset, $this->limit); |
| 43 |
} |
| 44 |
/** |
| 45 |
* Returns the size of the limited subset of data |
| 46 |
*/ |
| 47 |
public function getSize() : ?int |
| 48 |
{ |
| 49 |
if (null === ($length = $this->stream->getSize())) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
$size = $length - $this->offset; |
| 53 |
if ($this->limit !== -1) { |
| 54 |
$size = \min($this->limit, $size); |
| 55 |
} |
| 56 |
return \max(0, $size); |
| 57 |
} |
| 58 |
/** |
| 59 |
* Allow for a bounded seek on the read limited stream |
| 60 |
*/ |
| 61 |
public function seek(int $offset, int $whence = \SEEK_SET) : void |
| 62 |
{ |
| 63 |
if ($whence !== \SEEK_SET || $offset < 0) { |
| 64 |
throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence)); |
| 65 |
} |
| 66 |
$offset = Integers::add($this->offset, $offset); |
| 67 |
if ($this->limit !== -1) { |
| 68 |
$upperBound = Integers::add($this->offset, $this->limit); |
| 69 |
if ($offset > $upperBound) { |
| 70 |
$offset = $upperBound; |
| 71 |
} |
| 72 |
} |
| 73 |
$this->stream->seek($offset); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Give a relative tell() |
| 77 |
*/ |
| 78 |
public function tell() : int |
| 79 |
{ |
| 80 |
return $this->stream->tell() - $this->offset; |
| 81 |
} |
| 82 |
/** |
| 83 |
* Set the offset to start limiting from |
| 84 |
* |
| 85 |
* @param int $offset Offset to seek to and begin byte limiting from |
| 86 |
* |
| 87 |
* @throws \RuntimeException if the stream cannot be seeked. |
| 88 |
*/ |
| 89 |
public function setOffset(int $offset) : void |
| 90 |
{ |
| 91 |
$offset = Integers::assertNonNegativeInteger($offset, 'Offset'); |
| 92 |
$current = $this->stream->tell(); |
| 93 |
if ($current === $offset) { |
| 94 |
$this->offset = $offset; |
| 95 |
return; |
| 96 |
} |
| 97 |
// If the stream cannot seek to the offset position, then read to it. |
| 98 |
if ($this->stream->isSeekable()) { |
| 99 |
$this->stream->seek($offset); |
| 100 |
$this->offset = $offset; |
| 101 |
return; |
| 102 |
} |
| 103 |
if ($current > $offset) { |
| 104 |
throw new \RuntimeException("Could not seek to stream offset {$offset}"); |
| 105 |
} |
| 106 |
while ($current < $offset) { |
| 107 |
if ($this->stream->eof()) { |
| 108 |
$this->offset = $current; |
| 109 |
return; |
| 110 |
} |
| 111 |
$result = $this->stream->read($offset - $current); |
| 112 |
if ($result === '') { |
| 113 |
if ($this->stream->eof()) { |
| 114 |
$this->offset = $current; |
| 115 |
return; |
| 116 |
} |
| 117 |
throw new \RuntimeException("Could not seek to stream offset {$offset}"); |
| 118 |
} |
| 119 |
$current = Integers::add($current, \strlen($result)); |
| 120 |
} |
| 121 |
$this->offset = $offset; |
| 122 |
} |
| 123 |
/** |
| 124 |
* Set the limit of bytes that the decorator allows to be read from the |
| 125 |
* stream. |
| 126 |
* |
| 127 |
* @param int $limit Number of bytes to allow to be read from the stream. |
| 128 |
* Use -1 for no limit. |
| 129 |
*/ |
| 130 |
public function setLimit(int $limit) : void |
| 131 |
{ |
| 132 |
$this->limit = Integers::assertLimitInteger($limit, 'Limit'); |
| 133 |
} |
| 134 |
public function read(int $length) : string |
| 135 |
{ |
| 136 |
if ($length < 0) { |
| 137 |
throw new \RuntimeException('Length parameter cannot be negative'); |
| 138 |
} |
| 139 |
if ($this->limit === -1) { |
| 140 |
return $this->stream->read($length); |
| 141 |
} |
| 142 |
// Check if the current position is less than the total allowed |
| 143 |
// bytes + original offset |
| 144 |
$remaining = Integers::add($this->offset, $this->limit) - $this->stream->tell(); |
| 145 |
if ($remaining > 0) { |
| 146 |
// Only return the amount of requested data, ensuring that the byte |
| 147 |
// limit is not exceeded |
| 148 |
return $this->stream->read(\min($remaining, $length)); |
| 149 |
} |
| 150 |
return ''; |
| 151 |
} |
| 152 |
} |
| 153 |
|