This package can be installed as a Composer dependency.
composer require kafkiansky/binary
This package requires PHP 8.1 or later.
<?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.
<?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->readInt32(),
$buffer->read($buffer->readInt16()),
$buffer->readInt32(),
$buffer->readInt16(),
$buffer->readInt32(),
$buffer->readInt32(),
\assert(0 === \count($buffer)),
);
-
int8
-
uint8
-
int16
-
uint16
-
int32
-
uint32
-
int64
-
uint64
-
f32 (float)
-
f64 (double)
-
string
$ composer test
The MIT License (MIT). See License File for more information.