Skip to content

Commit

Permalink
-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertHellmann committed Oct 24, 2019
1 parent 1cf7e2a commit 47a37b7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Reader/StreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,39 @@ final class StreamReader implements ReaderInterface
*/
private $nextConsumeLength = 0;

/**
* creates a new streamReader instance.
*
* @param StreamInterface $stream a PSR-7 stream to read from.
* @param int $bufferSize internal buffer size to use.
*/
public function __construct(StreamInterface $stream, int $bufferSize = 1024)
{
$this->stream = $stream;
$this->bufferSize = $bufferSize;
}

/**
* checks if reached end of stream.
*
* @return bool wheather we´re at the end of the stream or not.
*/
public function eof(): bool
{
//Expand buffer by one byte, most reliable method I guess (it has an eof check inbuilt)
$this->expandBuffer(1);
return \strlen($this->buffer) <= 0; //There was no way to expand the buffer, basically
}

/**
* peeks the specified length from the given stream and returns it´s assert string.
*
* peek won´t move the internal pointer of the stream forward. To move the pointer forward
* use consume() after peeking.
*
* @param int $length the amount of characters to peek (Default: 1).
* @return string the peeked string.
*/
public function peek(int $length = 1): string
{
$this->expandBuffer($length);
Expand Down Expand Up @@ -67,6 +87,11 @@ public function consume(int $length = 0): string
return $consumedBytes;
}

/**
* expands the internal buffer by the specified bufferSize if it´s below the specified length.
*
* @param int $length the amount of characters to expand the buffer by
*/
private function expandBuffer(int $length): void
{
if (\strlen($this->buffer) >= $length || $this->stream->eof()) {
Expand Down

0 comments on commit 47a37b7

Please sign in to comment.