| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| @@ -11,16 +12,14 @@ | ||
| 11 | 12 | * this value and return fewer or more bytes than requested. Any extra data |
| 12 | 13 | * returned by the provided callable is buffered internally until drained using |
| 13 | 14 | * the read() function of the PumpStream. The provided callable MUST return |
| 14 | 15 | * false when there is no more data to read. |
| 15 | - * | |
| 16 | - * @final | |
| 17 | 16 | */ |
| 18 | -class PumpStream implements StreamInterface | |
| 17 | +final class PumpStream implements StreamInterface | |
| 19 | 18 | { |
| 20 | - /** @var callable */ | |
| 19 | + /** @var callable(int): (string|false|null)|null */ | |
| 21 | 20 | private $source; |
| 22 | - /** @var int */ | |
| 21 | + /** @var int|null */ | |
| 23 | 22 | private $size; |
| 24 | 23 | /** @var int */ |
| 25 | 24 | private $tellPos = 0; |
| 26 | 25 | /** @var array */ |
| @@ -27,79 +26,83 @@ | ||
| 27 | 26 | private $metadata; |
| 28 | 27 | /** @var BufferStream */ |
| 29 | 28 | private $buffer; |
| 30 | 29 | /** |
| 31 | - * @param callable $source Source of the stream data. The callable MAY | |
| 32 | - * accept an integer argument used to control the | |
| 33 | - * amount of data to return. The callable MUST | |
| 34 | - * return a string when called, or false on error | |
| 35 | - * or EOF. | |
| 36 | - * @param array $options Stream options: | |
| 37 | - * - metadata: Hash of metadata to use with stream. | |
| 38 | - * - size: Size of the stream, if known. | |
| 30 | + * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY | |
| 31 | + * accept an integer argument used to control the | |
| 32 | + * amount of data to return. The callable MUST | |
| 33 | + * return a string when called, or false|null on error | |
| 34 | + * or EOF. | |
| 35 | + * @param array{size?: int, metadata?: array} $options Stream options: | |
| 36 | + * - metadata: Hash of metadata to use with stream. | |
| 37 | + * - size: Size of the stream, if known. | |
| 39 | 38 | */ |
| 40 | 39 | public function __construct(callable $source, array $options = []) |
| 41 | 40 | { |
| 42 | 41 | $this->source = $source; |
| 43 | - $this->size = isset($options['size']) ? $options['size'] : null; | |
| 44 | - $this->metadata = isset($options['metadata']) ? $options['metadata'] : []; | |
| 42 | + $this->size = $options['size'] ?? null; | |
| 43 | + $this->metadata = $options['metadata'] ?? []; | |
| 45 | 44 | $this->buffer = new BufferStream(); |
| 46 | 45 | } |
| 47 | - public function __toString() | |
| 46 | + public function __toString() : string | |
| 48 | 47 | { |
| 49 | 48 | try { |
| 50 | 49 | return Utils::copyToString($this); |
| 51 | - } catch (\Exception $e) { | |
| 50 | + } catch (\Throwable $e) { | |
| 51 | + if (\PHP_VERSION_ID >= 70400) { | |
| 52 | + throw $e; | |
| 53 | + } | |
| 54 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 52 | 55 | return ''; |
| 53 | 56 | } |
| 54 | 57 | } |
| 55 | - public function close() | |
| 58 | + public function close() : void | |
| 56 | 59 | { |
| 57 | 60 | $this->detach(); |
| 58 | 61 | } |
| 59 | 62 | public function detach() |
| 60 | 63 | { |
| 61 | - $this->tellPos = \false; | |
| 64 | + $this->tellPos = 0; | |
| 62 | 65 | $this->source = null; |
| 63 | 66 | return null; |
| 64 | 67 | } |
| 65 | - public function getSize() | |
| 68 | + public function getSize() : ?int | |
| 66 | 69 | { |
| 67 | 70 | return $this->size; |
| 68 | 71 | } |
| 69 | - public function tell() | |
| 72 | + public function tell() : int | |
| 70 | 73 | { |
| 71 | 74 | return $this->tellPos; |
| 72 | 75 | } |
| 73 | - public function eof() | |
| 76 | + public function eof() : bool | |
| 74 | 77 | { |
| 75 | - return !$this->source; | |
| 78 | + return $this->source === null; | |
| 76 | 79 | } |
| 77 | - public function isSeekable() | |
| 80 | + public function isSeekable() : bool | |
| 78 | 81 | { |
| 79 | 82 | return \false; |
| 80 | 83 | } |
| 81 | - public function rewind() | |
| 84 | + public function rewind() : void | |
| 82 | 85 | { |
| 83 | 86 | $this->seek(0); |
| 84 | 87 | } |
| 85 | - public function seek($offset, $whence = \SEEK_SET) | |
| 88 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 86 | 89 | { |
| 87 | 90 | throw new \RuntimeException('Cannot seek a PumpStream'); |
| 88 | 91 | } |
| 89 | - public function isWritable() | |
| 92 | + public function isWritable() : bool | |
| 90 | 93 | { |
| 91 | 94 | return \false; |
| 92 | 95 | } |
| 93 | - public function write($string) | |
| 96 | + public function write($string) : int | |
| 94 | 97 | { |
| 95 | 98 | throw new \RuntimeException('Cannot write to a PumpStream'); |
| 96 | 99 | } |
| 97 | - public function isReadable() | |
| 100 | + public function isReadable() : bool | |
| 98 | 101 | { |
| 99 | 102 | return \true; |
| 100 | 103 | } |
| 101 | - public function read($length) | |
| 104 | + public function read($length) : string | |
| 102 | 105 | { |
| 103 | 106 | $data = $this->buffer->read($length); |
| 104 | 107 | $readLen = \strlen($data); |
| 105 | 108 | $this->tellPos += $readLen; |
| @@ -110,9 +113,9 @@ | ||
| 110 | 113 | $this->tellPos += \strlen($data) - $readLen; |
| 111 | 114 | } |
| 112 | 115 | return $data; |
| 113 | 116 | } |
| 114 | - public function getContents() | |
| 117 | + public function getContents() : string | |
| 115 | 118 | { |
| 116 | 119 | $result = ''; |
| 117 | 120 | while (!$this->eof()) { |
| 118 | 121 | $result .= $this->read(1000000); |
| @@ -118,20 +121,23 @@ | ||
| 118 | 121 | $result .= $this->read(1000000); |
| 119 | 122 | } |
| 120 | 123 | return $result; |
| 121 | 124 | } |
| 125 | + /** | |
| 126 | + * @return mixed | |
| 127 | + */ | |
| 122 | 128 | public function getMetadata($key = null) |
| 123 | 129 | { |
| 124 | 130 | if (!$key) { |
| 125 | 131 | return $this->metadata; |
| 126 | 132 | } |
| 127 | - return isset($this->metadata[$key]) ? $this->metadata[$key] : null; | |
| 133 | + return $this->metadata[$key] ?? null; | |
| 128 | 134 | } |
| 129 | - private function pump($length) | |
| 135 | + private function pump(int $length) : void | |
| 130 | 136 | { |
| 131 | - if ($this->source) { | |
| 137 | + if ($this->source !== null) { | |
| 132 | 138 | do { |
| 133 | - $data = \call_user_func($this->source, $length); | |
| 139 | + $data = ($this->source)($length); | |
| 134 | 140 | if ($data === \false || $data === null) { |
| 135 | 141 | $this->source = null; |
| 136 | 142 | return; |
| 137 | 143 | } |