<?php
// ApiUuid.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Lib;use Exception;class ApiUuid{public static function uuid4(?string $data=null):string{if(!empty($data)&&strlen($data)>=16){$data[6]=chr(ord($data[6])& 0x0f|0x40);$data[8]=chr(ord($data[8])& 0x3f|0x80);return vsprintf('%s%s-%s-%s-%s-%s%s%s',str_split(bin2hex($data),4));}if(function_exists('com_create_guid')===true){return trim(com_create_guid(),'{}');}if(function_exists('openssl_random_pseudo_bytes')===true){$data=openssl_random_pseudo_bytes(16);$data[6]=chr(ord($data[6])& 0x0f|0x40);$data[8]=chr(ord($data[8])& 0x3f|0x80);return vsprintf('%s%s-%s-%s-%s-%s%s%s',str_split(bin2hex($data),4));}mt_srand((double)microtime()*10000);return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',random_int(0,0xffff),random_int(0,0xffff),random_int(0,0xffff),random_int(0,0x0fff)|0x4000,random_int(0,0x3fff)|0x8000,random_int(0,0xffff),random_int(0,0xffff),random_int(0,0xffff));}public static function toString(string|null $hex):string|null{$result=$hex;if(!empty($hex)){$parts=str_split($hex,4);if(count($parts)===8){$result=strtolower(vsprintf('%s%s-%s-%s-%s-%s%s%s',$parts));}}return $result;}public static function toHex(string $string):string{return strtoupper(str_replace('-','',$string));}}