PHP Classes

File: docs/sonyflake.rst

Recommend this page to a friend!
  Packages of A. B. M. Mahmudul Hasan   UID (UUID, ULID, Snowflake, Sonyflake, Nano, CUID, KSUID, XID, TBSL)   docs/sonyflake.rst   Download  
File: docs/sonyflake.rst
Role: Example script
Content type: text/plain
Description: Example script
Class: UID (UUID, ULID, Snowflake, Sonyflake, Nano, CUID, KSUID, XID, TBSL)
Generate, parse, validate unique identifiers
Author: By
Last change:
Date: 8 days ago
Size: 1,650 bytes
 

Contents

Class file image Download
Sonyflake
=========

Class: ``Infocyph\\UID\\Sonyflake``

Bit Layout
----------

Sonyflake uses a 64-bit layout:

- 39 bits elapsed time in 10ms units from custom epoch
- 16 bits machine ID
- 8 bits sequence

Generation
----------

.. code-block:: php

   <?php

  
use Infocyph\UID\Sonyflake;

  
$id = Sonyflake::generate();
  
$idFromMachine = Sonyflake::generate(machineId: 42);

Configuration Object
--------------------

Use ``
Infocyph\\UID\\Configuration\\SonyflakeConfig`` for:

-
fixed or callback-resolved machine ID
- custom epoch
- custom sequence provider
- clock-backward policy
- output type (string/int/binary)

..
code-block:: php

  
<?php

  
use Infocyph\UID\Configuration\SonyflakeConfig;
   use
Infocyph\UID\Sonyflake;

  
$config = new SonyflakeConfig(machineId: 42);
  
$id = Sonyflake::generateWithConfig($config);

Validation and Parsing
----------------------

..
code-block:: php

  
<?php

  
use Infocyph\UID\Sonyflake;

  
Sonyflake::isValid($id);
  
$parsed = Sonyflake::parse($id);

``
parse()`` output:

- ``
time`` (DateTimeImmutable)
- ``
sequence`` (int)
- ``
machine_id`` (int)

Custom Epoch APIs
-----------------

- ``
Sonyflake::setStartTimeStamp('2020-01-01 00:00:00')``
- ``
Sonyflake::parseWithEpoch($id, $epochMs)``

Binary and Alternate Bases
--------------------------

- ``
Sonyflake::toBytes($id)`` / ``Sonyflake::fromBytes($bytes)``
- ``
Sonyflake::toBase($id, $base)`` / ``Sonyflake::fromBase($encoded, $base)``

Supported bases: ``16``, ``32``, ``36``, ``58``, ``62``.

Exception Types
---------------

- ``
Infocyph\\UID\\Exceptions\\SonyflakeException``
- ``
Infocyph\\UID\\Exceptions\\FileLockException``