PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/phpuuid.php

https://bitbucket.org/djl/phpuuid
PHP | 141 lines | 77 code | 11 blank | 53 comment | 2 complexity | 1ed55fea60cb920ab0b3613745bdefb5 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. * phpUUID - pure PHP UUID implementation
  4. * Copyright (C) 2011 Daniel J. Lauk
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * This file contains the UUID class.
  21. */
  22. /**
  23. * A simple UUID class.
  24. *
  25. * The class is based on the information in RFC 4122. There is one notable
  26. * difference though, which is the 'node' field:
  27. * According to the RFC the node field is 48 bits long (6 octets). We split
  28. * it up into a high (16 bits) and low (32 bits) part, so that 32-bit
  29. * machines can easily deal with it.
  30. *
  31. * @see http://tools.ietf.org/html/rfc4122
  32. */
  33. class UUID {
  34. protected $timeLow;
  35. protected $timeMid;
  36. protected $timeHighAndVersion;
  37. protected $clockSeqHighAndReserved;
  38. protected $clockSeqLow;
  39. protected $nodeHigh;
  40. protected $nodeLow;
  41. /**
  42. * Create a UUID object from a string.
  43. *
  44. * If the string is not in the correct format (e.g.
  45. * '6ba7b810-9dad-11d1-80b4-00c04fd430c8'), an `InvalidArgumentException`
  46. * will be thrown.
  47. *
  48. * @param string $str The UUID string
  49. * @return UUID The UUID object
  50. */
  51. public static function fromString($str) {
  52. $matches = null;
  53. $count = preg_match('/^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{4})([0-9a-f]{8})$/i', $str, $matches);
  54. if ($count === 0) throw new InvalidArgumentException(
  55. 'Not a valid UUID: ' . $str);
  56. $i = 1;
  57. $uuid = new UUID();
  58. $uuid->timeLow = UUID::hexToInt($matches[$i++]);
  59. $uuid->timeMid = UUID::hexToInt($matches[$i++]);
  60. $uuid->timeHighAndVersion = UUID::hexToInt($matches[$i++]);
  61. $uuid->clockSeqHighAndReserved = UUID::hexToInt($matches[$i++]);
  62. $uuid->clockSeqLow = UUID::hexToInt($matches[$i++]);
  63. $uuid->nodeHigh = UUID::hexToInt($matches[$i++]);
  64. $uuid->nodeLow = UUID::hexToInt($matches[$i++]);
  65. return $uuid;
  66. }
  67. private static function hexToInt($str) {
  68. $rc = 0;
  69. foreach (str_split($str) as $c) {
  70. switch ($c) {
  71. case '0': $x = 0; break;
  72. case '1': $x = 1; break;
  73. case '2': $x = 2; break;
  74. case '3': $x = 3; break;
  75. case '4': $x = 4; break;
  76. case '5': $x = 5; break;
  77. case '6': $x = 6; break;
  78. case '7': $x = 7; break;
  79. case '8': $x = 8; break;
  80. case '9': $x = 9; break;
  81. case 'a':
  82. case 'A': $x = 10; break;
  83. case 'b':
  84. case 'B': $x = 11; break;
  85. case 'c':
  86. case 'C': $x = 12; break;
  87. case 'd':
  88. case 'D': $x = 13; break;
  89. case 'e':
  90. case 'E': $x = 14; break;
  91. case 'f':
  92. case 'F': $x = 15; break;
  93. default: throw new IvalidArgumentException(
  94. "Invalid hex string: $str");
  95. }
  96. $rc = $rc << 4 | $x;
  97. }
  98. return $rc;
  99. }
  100. /**
  101. * Create a new UUID based on (pseudo) random numbers.
  102. *
  103. * This will set the version field on the UUID correctly.
  104. *
  105. * @return UUID A newly created (pseudo) random UUID.
  106. */
  107. public static function random() {
  108. $uuid = new UUID();
  109. $uuid->timeLow = mt_rand(0, 0xffff) << 16 | mt_rand(0, 0xffff);
  110. $uuid->timeMid = mt_rand(0, 0xffff);
  111. $uuid->timeHighAndVersion = mt_rand(0, 0xffff) & 0x0fff | 0x4000;
  112. $uuid->clockSeqHighAndReserved = mt_rand(0, 0xff) & 0x3f | 0xbf;
  113. $uuid->clockSeqLow = mt_rand(0, 0xff);
  114. $uuid->nodeHigh = mt_rand(0, 0xffff);
  115. $uuid->nodeLow = mt_rand(0, 0xffff) << 16 | mt_rand(0, 0xffff);
  116. return $uuid;
  117. }
  118. /**
  119. * Create a string representation of this UUID object.
  120. *
  121. * @return string The UUID string.
  122. */
  123. public function toString() {
  124. $str = sprintf('%08x-%04x-%04x-%02x%02x-%04x%08x',
  125. $this->timeLow, $this->timeMid,
  126. $this->timeHighAndVersion, $this->clockSeqHighAndReserved,
  127. $this->clockSeqLow, $this->nodeHigh, $this->nodeLow);
  128. return $str;
  129. }
  130. }