/ietf/src/main/php/org/ietf/UUID.class.php

https://github.com/Gamepay/xp-contrib · PHP · 143 lines · 73 code · 9 blank · 61 comment · 2 complexity · d0f791b118594460268091ec61e61d55 MD5 · raw file

  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. define('UUID_FMTSTRING', '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x');
  7. /**
  8. * Encapsulates UUIDs (Universally Unique IDentifiers), also known as
  9. * GUIDs (Globally Unique IDentifiers).
  10. *
  11. * <quote>
  12. * A UUID is an identifier that is unique across both space and time,
  13. * with respect to the space of all UUIDs. To be precise, the UUID
  14. * consists of a finite bit space. Thus the time value used for
  15. * constructing a UUID is limited and will roll over in the future
  16. * (approximately at A.D. 3400, based on the specified algorithm).
  17. * </quote>
  18. *
  19. * Example [creating a new UUID]:
  20. * <code>
  21. * $uuid= UUID::create();
  22. * var_dump($uuid->toString());
  23. * </code>
  24. *
  25. * Example [get a UUID from a string]:
  26. * <code>
  27. * try {
  28. * $uuid= UUID::fromString($uuidstr);
  29. * } catch (FormatException $e) {
  30. * $e->printStackTrace();
  31. * exit();
  32. * }
  33. * var_dump($uuid->toString());
  34. * </code>
  35. *
  36. * @purpose Generate UUIDs
  37. * @see http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-00.txt
  38. */
  39. class UUID extends Object {
  40. public
  41. $time_low = 0,
  42. $time_mid = 0,
  43. $time_hi_and_version = 0,
  44. $clock_seq_low = 0,
  45. $clock_seq_hi_and_reserved = 0,
  46. $node = array();
  47. /**
  48. * Create a new UUID
  49. *
  50. * @return org.ietf.UUID
  51. * @see http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-00.txt section 4.1.4
  52. */
  53. public static function create() {
  54. // Get timestamp and convert it to UTC (based Oct 15, 1582).
  55. list($usec, $sec) = explode(' ', microtime());
  56. $t= ($sec * 10000000) + ($usec * 10) + 122192928000000000;
  57. $clock_seq= mt_rand();
  58. $uuid= new UUID();
  59. $uuid->time_low= ($t & 0xFFFFFFFF);
  60. $uuid->time_mid= (($t >> 32) & 0xFFFF);
  61. $uuid->time_hi_and_version= (($t >> 48) & 0x0FFF);
  62. $uuid->time_hi_and_version |= (1 << 12);
  63. $uuid->clock_seq_low= $clock_seq & 0xFF;
  64. $uuid->clock_seq_hi_and_reserved= ($clock_seq & 0x3F00) >> 8;
  65. $uuid->clock_seq_hi_and_reserved |= 0x80;
  66. $h= md5(php_uname());
  67. $uuid->node= array(
  68. hexdec(substr($h, 0x0, 2)),
  69. hexdec(substr($h, 0x2, 2)),
  70. hexdec(substr($h, 0x4, 2)),
  71. hexdec(substr($h, 0x6, 2)),
  72. hexdec(substr($h, 0x8, 2)),
  73. hexdec(substr($h, 0xB, 2))
  74. );
  75. return $uuid;
  76. }
  77. /**
  78. * Create a UUID from a string
  79. *
  80. * @param string str
  81. * @return org.ietf.UUID
  82. * @throws lang.FormatException in case str is not a valid UUID string
  83. */
  84. public static function fromString($str) {
  85. $uuid= new UUID();
  86. $a= array();
  87. if (11 != sscanf(
  88. $str,
  89. UUID_FMTSTRING,
  90. $uuid->time_low,
  91. $uuid->time_mid,
  92. $uuid->time_hi_and_version,
  93. $uuid->clock_seq_low,
  94. $uuid->clock_seq_hi_and_reserved,
  95. $uuid->node[0],
  96. $uuid->node[1],
  97. $uuid->node[2],
  98. $uuid->node[3],
  99. $uuid->node[4],
  100. $uuid->node[5]
  101. )) {
  102. throw new FormatException($str.' is not a valid UUID string');
  103. }
  104. return $uuid;
  105. }
  106. /**
  107. * Creates a string representation.
  108. *
  109. * Examples:
  110. * <pre>
  111. * f81d4fae-7dec-11d0-a765-00a0c91e6bf6
  112. * c71a4a80-4a80-171a-8fb7-000401000800
  113. * </pre>
  114. *
  115. * @return string
  116. */
  117. public function toString() {
  118. return sprintf(
  119. UUID_FMTSTRING,
  120. $this->time_low,
  121. $this->time_mid,
  122. $this->time_hi_and_version,
  123. $this->clock_seq_hi_and_reserved,
  124. $this->clock_seq_low,
  125. $this->node[0],
  126. $this->node[1],
  127. $this->node[2],
  128. $this->node[3],
  129. $this->node[4],
  130. $this->node[5]
  131. );
  132. }
  133. }
  134. ?>