Uuid.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace app\helpers;
  3. use yii\helpers\ArrayHelper;
  4. class Uuid
  5. {
  6. //43a17aae-aa70-4da0-a82e-6c258646a693
  7. //28fc1384-6132-4686-b200-a3c564f652c9
  8. public static function GetUUID():string
  9. {
  10. $bytes = random_bytes(16);
  11. $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
  12. $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
  13. $hex = bin2hex($bytes);
  14. $fields = [
  15. 'time_low' => substr($hex, 0, 8),
  16. 'time_mid' => substr($hex, 8, 4),
  17. 'time_hi_and_version' => substr($hex, 12, 4),
  18. 'clock_seq_hi_and_reserved' => substr($hex, 16, 2),
  19. 'clock_seq_low' => substr($hex, 18, 2),
  20. 'node' => substr($hex, 20, 12),
  21. ];
  22. return vsprintf(
  23. '%08s-%04s-%04s-%02s%02s-%012s',
  24. $fields
  25. );
  26. }
  27. public static function isvalid(string $string):string
  28. {
  29. if( preg_match('/^([a-z0-9]){8}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){12}$/', $string) ){
  30. return true;
  31. }
  32. return false;
  33. }
  34. }