| 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 |
* Provides a buffer stream that can be written to to fill a buffer, and read |
| 9 |
* from to remove bytes from the buffer. |
| 10 |
* |
| 11 |
* This stream returns a "hwm" metadata value that tells upstream consumers |
| 12 |
* what the configured high water mark of the stream is, or the maximum |
| 13 |
* preferred size of the buffer. |
| 14 |
*/ |
| 15 |
final class BufferStream implements StreamInterface |
| 16 |
{ |
| 17 |
use NonSerializableStreamTrait; |
| 18 |
private int $hwm; |
| 19 |
private string $buffer = ''; |
| 20 |
/** |
| 21 |
* @param int $hwm High water mark, representing the preferred maximum |
| 22 |
* buffer size. If the size of the buffer reaches or exceeds |
| 23 |
* the high water mark, then calls to write will continue to |
| 24 |
* succeed but will return 0 to inform writers to slow down |
| 25 |
* until the buffer has been drained by reading from it. |
| 26 |
*/ |
| 27 |
public function __construct(int $hwm = 16384) |
| 28 |
{ |
| 29 |
$this->hwm = Integers::assertNonNegativeInteger($hwm, 'High water mark'); |
| 30 |
} |
| 31 |
public function __toString() : string |
| 32 |
{ |
| 33 |
return $this->getContents(); |
| 34 |
} |
| 35 |
public function getContents() : string |
| 36 |
{ |
| 37 |
$buffer = $this->buffer; |
| 38 |
$this->buffer = ''; |
| 39 |
return $buffer; |
| 40 |
} |
| 41 |
public function close() : void |
| 42 |
{ |
| 43 |
$this->buffer = ''; |
| 44 |
} |
| 45 |
public function detach() |
| 46 |
{ |
| 47 |
$this->close(); |
| 48 |
return null; |
| 49 |
} |
| 50 |
public function getSize() : ?int |
| 51 |
{ |
| 52 |
return \strlen($this->buffer); |
| 53 |
} |
| 54 |
public function isReadable() : bool |
| 55 |
{ |
| 56 |
return \true; |
| 57 |
} |
| 58 |
public function isWritable() : bool |
| 59 |
{ |
| 60 |
return \true; |
| 61 |
} |
| 62 |
public function isSeekable() : bool |
| 63 |
{ |
| 64 |
return \false; |
| 65 |
} |
| 66 |
public function rewind() : void |
| 67 |
{ |
| 68 |
$this->seek(0); |
| 69 |
} |
| 70 |
public function seek(int $offset, int $whence = \SEEK_SET) : void |
| 71 |
{ |
| 72 |
throw new \RuntimeException('Cannot seek a BufferStream'); |
| 73 |
} |
| 74 |
public function eof() : bool |
| 75 |
{ |
| 76 |
return \strlen($this->buffer) === 0; |
| 77 |
} |
| 78 |
public function tell() : int |
| 79 |
{ |
| 80 |
throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
| 81 |
} |
| 82 |
/** |
| 83 |
* Reads data from the buffer. |
| 84 |
*/ |
| 85 |
public function read(int $length) : string |
| 86 |
{ |
| 87 |
if ($length < 0) { |
| 88 |
throw new \RuntimeException('Length parameter cannot be negative'); |
| 89 |
} |
| 90 |
$currentLength = \strlen($this->buffer); |
| 91 |
if ($length >= $currentLength) { |
| 92 |
// No need to slice the buffer because we don't have enough data. |
| 93 |
$result = $this->buffer; |
| 94 |
$this->buffer = ''; |
| 95 |
} else { |
| 96 |
// Slice up the result to provide a subset of the buffer. |
| 97 |
$result = \substr($this->buffer, 0, $length); |
| 98 |
$this->buffer = \substr($this->buffer, $length); |
| 99 |
} |
| 100 |
return $result; |
| 101 |
} |
| 102 |
/** |
| 103 |
* Writes data to the buffer. |
| 104 |
*/ |
| 105 |
public function write(string $string) : int |
| 106 |
{ |
| 107 |
$this->buffer .= $string; |
| 108 |
if (\strlen($this->buffer) >= $this->hwm) { |
| 109 |
return 0; |
| 110 |
} |
| 111 |
return \strlen($string); |
| 112 |
} |
| 113 |
/** |
| 114 |
* @return mixed |
| 115 |
*/ |
| 116 |
public function getMetadata(?string $key = null) |
| 117 |
{ |
| 118 |
if ($key === 'hwm') { |
| 119 |
return $this->hwm; |
| 120 |
} |
| 121 |
return $key === null ? [] : null; |
| 122 |
} |
| 123 |
} |
| 124 |
|