Prevent recreate ByteBuffer objects

This commit is contained in:
2021-01-30 18:05:36 +03:00
parent f74b82a400
commit a7b847146a
2 changed files with 132 additions and 39 deletions

80
src/Buffer.php Normal file
View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Nsq;
use PHPinnacle\Buffer\ByteBuffer;
use const PHP_EOL;
final class Buffer
{
private ByteBuffer $buffer;
public function __construct(string $initial = '')
{
$this->buffer = new ByteBuffer($initial);
}
public function append(string $data): self
{
$this->buffer->append($data);
return $this;
}
public function appendCommand(string $command): void
{
$this->buffer->append($command.PHP_EOL);
}
public function appendData(string $data): void
{
$this->buffer->appendUint32(\strlen($data));
$this->buffer->append($data);
}
public function consumeSize(): int
{
/** @see Bytes::BYTES_SIZE */
return $this->buffer->consumeUint32();
}
public function consumeType(): int
{
/** @see Bytes::BYTES_TYPE */
return $this->buffer->consumeUint32();
}
public function consumeTimestamp(): int
{
/** @see Bytes::BYTES_TIMESTAMP */
return $this->buffer->consumeInt64();
}
public function consumeAttempts(): int
{
/** @see Bytes::BYTES_ATTEMPTS */
return $this->buffer->consumeUint16();
}
public function consumeId(): string
{
return $this->buffer->consume(Bytes::BYTES_ID);
}
public function size(): int
{
return $this->buffer->size();
}
public function bytes(): string
{
return $this->buffer->bytes();
}
public function flush(): string
{
return $this->buffer->flush();
}
}