Tiny Blocks

Encoder

tiny-blocks/encoder

Encoder and decoder for arbitrary data.

composer require tiny-blocks/encoder
  • Version 4.0.2, released Aug 7, 2026
  • PHP ^8.5
  • License MIT
  • Downloads 5,612 a month
  • Source
  • Packagist
On this page

Overview

Encoder and decoder for arbitrary data.

How to use

The library provides concrete implementations of the Encoder interface, enabling encoding and decoding of data into specific formats like Base62.

Using Base62

To encode a value into Base62 format:

<?php

declare(strict_types=1);

use TinyBlocks\Encoder\Base62;

$encoder = Base62::from(value: 'Hello world!');
$encoded = $encoder->encode();

# Output: T8dgcjRGuYUueWht

To decode a Base62-encoded value back to its original form:

<?php

declare(strict_types=1);

use TinyBlocks\Encoder\Base62;

$encoder = Base62::from(value: 'T8dgcjRGuYUueWht');
$decoded = $encoder->decode();

# Output: Hello world!

If you attempt to decode an invalid Base62 value, an InvalidDecoding exception will be thrown:

<?php

declare(strict_types=1);

use TinyBlocks\Encoder\Base62;
use TinyBlocks\Encoder\Exceptions\InvalidDecoding;

try {
    $encoder = Base62::from(value: 'invalid_value');
    $decoded = $encoder->decode();
} catch (InvalidDecoding $exception) {
    echo $exception->getMessage();
    # Output: The value <invalid_value> could not be decoded.
}