PHP code example of kwaadpepper / serial-int-caster

1. Go to this page and download the library: Download kwaadpepper/serial-int-caster 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/ */

    

kwaadpepper / serial-int-caster example snippets


use Kwaadpepper\\Serial\\SerialCaster;
use Kwaadpepper\\Serial\\SerialCasterBuilder;
use Kwaadpepper\\Serial\\Converters\\BCMathBaseConverter;
use Kwaadpepper\\Serial\\Converters\\GmpBaseConverter;
use Kwaadpepper\\Serial\\Shufflers\\FisherYatesShuffler;

// Using BCMathBaseConverter
$int_to_encode = 9223372036854775807; // PHP_INT_MAX
$seed          = 1492;
$length        = 12;
$chars         = 'ABCDEFabcdef0123456789';

$caster_bcmath = (new SerialCasterBuilder(new BCMathBaseConverter()))
    ->withShuffler(new FisherYatesShuffler())
    ->withChars($chars)
    ->withLength($length)
    ->withSeed($seed)
    ->build();

$encoded_number_bcmath = $caster_bcmath->encode($int_to_encode);

// Prints TRUE
print_r($int_to_encode === $caster_bcmath->decode($encoded_number_bcmath));

// Using GmpBaseConverter
$int_to_encode = 9223372036854775807; // PHP_INT_MAX
$seed          = 1492;
$length        = 12;

$caster_gmp = (new SerialCasterBuilder(new GmpBaseConverter()))
    ->withShuffler(new FisherYatesShuffler())
    ->withChars($chars)
    ->withLength($length)
    ->withSeed($seed)
    ->build();

$encoded_number_gmp = $caster_gmp->encode($int_to_encode);

// Prints TRUE
print_r($int_to_encode === $caster_gmp->decode($encoded_number_gmp));

use Kwaadpepper\\Serial\\SerialCaster;
use Kwaadpepper\\Serial\\SerialCasterBuilder;
use Kwaadpepper\\Serial\\Converters\\NativeBaseConverter;
use Kwaadpepper\\Serial\\Shufflers\\FisherYatesShuffler;

$int_to_encode_native = 15;
$seed                 = 1492;
$length               = 6;
$chars                = '01234ABCDE';

$caster_native = (new SerialCasterBuilder(new NativeBaseConverter()))
    ->withShuffler(new FisherYatesShuffler())
    ->withChars($chars)
    ->withLength($length)
    ->withSeed($seed)
    ->build();

$encoded_number_native = $caster_native->encode($int_to_encode_native);

// Prints TRUE
print_r($int_to_encode_native === $caster_native->decode($encoded_number_native));