/library/Cream/Guid.php

https://github.com/WebTricks/WebTricks-CMS · PHP · 208 lines · 103 code · 14 blank · 91 comment · 41 complexity · e08580ce4cebee77a466c4f8690a6812 MD5 · raw file

  1. <?php
  2. /**
  3. * WebTricks - PHP Framework
  4. *
  5. * LICENSE
  6. *
  7. * For the full copyright and license information, please view the
  8. * following URL: http://www.webtricksframework.com/license
  9. *
  10. * DISCLAIMER
  11. *
  12. * Do not edit or add to this file if you wish to upgrade to newer versions in
  13. * the future. If you wish to customize WebTricks for your needs please go to
  14. * http://www.webtricksframework.com for more information.
  15. *
  16. * @copyright Copyright (c) 2007-2010 Cream (http://www.cream.nl)
  17. * @license http://www.webtricksframework.com/license
  18. */
  19. /**
  20. * Class representation of the globally unique identifier or GUID, a
  21. * special type of identifier used in software applications in order
  22. * to provide a reference number which is unique in any context.
  23. *
  24. * @package Cream
  25. * @author Danny Verkade
  26. */
  27. class Cream_Guid extends Cream_ApplicationComponent
  28. {
  29. /**
  30. * @var array
  31. */
  32. protected $_bytes;
  33. protected $_string;
  34. /**
  35. * @var Cream_Guid
  36. */
  37. protected static $_emptyGuid;
  38. /**
  39. * Create an instance of this class
  40. *
  41. * @param array $bytes
  42. * @return Cream_Guid
  43. */
  44. public static function instance($bytes)
  45. {
  46. return Cream::instance(__CLASS__, $bytes);
  47. }
  48. /**
  49. * Constructs a new Guid instance given its underlying value as a byte
  50. * array.
  51. *
  52. * @param array $bytes a 16 element byte array.
  53. */
  54. function __init($bytes)
  55. {
  56. if (!is_array($bytes) || count($bytes) != 16) {
  57. throw new Cream_Exceptions_Exception("The argument must be a 16 element byte array");
  58. }
  59. for ($i = 0; $i < 16; $i++) {
  60. $b = $bytes[$i];
  61. if ((string)(int)$b !== (string)$b || $b < 0 || $b > 255) {
  62. throw new Cream_Exceptions_Exception("Value other than a byte at offset {$i}");
  63. }
  64. }
  65. $this->_bytes = $bytes;
  66. }
  67. /**
  68. * Returns a Guid object that has all its bits set to zero.
  69. *
  70. * @return Cream_Guid a nil Guid.
  71. * @static
  72. */
  73. public static function emptyGuid()
  74. {
  75. //if (!self::_emptyGuid) {
  76. // self::_emptyGuid = Cream_Guid::instance(array_pad(array(), 16, 0));
  77. //}
  78. return Cream_Guid::instance(array_pad(array(), 16, 0));
  79. }
  80. /**
  81. * Returns a new, pseudo-randomly generated Guid object.
  82. *
  83. * @return Cream_Guid a new Guid object.
  84. */
  85. public static function generateGuid()
  86. {
  87. $bytes = array();
  88. for ($i = 0; $i < 16; $i++) {
  89. if ($i == 6) { // Version field (version 4)
  90. $b = mt_rand(0, 15) | 64;
  91. } else if ($i == 8) { // Variant field (type 2)
  92. $b = mt_rand(0, 63) | 128;
  93. } else {
  94. $b = mt_rand(0, 255);
  95. }
  96. $bytes[] = $b;
  97. }
  98. return Cream_Guid::instance($bytes);
  99. }
  100. /**
  101. * Parses a Guid object from the specified 32 character hexadecimal
  102. * string. Returns null if the string could not be parsed.
  103. *
  104. * @param string $str the string representation of a Guid.
  105. * @return Cream_Guid a Guid object parsed from its string representation.
  106. */
  107. public static function parseGuid($str)
  108. {
  109. $guid = null;
  110. $str = str_replace(array('{', '(', '-', ')', '}'), '', $str);
  111. if (strlen($str) == 32) {
  112. $bytes = array();
  113. for ($i = 1; $i <= 32; $i++) {
  114. $ch = $str{$i - 1};
  115. if (($ch < '0' || $ch > '9')
  116. && ($ch < 'a' || $ch > 'f')
  117. && ($ch < 'A' || $ch > 'F')) {
  118. break;
  119. }
  120. $n = hexdec($ch);
  121. if ($i % 2 != 0) {
  122. $b = $n;
  123. } else {
  124. $bytes[] = $b * 16 + $n;
  125. }
  126. }
  127. if (count($bytes) == 16) {
  128. $guid = Cream_Guid::instance($bytes);
  129. }
  130. }
  131. return $guid;
  132. }
  133. /**
  134. * Checks if the given string is a valid GUID. If it is, returns
  135. * true, otherwise false
  136. *
  137. * @param string $guid
  138. * @return boolean
  139. */
  140. public static function isGuid($guid)
  141. {
  142. if (self::parseGuid($guid)) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. /**
  149. * Returns a string representation of this Guid object.
  150. *
  151. * @return string a string in the format
  152. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where "x" is a
  153. * hexadecimal digit.
  154. */
  155. public function toString()
  156. {
  157. if (!$this->_string) {
  158. $str = '';
  159. for ($i = 1; $i <= 16; $i++) {
  160. $b = $this->_bytes[$i - 1];
  161. if ($b < 16) {
  162. $str .= '0';
  163. }
  164. $str .= dechex($b);
  165. if ($i == 4 || $i == 6 || $i == 8 || $i == 10) {
  166. $str .= '-';
  167. }
  168. }
  169. $this->_string = $str;
  170. }
  171. return $this->_string;
  172. }
  173. /**
  174. * Returns a string representation of this Guid object.
  175. *
  176. * @return string a string in the format
  177. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where "x" is a
  178. * hexadecimal digit.
  179. */
  180. public function __toString()
  181. {
  182. return $this->toString();
  183. }
  184. /**
  185. * Returns a 16 element byte array containing the underlying value of
  186. * this Guid object.
  187. *
  188. * @return array
  189. */
  190. public function toByteArray()
  191. {
  192. return $this->bytes;
  193. }
  194. }