1
0
Fork 0
mirror of https://github.com/shufflingpixels/php-io.git synced 2026-10-19 06:26:46 +02:00

Adopt Go-style minimal interfaces for streams

Replace PSR-7 StreamInterface with a set of small, composable interfaces
(ReaderInterface, WriterInterface, SeekerInterface and their
combinations).
BinaryReader now depends only on ReaderInterface, BinaryWriter on
WriterInterface, making both easy to satisfy with any byte source or
sink.

Buffer and Resource are simplified — no capability flags, no
detach/close
lifecycle, no PSR-7 metadata methods. LimitedResource is replaced by
LimitedReader, a lightweight reader that enforces a byte count limit.
BinaryReader::read() is renamed to readExact() for clarity.

Tests updated throughout to match the new APIs.
This commit is contained in:
Henrik Hautakoski 2026-06-27 08:40:11 +02:00
parent a27c0a87ea
commit 07ec405512
25 changed files with 451 additions and 1168 deletions

View file

@ -1,5 +1,6 @@
<?php
use Shufflingpixels\IO\BinaryReader;
use Shufflingpixels\IO\BinaryWriter;
use Shufflingpixels\IO\Buffer;
@ -9,25 +10,14 @@ function writer(): array
return [new BinaryWriter($buffer), $buffer];
}
it('proxies tell and seek', function () {
[$writer, $buffer] = writer();
expect($writer->tell())->toBe(0);
$writer->write('abc');
expect($writer->tell())->toBe(3);
$writer->seek(1);
expect($writer->tell())->toBe(1);
});
it('writes raw bytes', function () {
[$writer, $buffer] = writer();
$writer->write('hello');
expect((string) $buffer)->toBe('hello');
});
function bufferContents(Buffer $buffer): string
{
$pos = $buffer->tell();
$buffer->seek(0);
$result = $buffer->read($buffer->length());
$buffer->seek($pos);
return $result;
}
it('writes 8 bit integers', function () {
[$writer, $buffer] = writer();
@ -37,7 +27,7 @@ it('writes 8 bit integers', function () {
$writer->writeInt8(-1);
$writer->writeInt8(-128);
expect((string) $buffer)->toBe("\x7f\xff\xff\x80");
expect(bufferContents($buffer))->toBe("\x7f\xff\xff\x80");
});
it('writes 16 bit integers in little and big endian', function () {
@ -48,7 +38,7 @@ it('writes 16 bit integers in little and big endian', function () {
$writer->writeInt16LE(-1);
$writer->writeInt16BE(-32768);
expect((string) $buffer)->toBe(
expect(bufferContents($buffer))->toBe(
pack('v', 0x1234) . pack('n', 0x5678) . pack('v', 0xffff) . pack('n', 0x8000)
);
});
@ -61,7 +51,7 @@ it('writes 32 bit integers in little and big endian', function () {
$writer->writeInt32LE(-1);
$writer->writeInt32BE(-2147483648);
expect((string) $buffer)->toBe(
expect(bufferContents($buffer))->toBe(
pack('V', 0x12345678) . pack('N', 0x10203040) . pack('V', 0xffffffff) . pack('N', 0x80000000)
);
});
@ -71,7 +61,7 @@ it('writes a padded string shorter than the field length', function () {
$writer->writePaddedString('hi', 5);
expect((string) $buffer)->toBe("hi\x00\x00\x00");
expect(bufferContents($buffer))->toBe("hi\x00\x00\x00");
});
it('writes a padded string exactly matching the field length', function () {
@ -79,7 +69,7 @@ it('writes a padded string exactly matching the field length', function () {
$writer->writePaddedString('hello', 5);
expect((string) $buffer)->toBe('hello');
expect(bufferContents($buffer))->toBe('hello');
});
it('truncates a string longer than the field length', function () {
@ -87,7 +77,7 @@ it('truncates a string longer than the field length', function () {
$writer->writePaddedString('toolong', 4);
expect((string) $buffer)->toBe('tool');
expect(bufferContents($buffer))->toBe('tool');
});
it('uses a custom pad character', function () {
@ -95,7 +85,7 @@ it('uses a custom pad character', function () {
$writer->writePaddedString('hi', 5, ' ');
expect((string) $buffer)->toBe('hi ');
expect(bufferContents($buffer))->toBe('hi ');
});
it('throws for a multi-byte pad character', function () {
@ -112,8 +102,8 @@ it('written values round-trip through BinaryReader', function () {
$writer->writeInt16LE(-300);
$writer->writeUInt32BE(0xdeadbeef);
$buffer->rewind();
$reader = new \Shufflingpixels\IO\BinaryReader($buffer);
$buffer->seek(0);
$reader = new BinaryReader($buffer);
expect($reader->readUInt8())->toBe(42)
->and($reader->readInt16LE())->toBe(-300)