PHP Luminova: Generate and Validate UUIDs
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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$version | int | UUID version, one of 1, 3, 4, 5, and 7 (default: 4). |
$namespace | string\|null | Namespace UUID or alias for v3 or v5 (default: null). |
$name | string\|null | Name within the namespace for v3 or v5 (default: null). |
$time | int\|null | Explicit timestamp for v1 or v7. Pass null to use the current time (default: null). |
Return:
string - Generated UUID string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the version is unsupported, the namespace or name is invalid, or the timestamp format is rejected.
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
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$uuid | string | Candidate UUID. |
$version | int\|null | Expected version. Pass null or 0 to detect it (default: null). |
$namespace | string\|null | Namespace UUID or alias used for v3 or v5 verification (default: null). |
$name | string\|null | Name within the namespace used for v3 or v5 verification (default: null). |
$maxPastAgeMs | int\|null | Maximum accepted age in milliseconds for v1 and v7 (default: null). |
$maxFutureSkewMs | int\|null | Maximum future offset in milliseconds for v1 and v7 (default: null). |
Return:
bool - Whether the selected checks pass.
Throws:
- Luminova\Exceptions\InvalidArgumentException if name-based verification is requested with an invalid namespace or name.
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): ?intParameters:
| Parameter | Type | Description |
|---|---|---|
$uuid | string | Candidate 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$time | int\|null | Explicit Unix timestamp or null for the current time. |
Return:
string - Generated UUID string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the supplied timestamp is negative or its format is rejected.
v4
Generate a UUID from random bytes with version and variant bits set.
public static function v4(): stringReturn:
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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$timeMs | int\|null | Explicit Unix timestamp or null for the current time (default: null). |
Return:
string - Generated UUID string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the supplied timestamp is negative or its format is rejected.
v3
Generate a deterministic name-based UUID using MD5.
public static function v3(?string $namespace, string $name): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$namespace | string\|null | Namespace UUID or alias. |
$name | string | Name within the namespace. |
Return:
string - Generated UUID string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the namespace or name is empty, or the namespace UUID is invalid.
v5
Generate a deterministic name-based UUID using SHA-1.
public static function v5(?string $namespace, string $name): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$namespace | string\|null | Namespace UUID or alias. |
$name | string | Name within the namespace. |
Return:
string - Generated UUID string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the namespace or name is empty, or the namespace UUID is invalid.