Skip to content

Commit

Permalink
make compatible with stream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 3, 2024
1 parent 84b0da5 commit 3faec66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"php": "^7.4 || ^8.0",
"nyholm/psr7": "^1.3",
"php-http/httplug": "^2.0",
"psr/http-client": "^1.0",
Expand Down
69 changes: 13 additions & 56 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ public function __construct(RequestInterface $request, $socket, ?int $size = nul
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
try {
return $this->getContents();
Expand All @@ -73,20 +70,14 @@ public function __toString()
}
}

/**
* {@inheritdoc}
*/
public function close()
public function close(): void
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
}
fclose($this->socket);
}

/**
* {@inheritdoc}
*/
public function detach()
{
if ($this->isDetached) {
Expand All @@ -104,15 +95,12 @@ public function detach()
*
* @return int<0, max>|null
*/
public function getSize()
public function getSize(): ?int
{
return $this->size;
}

/**
* {@inheritdoc}
*/
public function tell()
public function tell(): int
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -125,10 +113,7 @@ public function tell()
return $tell;
}

/**
* {@inheritdoc}
*/
public function eof()
public function eof(): bool
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -137,54 +122,32 @@ public function eof()
return feof($this->socket);
}

/**
* {@inheritdoc}
*/
public function isSeekable()
public function isSeekable(): bool
{
return false;
}

/**
* {@inheritdoc}
*
* @return void
*/
public function seek($offset, $whence = SEEK_SET)
public function seek($offset, $whence = SEEK_SET): void
{
throw new StreamException('This stream is not seekable');
}

/**
* {@inheritdoc}
*
* @return void
*/
public function rewind()
public function rewind(): void
{
throw new StreamException('This stream is not seekable');
}

/**
* {@inheritdoc}
*/
public function isWritable()
public function isWritable(): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function write($string)
public function write($string): int
{
throw new StreamException('This stream is not writable');
}

/**
* {@inheritdoc}
*/
public function isReadable()
public function isReadable(): bool
{
return true;
}
Expand All @@ -194,7 +157,7 @@ public function isReadable()
*
* @param int<0, max> $length
*/
public function read($length)
public function read($length): string
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand Down Expand Up @@ -232,10 +195,7 @@ public function read($length)
return $read;
}

/**
* {@inheritdoc}
*/
public function getContents()
public function getContents(): string
{
if ($this->isDetached || null === $this->socket) {
throw new StreamException('Stream is detached');
Expand All @@ -261,9 +221,6 @@ public function getContents()
return $contents;
}

/**
* {@inheritdoc}
*/
public function getMetadata($key = null)
{
if ($this->isDetached || null === $this->socket) {
Expand Down

0 comments on commit 3faec66

Please sign in to comment.