kafkiansky / binary
Tool for writing/reading bytes in any order.
Installs: 1 496
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- azjezz/psl: ^2.9
Requires (Dev)
- ergebnis/composer-normalize: ^2.41
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.18.4
- squizlabs/php_codesniffer: ^3.8
- vimeo/psalm: ^5.18
This package is auto-updated.
Last update: 2024-11-17 05:52:04 UTC
README
This package can be installed as a Composer dependency.
composer require kafkiansky/binary
This package requires PHP 8.1 or later.
Usage
Endianness
<?php declare(strict_types=1); require_once __DIR__.'/vendor/autoload.php'; use Kafkiansky\Binary\Endianness; $endian = Endianness::big(); // Big Endian. $endian = Endianness::little(); // Little Endian. $endian = Endianness::network(); // Big Endian. $endian = Endianness::native(); // Machine (native) byte order.
Buffer
<?php declare(strict_types=1); require_once __DIR__.'/vendor/autoload.php'; use Kafkiansky\Binary\{Buffer, Endianness}; $buffer = Buffer::empty(); // Network byte order (big endian) by default. $buffer = Buffer::empty(Endianness::little()); // Override byte order. // https://kafka.apache.org/protocol.html#The_Messages_CreateTopics $bytes = Buffer::empty() ->writeInt32(1) ->writeInt16(6) ->write('events') ->writeInt32(1) ->writeInt16(1) ->writeInt32(0) ->writeInt32(0) ->reset() ; $buffer = Buffer::fromString($bytes); var_dump( $buffer->consumeInt32(), $buffer->consume($buffer->consumeInt16()), $buffer->consumeInt32(), $buffer->consumeInt16(), $buffer->consumeInt32(), $buffer->consumeInt32(), \assert(0 === \count($buffer)), );
Stream
<?php declare(strict_types=1); require_once __DIR__.'/../vendor/autoload.php'; use Kafkiansky\Binary\Buffer; $resource = fopen('php://temp', 'a+'); \assert(\is_resource($resource)); $bytes = Buffer::fromResource($resource) ->writeInt32(1) ->writeInt16(6) ->write('events') ->writeInt32(1) ->writeInt16(1) ->writeInt32(0) ->writeInt32(0) ->reset() ; $buffer = Buffer::fromString($bytes); var_dump( $buffer->consumeInt32(), $buffer->consume($buffer->consumeInt16()), $buffer->consumeInt32(), $buffer->consumeInt16(), $buffer->consumeInt32(), $buffer->consumeInt32(), \assert(0 === \count($buffer)), );
Available types
-
int8
-
uint8
-
int16
-
uint16
-
int32
-
uint32
-
int64
-
uint64
-
f32 (float)
-
f64 (double)
-
string
-
varuint
-
varint
Testing
$ composer test
License
The MIT License (MIT). See License File for more information.