PHP code example of adambean / randflake-id-php

1. Go to this page and download the library: Download adambean/randflake-id-php 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/ */

    

adambean / randflake-id-php example snippets




declare(strict_types=1);

use Adambean\RandflakeId\Generator;

// Create a secret string for encryption
$secret = Generator::generateSecret();

// Create a secret without symbols if you prefer (digits and mixed case letters only)
$secret = Generator::generateSecret(true);



declare(strict_types=1);

use Adambean\RandflakeId\Generator;

// Create an instance of the generator library
$generator = new Generator($nodeId, $secret);

// Optionally create an instance of the generator library with a limited node lease time (e.g. for 1 day)
$generator = new Generator($nodeId, $secret, time(), time() + 86400);

// Generate an ID
$id = $generator->generate();

// Generate an encrypted ID
$idEncrypted = $generator->generate(true);

// Generate a non-encrypted but encoded ID
$idEncoded = $generator->generate(false, true);

// Generate an encrypted and encoded ID
$idEncryptedAndEncoded = $generator->generate(true, true);

// Encrypt an existing raw ID
$idEncrypted = $generator->encryptId($id);

// Decrypt an existing encrypted ID
$id = $generator->decryptId($idEncrypted);

// Encode an integer string ID to Base32Hex
$idEncoded = $generator->encodeId($id);

// Decode a Base32Hex encoded ID back to an integer string
$id = $generator->decodeId($idEncoded);

// Get the timestamp, node ID, and sequence of an existing ID
$idDetails = $generator->inspect($id); // If it was raw
$idDetails = $generator->inspect($idEncrypted, true); // If it was encrypted

// Assert if an ID is valid (encoded or not)
$generator->isIdValid($id);

/**
 * @phpstan-import-type RandflakeIdDetailsArray from Generator
 */

$generator->changeLease($generator->getLeaseEnd() + 86400); // Extend the existing lease by 1 day

// Convert a numeric string ID to an integer for database storage
$idForDatabase = $generator->stringToInt($id);

// Convert an integer ID from the database back to a numeric string
$id = $generator->intToString($idFromDatabase);
sh
php -r 'echo PHP_INT_SIZE;'