Add generate_uuid() and Check::uuid()

This commit is contained in:
Benjamin Renard 2024-01-30 20:08:41 +01:00
parent 3abd2a328b
commit d0eb50cf22
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 40 additions and 0 deletions

View file

@ -120,6 +120,20 @@ class Check {
}
}
/**
* check if an uuid is correct
*
* @access public
* @param string $value The value to check
* @return bool true or false
*/
public static function uuid($value) {
if (preg_match('/^[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}$/',$value)) {
return true;
}
return false;
}
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab

View file

@ -344,4 +344,30 @@ function implode_with_keys($values, $quoted=true, $separator=', ', $kv_separator
return implode($separator, $result);
}
/**
* Generate UUID
* @return string UUID
*/
function generate_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab