| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use YoastSEO_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 \YoastSEO_Vendor\Psr\Http\Message\StreamInterface |
| 16 |
{ |
| 17 |
/** @var int */ |
| 18 |
private $hwm; |
| 19 |
/** @var string */ |
| 20 |
private $buffer = ''; |
| 21 |
/** |
| 22 |
* @param int $hwm High water mark, representing the preferred maximum |
| 23 |
* buffer size. If the size of the buffer exceeds the high |
| 24 |
* water mark, then calls to write will continue to succeed |
| 25 |
* but will return 0 to inform writers to slow down |
| 26 |
* until the buffer has been drained by reading from it. |
| 27 |
*/ |
| 28 |
public function __construct(int $hwm = 16384) |
| 29 |
{ |
| 30 |
$this->hwm = $hwm; |
| 31 |
} |
| 32 |
public function __toString() : string |
| 33 |
{ |
| 34 |
return $this->getContents(); |
| 35 |
} |
| 36 |
public function getContents() : string |
| 37 |
{ |
| 38 |
$buffer = $this->buffer; |
| 39 |
$this->buffer = ''; |
| 40 |
return $buffer; |
| 41 |
} |
| 42 |
public function close() : void |
| 43 |
{ |
| 44 |
$this->buffer = ''; |
| 45 |
} |
| 46 |
public function detach() |
| 47 |
{ |
| 48 |
$this->close(); |
| 49 |
return null; |
| 50 |
} |
| 51 |
public function getSize() : ?int |
| 52 |
{ |
| 53 |
return \strlen($this->buffer); |
| 54 |
} |
| 55 |
public function isReadable() : bool |
| 56 |
{ |
| 57 |
return \true; |
| 58 |
} |
| 59 |
public function isWritable() : bool |
| 60 |
{ |
| 61 |
return \true; |
| 62 |
} |
| 63 |
public function isSeekable() : bool |
| 64 |
{ |
| 65 |
return \false; |
| 66 |
} |
| 67 |
public function rewind() : void |
| 68 |
{ |
| 69 |
$this->seek(0); |
| 70 |
} |
| 71 |
public function seek($offset, $whence = \SEEK_SET) : void |
| 72 |
{ |
| 73 |
if (!\is_int($offset)) { |
| 74 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.', \get_debug_type($offset)); |
| 75 |
} |
| 76 |
if (!\is_int($whence)) { |
| 77 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.', \get_debug_type($whence)); |
| 78 |
} |
| 79 |
throw new \RuntimeException('Cannot seek a BufferStream'); |
| 80 |
} |
| 81 |
public function eof() : bool |
| 82 |
{ |
| 83 |
return \strlen($this->buffer) === 0; |
| 84 |
} |
| 85 |
public function tell() : int |
| 86 |
{ |
| 87 |
throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
| 88 |
} |
| 89 |
/** |
| 90 |
* Reads data from the buffer. |
| 91 |
*/ |
| 92 |
public function read($length) : string |
| 93 |
{ |
| 94 |
if (!\is_int($length)) { |
| 95 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.', \get_debug_type($length)); |
| 96 |
} |
| 97 |
$currentLength = \strlen($this->buffer); |
| 98 |
if ($length >= $currentLength) { |
| 99 |
// No need to slice the buffer because we don't have enough data. |
| 100 |
$result = $this->buffer; |
| 101 |
$this->buffer = ''; |
| 102 |
} else { |
| 103 |
// Slice up the result to provide a subset of the buffer. |
| 104 |
$result = \substr($this->buffer, 0, $length); |
| 105 |
$this->buffer = \substr($this->buffer, $length); |
| 106 |
} |
| 107 |
return $result; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Writes data to the buffer. |
| 111 |
*/ |
| 112 |
public function write($string) : int |
| 113 |
{ |
| 114 |
if (!\is_string($string)) { |
| 115 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.', \get_debug_type($string)); |
| 116 |
} |
| 117 |
$this->buffer .= $string; |
| 118 |
if (\strlen($this->buffer) >= $this->hwm) { |
| 119 |
return 0; |
| 120 |
} |
| 121 |
return \strlen($string); |
| 122 |
} |
| 123 |
/** |
| 124 |
* @return mixed |
| 125 |
*/ |
| 126 |
public function getMetadata($key = null) |
| 127 |
{ |
| 128 |
if ($key !== null && !\is_string($key)) { |
| 129 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.', \get_debug_type($key)); |
| 130 |
} |
| 131 |
if ($key === 'hwm') { |
| 132 |
return $this->hwm; |
| 133 |
} |
| 134 |
return $key ? null : []; |
| 135 |
} |
| 136 |
} |
| 137 |
|