Luminova Framework

PHP Luminova: Generate and Validate UUIDs

Last updated: 2026-11-28 20:57:12

Generate UUID versions 1, 3, 4, 5, and 7. Inspect the version, validate the structure, and optionally check timestamps or name-based values.

Use version 4 for random identifiers, version 7 when the identifier should include creation time, or version 5 when a namespace and name should produce a repeatable value. Version 1 embeds a timestamp. Version 3 uses MD5 for name-based identifiers, and version 5 uses SHA-1.


Usage

use Luminova\Utility\Uuid;

$id = Uuid::v4();
var_dump(Uuid::isValid($id, version: 4)); // bool(true)

$named = Uuid::v5('dns', 'example.com');
var_dump(Uuid::isValid($named, 5, 'dns', 'example.com')); // bool(true)

Class Definition

  • Full namespace: Luminova\Utility\Uuid
  • This class is final.

Constants

UUID_NAMESPACE maps the aliases dns, url, oid, and x500 to namespace UUIDs. The name-based methods accept either an alias or a supported namespace UUID.


Methods

generate

Dispatch to the selected UUID generator. A supplied timestamp is used only by the time-based versions. Version 1 has a custom timestamp caveat described under v1().

public static function generate(
    int $version = 4,
    ?string $namespace = null,
    ?string $name = null,
    ?int $time = null
): string

Parameters:

ParameterTypeDescription
$versionintUUID version, one of 1, 3, 4, 5, and 7 (default: 4).
$namespacestring\|nullNamespace UUID or alias for v3 or v5 (default: null).
$namestring\|nullName within the namespace for v3 or v5 (default: null).
$timeint\|nullExplicit timestamp for v1 or v7. Pass null to use the current time (default: null).

Return:

string - Generated UUID string.

Throws:


isValid

Check UUID structure. When both namespace and name are supplied for v3 or v5, regenerate the expected UUID and compare it. For v1 or v7, the optional age limits apply to the embedded time.

public static function isValid(
    string $uuid,
    ?int $version = null,
    ?string $namespace = null,
    ?string $name = null,
    ?int $maxPastAgeMs = null,
    ?int $maxFutureSkewMs = null
): bool

Parameters:

ParameterTypeDescription
$uuidstringCandidate UUID.
$versionint\|nullExpected version. Pass null or 0 to detect it (default: null).
$namespacestring\|nullNamespace UUID or alias used for v3 or v5 verification (default: null).
$namestring\|nullName within the namespace used for v3 or v5 verification (default: null).
$maxPastAgeMsint\|nullMaximum accepted age in milliseconds for v1 and v7 (default: null).
$maxFutureSkewMsint\|nullMaximum future offset in milliseconds for v1 and v7 (default: null).

Return:

bool - Whether the selected checks pass.

Throws:


version

Read the version nibble after checking length and hyphen positions. Use isValid() to validate the other UUID fields.

public static function version(string $uuid): ?int

Parameters:

ParameterTypeDescription
$uuidstringCandidate UUID.

Return:

int|null - Version nibble as an integer, or null if the layout or nibble is invalid.


v1

Generate a time-based UUID with a random clock sequence and node. Pass null for the current time. The explicit timestamp path currently uses Unix millisecond conversion, which can produce an unexpected embedded v1 time.

public static function v1(?int $time): string

Parameters:

ParameterTypeDescription
$timeint\|nullExplicit Unix timestamp or null for the current time.

Return:

string - Generated UUID string.

Throws:


v4

Generate a UUID from random bytes with version and variant bits set.

public static function v4(): string

Return:

string - Generated UUID string.


v7

Generate a UUID with a Unix millisecond timestamp followed by random bits. The method accepts seconds, milliseconds, or microseconds for explicit timestamps and determines the unit from the digit count.

public static function v7(?int $timeMs = null): string

Parameters:

ParameterTypeDescription
$timeMsint\|nullExplicit Unix timestamp or null for the current time (default: null).

Return:

string - Generated UUID string.

Throws:


v3

Generate a deterministic name-based UUID using MD5.

public static function v3(?string $namespace, string $name): string

Parameters:

ParameterTypeDescription
$namespacestring\|nullNamespace UUID or alias.
$namestringName within the namespace.

Return:

string - Generated UUID string.

Throws:


v5

Generate a deterministic name-based UUID using SHA-1.

public static function v5(?string $namespace, string $name): string

Parameters:

ParameterTypeDescription
$namespacestring\|nullNamespace UUID or alias.
$namestringName within the namespace.

Return:

string - Generated UUID string.

Throws: