PHP code example of niktomo / konayuki

1. Go to this page and download the library: Download niktomo/konayuki 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/ */

    

niktomo / konayuki example snippets


use Konayuki\Laravel\Facades\Konayuki;

$id = Konayuki::next();        // SnowflakeId
$id->value;                    // 7_123_456_789_012_345  (PHP int, 63-bit)
$id->timestamp();              // 1_712_345_678_000 (ms)
$id->workerId();               // auto-allocated via flock
$id->sequence();               // 0..4095 within ms

'layout' => [
    'timestamp_bits' => 41,
    'worker_bits'    => 10,
    'sequence_bits'  => 12,
],

// PHP: serialize as string
'id' => (string) $id->value,

// PHP: serialize as string
'id' => (string) $id->value,

use Kasumi\Scrambler;

$scrambler = new Scrambler(salt: (int) env('SCRAMBLE_SALT'));

// encode
'id' => (string) $scrambler->scramble($id->value),   // "0yhgff51j5fube" (14-char base36)

// decode (involutory — same call)
$originalValue = $scrambler->scramble($encoded)->toInt();

use Konayuki\IdGenerator;
use Konayuki\Apcu\ApcuAtomicCounter;
use Konayuki\SystemClock;
use Konayuki\Layout;
use Konayuki\RealTimestamp;

$generator = new IdGenerator(
    counter: new ApcuAtomicCounter(),
    clock: new SystemClock(),
    layout: new Layout(epochMs: 1_767_225_600_000),
    timestamp: new RealTimestamp(),
    workerId: 0,
);
ini
; php.ini
apc.shm_size = 128M
apc.enable_cli = 1