feat: expose UUID helper as a class of static methods

This commit is contained in:
Victor Westerlund 2025-11-02 09:44:43 +01:00
parent 00337cd3aa
commit 51a5cfaaf5
Signed by: vlw
GPG key ID: D0AD730E1057DFC6

View file

@ -2,37 +2,42 @@
namespace vlw\Scaffold\Helpers;
const UUID_LENGTH = 36;
/**
* Generate an all binary 0:s UUID
*
* @return string
* Generate Universally unique identifiers
*/
function uuid_nil(): string {
return "00000000-0000-0000-0000-000000000000";
}
class UUID {
public const LENGTH = 36;
/**
* Generate an all binary 1:s UUID
*
* @return string
*/
function uuid_max(): string {
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
}
/**
* Generate an all binary 0:s UUID
*
* @return string
*/
public static function nil(): string {
return "00000000-0000-0000-0000-000000000000";
}
/**
* Generate a version 4 UUID
*
* @return string
*/
function uuid_v4(): string {
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
/**
* Generate an all binary 1:s UUID
*
* @return string
*/
public static function max(): string {
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
}
/**
* Generate a version 4 UUID
*
* @return string
*/
public static function v4(): string {
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}