123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- declare(strict_types=1);
- namespace GuzzleHttp\Psr7;
- use Psr\Http\Message\StreamInterface;
- final class BufferStream implements StreamInterface
- {
-
- private $hwm;
-
- private $buffer = '';
-
- public function __construct(int $hwm = 16384)
- {
- $this->hwm = $hwm;
- }
- public function __toString(): string
- {
- return $this->getContents();
- }
- public function getContents(): string
- {
- $buffer = $this->buffer;
- $this->buffer = '';
- return $buffer;
- }
- public function close(): void
- {
- $this->buffer = '';
- }
- public function detach()
- {
- $this->close();
- return null;
- }
- public function getSize(): ?int
- {
- return strlen($this->buffer);
- }
- public function isReadable(): bool
- {
- return true;
- }
- public function isWritable(): bool
- {
- return true;
- }
- public function isSeekable(): bool
- {
- return false;
- }
- public function rewind(): void
- {
- $this->seek(0);
- }
- public function seek($offset, $whence = SEEK_SET): void
- {
- throw new \RuntimeException('Cannot seek a BufferStream');
- }
- public function eof(): bool
- {
- return strlen($this->buffer) === 0;
- }
- public function tell(): int
- {
- throw new \RuntimeException('Cannot determine the position of a BufferStream');
- }
-
- public function read($length): string
- {
- $currentLength = strlen($this->buffer);
- if ($length >= $currentLength) {
-
- $result = $this->buffer;
- $this->buffer = '';
- } else {
-
- $result = substr($this->buffer, 0, $length);
- $this->buffer = substr($this->buffer, $length);
- }
- return $result;
- }
-
- public function write($string): int
- {
- $this->buffer .= $string;
- if (strlen($this->buffer) >= $this->hwm) {
- return 0;
- }
- return strlen($string);
- }
-
- public function getMetadata($key = null)
- {
- if ($key === 'hwm') {
- return $this->hwm;
- }
- return $key ? null : [];
- }
- }
|