guifelix/ulid

A PHP package to generate Universally Unique Lexicographically Sortable Identifiers

Fund package maintenance!
guifelix

v0.1.0 2022-04-04 00:17 UTC

README

Latest Version on Packagist Tests Total Downloads

PHP Library to use ULID on your application.

Installation

You can install the package via composer:

composer require guifelix/php-ulid

Usage

Generate

use Guifelix\Ulid;

$ulid = Ulid::generate(); // Accept boolean as a parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ
echo $ulid->getTime(); //0001EH8YAE
echo $ulid->getRandomness(); //P8CXP4AMWCHHDBHJ
echo $ulid->isLowercase(); //false
echo $ulid->toTimestamp(); //1561622862

Generate from timestamp

use Guifelix\Ulid;

$ulid = Ulid::fromTimestamp(1561622862); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ

Generate from string (doesn't increment randomness)

use Guifelix\Ulid;

$ulid = Ulid::fromString('0001EH8YAEP8CXP4AMWCHHDBHJ'); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ

Validate

use Guifelix\Ulid;

Ulid::validate('8ZZZZZZZZZP8CXP4AMWCHHDBHI'); // Case insensitve
/**
 * validate Length, Crockford Characters and Time
 * Throws
 *  - InvalidUlidLengthException
 *  - InvalidUlidCharException
 *  - InvalidUlidTimestampException <- Max timestamp is 7ZZZZZZZZZ (281474976710655) or until the year 10889 AD :)
 *  - InvalidUlidException
 * /

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.