Skip to content

Commit

Permalink
Refactor to static constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shabouta committed Jan 16, 2019
1 parent 76ffeaa commit c23a21a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
82 changes: 39 additions & 43 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@ public function __construct(string $buffer = '')
}

/**
* Returns number of bytes in buffer.
* @param string|self $value
*
* @return int
* @return self
*/
public function size(): int
public function append($value): self
{
return $this->size;
}
if ($value instanceof Binary) {
$value = $value->data;
}

/**
* @return boolean
*/
public function empty(): bool
{
return $this->size === 0;
$this->data .= $value;
$this->size += \strlen($value);

return $this;
}

/**
Expand Down Expand Up @@ -108,19 +107,6 @@ public function consume(int $n): string
return $buffer;
}
}

/**
* @return string
*/
public function flush(): string
{
$data = $this->data;

$this->data = '';
$this->size = 0;

return $data;
}

/**
* @param int $n
Expand All @@ -147,25 +133,25 @@ public function discard(int $n): self
/**
* @param int $n
*
* @return self
* @return static
*/
public function slice(int $n): self
{
if ($this->size < $n) {
throw new Exception\BufferUnderflow;
} elseif ($this->size === $n) {
return new self($this->data);
return new static($this->data);
} else {
return new self(\substr($this->data, 0, $n));
return new static(\substr($this->data, 0, $n));
}
}

/**
* @param int $n
*
* @return self
* @return static
*/
public function consumeSlice(int $n): self
public function shift(int $n): self
{
if ($this->size < $n) {
throw new Exception\BufferUnderflow;
Expand All @@ -175,35 +161,45 @@ public function consumeSlice(int $n): self
$this->data = '';
$this->size = 0;

return new self($buffer);
return new static($buffer);

} else {
$buffer = \substr($this->data, 0, $n);

$this->data = \substr($this->data, $n);
$this->size -= $n;

return new self($buffer);
return new static($buffer);
}
}

/**
* Appends bytes at the end of the buffer.
*
* @param string|self $value
*
* @return self
* @return string
*/
public function append($value): self
public function flush(): string
{
if ($value instanceof Binary) {
$value = $value->data;
}
$data = $this->data;

$this->data .= $value;
$this->size += \strlen($value);
$this->data = '';
$this->size = 0;

return $this;
return $data;
}

/**
* @return int
*/
public function size(): int
{
return $this->size;
}

/**
* @return boolean
*/
public function empty(): bool
{
return $this->size === 0;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/BinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ public function testConsumeSlice()
{
$buf = new Binary('abcdef');

$slice1 = $buf->consumeSlice(1);
$slice1 = $buf->shift(1);
$this->assertEquals('a', $slice1->read($slice1->size()));
$this->assertEquals(5, $buf->size());

$slice2 = $buf->consumeSlice(2);
$slice2 = $buf->shift(2);
$this->assertEquals('bc', $slice2->read($slice2->size()));
$this->assertEquals(3, $buf->size());

$slice3 = $buf->consumeSlice(3);
$slice3 = $buf->shift(3);
$this->assertEquals('def', $slice3->read($slice3->size()));
$this->assertEquals(0, $buf->size());
}
Expand All @@ -193,7 +193,7 @@ public function testConsumeSlice()
public function testConsumeSliceThrows()
{
$buf = new Binary;
$buf->consumeSlice(1);
$buf->shift(1);
}

public function testAppend()
Expand Down

0 comments on commit c23a21a

Please sign in to comment.