mpyw / uuid-ulid-converter
UUID <=> ULID bidirectional converter
Installs: 158 056
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
Requires (Dev)
This package is auto-updated.
Last update: 2024-11-21 19:52:23 UTC
README
UUID <=> ULID bidirectional converter
Installing
composer require mpyw/uuid-ulid-converter
API
public static Converter::uuidToUlid(string $uuid, bool $lowercase = false): string public static Converter::ulidToUuid(string $ulid, bool $uppercase = false): string
Important
UUID is lowercase by default, whereas ULID is uppercase by default.
Usage
Basic
use Mpyw\UuidUlidConverter\Converter; var_dump(Converter::ulidToUuid('61862H2EWP9TCTRX3MJ15XNY7X')); // string(36) "c1418511-3b96-4e99-ac74-74904bdaf8fd" var_dump(Converter::uuidToUlid('c1418511-3b96-4e99-ac74-74904bdaf8fd')); // string(26) "61862H2EWP9TCTRX3MJ15XNY7X"
Advanced
The following workarounds are particularly useful in the PostgreSQL, which does not support ULID but does support UUID. Storing ULID as native UUID is more efficient than storing original ULID as strings.
Introduce advanced usages with robinvdvleuten/php-ulid.
Generate UUID-styled ULID
use Mpyw\UuidUlidConverter\Converter; use Ulid\Ulid; // Use generated UUID as primary key $uuid = Converter::ulidToUuid((string)Ulid::generate());
Timestamp range of UUID-styled ULID
use Mpyw\UuidUlidConverter\Converter; use Ulid\Ulid; $dates = [ new \DateTimeImmutable('2020-01-01 00:00:00.000 UTC'), new \DateTimeImmutable('2020-01-02 00:00:00.000 UTC'), ]; function createUuidRange(array $dates): array { $createPart = fn (\DateTimeInterface $date, bool $isEnd) => Converter::ulidToUuid( Ulid::fromTimestamp(round((int)$date->format('Uu') / 1000))->getTime() . str_repeat($isEnd ? 'Z' : '0', 16), ); return [ $createPart($dates[0], false), $createPart($dates[1], true), ]; } $uuids = createUuidRange($dates); /* array(2) { [0]=> string(36) "016f5e66-e800-0000-0000-000000000000" [1]=> string(36) "016f638d-4400-ffff-ffff-ffffffffffff" } */