PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Widget/Uuid.php

https://github.com/putersham/widget
PHP | 43 lines | 15 code | 2 blank | 26 comment | 0 complexity | 78c77df605d569aa97b0e5e93dee3b62 MD5 | raw file
  1. <?php
  2. /**
  3. * Widget Framework
  4. *
  5. * @copyright Copyright (c) 2008-2013 Twin Huang
  6. * @license http://opensource.org/licenses/mit-license.php MIT License
  7. */
  8. namespace Widget;
  9. /**
  10. * A util widget that generates a RANDOM UUID(universally unique identifier)
  11. *
  12. * @author Twin Huang <twinhuang@qq.com>
  13. */
  14. class Uuid extends AbstractWidget
  15. {
  16. /**
  17. * Generate a RANDOM UUID(universally unique identifier)
  18. *
  19. * @link http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
  20. * @link http://php.net/manual/en/function.uniqid.php
  21. * @return string
  22. */
  23. public function __invoke()
  24. {
  25. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  26. // 32 bits for "time_low"
  27. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  28. // 16 bits for "time_mid"
  29. mt_rand(0, 0xffff),
  30. // 16 bits for "time_hi_and_version",
  31. // four most significant bits holds version number 4
  32. mt_rand(0, 0x0fff) | 0x4000,
  33. // 16 bits, 8 bits for "clk_seq_hi_res",
  34. // 8 bits for "clk_seq_low",
  35. // two most significant bits holds zero and one for variant DCE1.1
  36. mt_rand(0, 0x3fff) | 0x8000,
  37. // 48 bits for "node"
  38. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  39. );
  40. }
  41. }