PHP code example of inodrahq / bcs

1. Go to this page and download the library: Download inodrahq/bcs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inodrahq / bcs example snippets


use Inodrahq\Bcs\Encoder;
use Inodrahq\Bcs\Decoder;
use Inodrahq\Bcs\U64;
use Inodrahq\Bcs\BcsString;
use Inodrahq\Bcs\Vector;
use Inodrahq\Bcs\Boolean;

// Encode a u64
$encoder = new Encoder();
(new U64(1000000))->encode($encoder);
$bytes = $encoder->getBytes(); // raw BCS bytes

// Decode it back
$decoder = new Decoder($bytes);
$value = U64::decode($decoder);
echo $value->getValue(); // "1000000"

// Encode a string
$encoder = new Encoder();
(new BcsString('hello'))->encode($encoder);

// Encode a vector of u64s
$encoder = new Encoder();
$vec = new Vector(U64::class, [new U64(1), new U64(2), new U64(3)]);
$vec->encode($encoder);

use Inodrahq\Bcs\Struct;
use Inodrahq\Bcs\U64;
use Inodrahq\Bcs\BcsString;

// Define and encode a struct
$struct = new Struct([
    'name' => new BcsString('Alice'),
    'balance' => new U64(1000),
]);

$encoder = new Encoder();
$struct->encode($encoder);

use Inodrahq\Bcs\Enum;
use Inodrahq\Bcs\U64;
use Inodrahq\Bcs\BcsString;

// Variant 0 with a string payload
$enum = new Enum(0, new BcsString('hello'));

// Variant 1 with a u64 payload
$enum = new Enum(1, new U64(42));

use Inodrahq\Bcs\Option;
use Inodrahq\Bcs\U64;

$some = Option::some(new U64(42));
$none = Option::none();

use Inodrahq\Bcs\Map;
use Inodrahq\Bcs\BcsString;
use Inodrahq\Bcs\U64;

$map = new Map(BcsString::class, U64::class, [
    [new BcsString('alice'), new U64(100)],
    [new BcsString('bob'), new U64(200)],
]);

use Inodrahq\Bcs\Tuple;
use Inodrahq\Bcs\U64;
use Inodrahq\Bcs\BcsString;

// Heterogeneous ordered collection
$tuple = new Tuple([U64::class, BcsString::class], [new U64(42), new BcsString('hello')]);
$encoder = new Encoder();
$tuple->encode($encoder);

// Decode with type context
$decoder = new Decoder($encoder->getBytes());
$decoded = Tuple::decodeWithTypes($decoder, [U64::class, BcsString::class]);
echo $decoded->get(0)->getValue(); // "42"
echo $decoded->get(1)->getValue(); // "hello"

use Inodrahq\Bcs\FixedArray;
use Inodrahq\Bcs\U8;

// Fixed-size array (no length prefix, unlike Vector)
$arr = new FixedArray(U8::class, [new U8(1), new U8(2), new U8(3)], 3);
$encoder = new Encoder();
$arr->encode($encoder);

// Decode with known size
$decoder = new Decoder($encoder->getBytes());
$decoded = FixedArray::decodeWithType($decoder, U8::class, 3);

use Inodrahq\Bcs\Decoder;
use Inodrahq\Bcs\Vector;
use Inodrahq\Bcs\Struct;
use Inodrahq\Bcs\Enum;
use Inodrahq\Bcs\Option;
use Inodrahq\Bcs\Map;

// Decode from hex
$decoder = Decoder::fromHex('03010203');
$vec = Vector::decodeWithType($decoder, U8::class);

// Struct decoding (fields must match encoding order)
$decoder = new Decoder($bytes);
$struct = Struct::decodeWithTypes($decoder, [
    'name' => BcsString::class,
    'balance' => U64::class,
]);
echo $struct->getField('name')->getValue();

// Enum decoding (map variant tag => type class, null for no data)
$decoder = new Decoder($bytes);
$enum = Enum::decodeWithVariants($decoder, [
    0 => BcsString::class, // variant 0 has a string
    1 => U64::class,       // variant 1 has a u64
    2 => null,             // variant 2 has no data
]);

// Option decoding
$decoder = new Decoder($bytes);
$opt = Option::decodeWithType($decoder, U64::class);
if ($opt->isSome()) {
    echo $opt->getValue()->getValue();
}

// Map decoding
$decoder = new Decoder($bytes);
$map = Map::decodeWithTypes($decoder, BcsString::class, U64::class);

use Inodrahq\Bcs\BcsType;
use Inodrahq\Bcs\Encoder;
use Inodrahq\Bcs\Decoder;

class MyType implements BcsType
{
    public function encode(Encoder $encoder): void
    {
        // Write your bytes
    }

    public static function decode(Decoder $decoder): static
    {
        // Read and return
    }
}

$encoder = new Encoder();
(new U64(255))->encode($encoder);
echo bin2hex($encoder->getBytes()); // "ff00000000000000"