PHP Classes

PHP Generate ULID: Generate, validate, and recover ULID values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-30 (10 months ago) RSS 2.0 feedNot enough user ratingsTotal: 62 All time: 10,453 This week: 33Up
Version License PHP version Categories
ulid-php-lib 7.0.0Custom (specified...7.1Cryptography, Text processing, Valida..., P...
Description 

Author

This package can generate, validate, and recover ULID values.

It can perform several operations on ULID (Universally Unique Lexicographically Sortable Identifier) values.

Currently, it can:

- Generate a ULID value from a string or a timestamp

- Validate a ULID string value

- Recover a date, timestamp or randomness value from a ULID value

Innovation Award
PHP Programming Innovation award nominee
November 2023
Number 4
UUID is the acronym for Universally Unique Identifier. A UUID is a string identifier that uniquely identifies objects among a set of things.

There are specifications on how to generate a UUID value. Each version has its restrictions.

A ULID is an evolution of the UUID to generate unique identifiers that provide compatibility with UUID, among other advantages.

This package implements ULID generation, validation, and recovery of details in PHP.

Manuel Lemos
Picture of Leonardo Di Sarli
  Performance   Level  
Name: Leonardo Di Sarli <contact>
Classes: 11 packages by
Country: Brazil Brazil
Age: ???
All time rank: 3841309 in Brazil Brazil
Week rank: 43 Up6 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 5x

Winner: 2x

Example

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use
Ulid\Ulid;


print_r('Generate Ulids');
echo
PHP_EOL;
$ulid = new Ulid();

$ulidFromTime = $ulid->generate(1585083964945);
echo
'ulid from time (1585083964945) = ' . $ulidFromTime . PHP_EOL;

$ulidFromNow = $ulid->generate();
echo
'ulid from now = ' . $ulidFromNow . PHP_EOL;

$invalid = $ulid->isValidFormat('1585083964945');
echo
'invalid format = ' . $invalid . PHP_EOL;

$valid = $ulid->isValidFormat('01E475VQGHA88VWQ4H22GQ3X3T');
echo
'valid format = ' . $valid . PHP_EOL;

$timeFromUlid = $ulid->getTimeFromUlid('01E48SD97BMWHAW82D229T0C7K');
echo
'get time from ulid (01E48SD97BMWHAW82D229T0C7K) = ' . $timeFromUlid . PHP_EOL;

$dateFromUlid = $ulid->getDateFromUlid('01E48SD97BMWHAW82D229T0C7K');
echo
'get date from ulid (01E48SD97BMWHAW82D229T0C7K) = ' . $dateFromUlid . PHP_EOL;

$randomnessFromUlid = $ulid->getRandomnessFromString('01E475VQGHNW990PVHXFDT4C6R');
echo
'get randomness from ulid (01E475VQGHNW990PVHXFDT4C6R) = ' . $randomnessFromUlid . PHP_EOL;


Details

PHP Universally Unique Lexicographically Sortable Identifier (ULID)

Latest Version codecov CI Build Downloads Old Downloads PRs Welcome Packagist License (custom server)

PHP library to create a ULID unique identifier value

Installation

Release 7.0.0 Requires PHP 8.3

Release 6.0.0 Requires PHP 8.2

Release 5.0.0 Requires PHP 8.1

Release 4.0.0 Requires PHP 7.4

Release 3.0.0 Requires PHP 7.3

Release 2.0.0 Requires PHP 7.2

Release 1.0.0 Requires PHP 7.1

The recommended way to install is through Composer.

composer require not-empty/ulid-php-lib

Usage

Generate an Ulid

use Ulid\Ulid;
$ulid = new Ulid();
$ulidFromNow = $ulid->generate();
echo $ulidFromNow;

Generate an Ulid from a timestamp

use Ulid\Ulid;
$ulid = new Ulid();
$ulidFromTime = $ulid->generate(1585083964945);
echo $ulidFromTime;

Validates if is a valid Ulid from a string

use Ulid\Ulid;
$ulid = new Ulid();
$invalid = $ulid->isValidFormat('1585083964945');
var_dump($invalid)

Recover timestamp from Ulid

use Ulid\Ulid;
$ulid = new Ulid();
$timeFromUlid = $ulid->getTimeFromUlid('01E48SD97BMWHAW82D229T0C7K');
echo $timeFromUlid;

Recover date from Ulid

use Ulid\Ulid;
$ulid = new Ulid();
$dateFromUlid = $ulid->getDateFromUlid('01E48SD97BMWHAW82D229T0C7K');
echo $dateFromUlid;

Recover randomness from Ulid

use Ulid\Ulid;
$ulid = new Ulid();
$randomnessFromUlid = $ulid->getRandomnessFromString('01E475VQGHNW990PVHXFDT4C6R');
echo $randomnessFromUlid;

if you want an environment to run or test it, you can build and install dependences like this

docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/ulid-php-lib:php83 -f contrib/Dockerfile .

Access the container

docker run -v ${PWD}/:/var/www/html -it not-empty/ulid-php-lib:php83 bash

Verify if all dependencies is installed

composer install --no-dev --prefer-dist

and run

php sample/ulid-sample.php

Development

Want to contribute? Great!

The project using a simple code. Make a change in your file and be careful with your updates! Any new code will only be accepted with all validations.

To ensure that the entire project is fine:

First you need to building a correct environment to install all dependences

docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/ulid-php-lib:php83 -f contrib/Dockerfile .

Access the container

docker run -v ${PWD}/:/var/www/html -it not-empty/ulid-php-lib:php83 bash

Install all dependences

composer install --dev --prefer-dist

Run all validations

composer check

Not Empty Foundation - Free codes, full minds


  Files folder image Files (16)  
File Role Description
Files folder image.github (1 directory)
Files folder imagecontrib (4 files)
Files folder imagesample (1 file)
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (16)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (16)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files (16)  /  contrib  
File Role Description
  Accessible without login Plain text file coverage-checker.php Example Example script
  Accessible without login Plain text file Dockerfile Data Auxiliary data
  Accessible without login Plain text file pre-commit Data Auxiliary data
  Accessible without login Plain text file setup.sh Data Auxiliary data

  Files folder image Files (16)  /  sample  
File Role Description
  Accessible without login Plain text file ulid-sample.php Example Example script

  Files folder image Files (16)  /  src  
File Role Description
  Plain text file Ulid.php Class Class source
  Plain text file UlidConstants.php Class Class source

  Files folder image Files (16)  /  tests  
File Role Description
  Plain text file UlidTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:62
This week:0
All time:10,453
This week:33Up