| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Stream decorator trait |
| 9 |
* |
| 10 |
* @property StreamInterface $stream |
| 11 |
*/ |
| 12 |
trait StreamDecoratorTrait |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @param StreamInterface $stream Stream to decorate |
| 16 |
*/ |
| 17 |
public function __construct(StreamInterface $stream) |
| 18 |
{ |
| 19 |
$this->stream = $stream; |
| 20 |
} |
| 21 |
/** |
| 22 |
* Magic method used to create a new stream if streams are not added in |
| 23 |
* the constructor of a decorator (e.g., LazyOpenStream). |
| 24 |
* |
| 25 |
* @return StreamInterface |
| 26 |
*/ |
| 27 |
public function __get(string $name) |
| 28 |
{ |
| 29 |
if ($name === 'stream') { |
| 30 |
$this->stream = $this->createStream(); |
| 31 |
return $this->stream; |
| 32 |
} |
| 33 |
throw new \UnexpectedValueException("{$name} not found on class"); |
| 34 |
} |
| 35 |
public function __toString() : string |
| 36 |
{ |
| 37 |
try { |
| 38 |
if ($this->isSeekable()) { |
| 39 |
$this->seek(0); |
| 40 |
} |
| 41 |
return $this->getContents(); |
| 42 |
} catch (\Throwable $e) { |
| 43 |
if (\PHP_VERSION_ID >= 70400) { |
| 44 |
throw $e; |
| 45 |
} |
| 46 |
\trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
| 47 |
return ''; |
| 48 |
} |
| 49 |
} |
| 50 |
public function getContents() : string |
| 51 |
{ |
| 52 |
return Utils::copyToString($this); |
| 53 |
} |
| 54 |
/** |
| 55 |
* Allow decorators to implement custom methods |
| 56 |
* |
| 57 |
* @return mixed |
| 58 |
*/ |
| 59 |
public function __call(string $method, array $args) |
| 60 |
{ |
| 61 |
/** @var callable $callable */ |
| 62 |
$callable = [$this->stream, $method]; |
| 63 |
$result = $callable(...$args); |
| 64 |
// Always return the wrapped object if the result is a return $this |
| 65 |
return $result === $this->stream ? $this : $result; |
| 66 |
} |
| 67 |
public function close() : void |
| 68 |
{ |
| 69 |
$this->stream->close(); |
| 70 |
} |
| 71 |
/** |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
public function getMetadata($key = null) |
| 75 |
{ |
| 76 |
return $this->stream->getMetadata($key); |
| 77 |
} |
| 78 |
public function detach() |
| 79 |
{ |
| 80 |
return $this->stream->detach(); |
| 81 |
} |
| 82 |
public function getSize() : ?int |
| 83 |
{ |
| 84 |
return $this->stream->getSize(); |
| 85 |
} |
| 86 |
public function eof() : bool |
| 87 |
{ |
| 88 |
return $this->stream->eof(); |
| 89 |
} |
| 90 |
public function tell() : int |
| 91 |
{ |
| 92 |
return $this->stream->tell(); |
| 93 |
} |
| 94 |
public function isReadable() : bool |
| 95 |
{ |
| 96 |
return $this->stream->isReadable(); |
| 97 |
} |
| 98 |
public function isWritable() : bool |
| 99 |
{ |
| 100 |
return $this->stream->isWritable(); |
| 101 |
} |
| 102 |
public function isSeekable() : bool |
| 103 |
{ |
| 104 |
return $this->stream->isSeekable(); |
| 105 |
} |
| 106 |
public function rewind() : void |
| 107 |
{ |
| 108 |
$this->seek(0); |
| 109 |
} |
| 110 |
public function seek($offset, $whence = \SEEK_SET) : void |
| 111 |
{ |
| 112 |
$this->stream->seek($offset, $whence); |
| 113 |
} |
| 114 |
public function read($length) : string |
| 115 |
{ |
| 116 |
return $this->stream->read($length); |
| 117 |
} |
| 118 |
public function write($string) : int |
| 119 |
{ |
| 120 |
return $this->stream->write($string); |
| 121 |
} |
| 122 |
/** |
| 123 |
* Implement in subclasses to dynamically create streams when requested. |
| 124 |
* |
| 125 |
* @throws \BadMethodCallException |
| 126 |
*/ |
| 127 |
protected function createStream() : StreamInterface |
| 128 |
{ |
| 129 |
throw new \BadMethodCallException('Not implemented'); |
| 130 |
} |
| 131 |
} |
| 132 |
|