Uuid.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. /**
  3. * This file is part of the ramsey/uuid library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
  11. * @link https://packagist.org/packages/ramsey/uuid Packagist
  12. * @link https://github.com/ramsey/uuid GitHub
  13. */
  14. namespace Ramsey\Uuid;
  15. use Ramsey\Uuid\Converter\NumberConverterInterface;
  16. use Ramsey\Uuid\Codec\CodecInterface;
  17. use Ramsey\Uuid\Exception\UnsupportedOperationException;
  18. /**
  19. * Represents a universally unique identifier (UUID), according to RFC 4122.
  20. *
  21. * This class provides immutable UUID objects (the Uuid class) and the static
  22. * methods `uuid1()`, `uuid3()`, `uuid4()`, and `uuid5()` for generating version
  23. * 1, 3, 4, and 5 UUIDs as specified in RFC 4122.
  24. *
  25. * If all you want is a unique ID, you should probably call `uuid1()` or `uuid4()`.
  26. * Note that `uuid1()` may compromise privacy since it creates a UUID containing
  27. * the computer’s network address. `uuid4()` creates a random UUID.
  28. *
  29. * @link http://tools.ietf.org/html/rfc4122
  30. * @link http://en.wikipedia.org/wiki/Universally_unique_identifier
  31. * @link http://docs.python.org/3/library/uuid.html
  32. * @link http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html
  33. */
  34. class Uuid implements UuidInterface
  35. {
  36. /**
  37. * When this namespace is specified, the name string is a fully-qualified domain name.
  38. * @link http://tools.ietf.org/html/rfc4122#appendix-C
  39. */
  40. const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  41. /**
  42. * When this namespace is specified, the name string is a URL.
  43. * @link http://tools.ietf.org/html/rfc4122#appendix-C
  44. */
  45. const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  46. /**
  47. * When this namespace is specified, the name string is an ISO OID.
  48. * @link http://tools.ietf.org/html/rfc4122#appendix-C
  49. */
  50. const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
  51. /**
  52. * When this namespace is specified, the name string is an X.500 DN in DER or a text output format.
  53. * @link http://tools.ietf.org/html/rfc4122#appendix-C
  54. */
  55. const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
  56. /**
  57. * The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
  58. * @link http://tools.ietf.org/html/rfc4122#section-4.1.7
  59. */
  60. const NIL = '00000000-0000-0000-0000-000000000000';
  61. /**
  62. * Reserved for NCS compatibility.
  63. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1
  64. */
  65. const RESERVED_NCS = 0;
  66. /**
  67. * Specifies the UUID layout given in RFC 4122.
  68. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1
  69. */
  70. const RFC_4122 = 2;
  71. /**
  72. * Reserved for Microsoft compatibility.
  73. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1
  74. */
  75. const RESERVED_MICROSOFT = 6;
  76. /**
  77. * Reserved for future definition.
  78. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1
  79. */
  80. const RESERVED_FUTURE = 7;
  81. /**
  82. * Regular expression pattern for matching a valid UUID of any variant.
  83. */
  84. const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$';
  85. /**
  86. * Version 1 (time-based) UUID object constant identifier
  87. */
  88. const UUID_TYPE_TIME = 1;
  89. /**
  90. * Version 2 (identifier-based) UUID object constant identifier
  91. */
  92. const UUID_TYPE_IDENTIFIER = 2;
  93. /**
  94. * Version 3 (name-based and hashed with MD5) UUID object constant identifier
  95. */
  96. const UUID_TYPE_HASH_MD5 = 3;
  97. /**
  98. * Version 4 (random) UUID object constant identifier
  99. */
  100. const UUID_TYPE_RANDOM = 4;
  101. /**
  102. * Version 5 (name-based and hashed with SHA1) UUID object constant identifier
  103. */
  104. const UUID_TYPE_HASH_SHA1 = 5;
  105. /**
  106. * The factory to use when creating UUIDs.
  107. * @var UuidFactoryInterface
  108. */
  109. private static $factory = null;
  110. /**
  111. * The codec to use when encoding or decoding UUID strings.
  112. * @var CodecInterface
  113. */
  114. protected $codec;
  115. /**
  116. * The fields that make up this UUID.
  117. *
  118. * This is initialized to the nil value.
  119. *
  120. * @var array
  121. * @see UuidInterface::getFieldsHex()
  122. */
  123. protected $fields = array(
  124. 'time_low' => '00000000',
  125. 'time_mid' => '0000',
  126. 'time_hi_and_version' => '0000',
  127. 'clock_seq_hi_and_reserved' => '00',
  128. 'clock_seq_low' => '00',
  129. 'node' => '000000000000',
  130. );
  131. /**
  132. * The number converter to use for converting hex values to/from integers.
  133. * @var NumberConverterInterface
  134. */
  135. protected $converter;
  136. /**
  137. * Creates a universally unique identifier (UUID) from an array of fields.
  138. *
  139. * Unless you're making advanced use of this library to generate identifiers
  140. * that deviate from RFC 4122, you probably do not want to instantiate a
  141. * UUID directly. Use the static methods, instead:
  142. *
  143. * ```
  144. * use Ramsey\Uuid\Uuid;
  145. *
  146. * $timeBasedUuid = Uuid::uuid1();
  147. * $namespaceMd5Uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://php.net/');
  148. * $randomUuid = Uuid::uuid4();
  149. * $namespaceSha1Uuid = Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://php.net/');
  150. * ```
  151. *
  152. * @param array $fields An array of fields from which to construct a UUID;
  153. * see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure.
  154. * @param NumberConverterInterface $converter The number converter to use
  155. * for converting hex values to/from integers.
  156. * @param CodecInterface $codec The codec to use when encoding or decoding
  157. * UUID strings.
  158. */
  159. public function __construct(
  160. array $fields,
  161. NumberConverterInterface $converter,
  162. CodecInterface $codec
  163. ) {
  164. $this->fields = $fields;
  165. $this->codec = $codec;
  166. $this->converter = $converter;
  167. }
  168. /**
  169. * Converts this UUID object to a string when the object is used in any
  170. * string context.
  171. *
  172. * @return string
  173. * @link http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
  174. */
  175. public function __toString()
  176. {
  177. return $this->toString();
  178. }
  179. /**
  180. * Converts this UUID object to a string when the object is serialized
  181. * with `json_encode()`
  182. *
  183. * @return string
  184. * @link http://php.net/manual/en/class.jsonserializable.php
  185. */
  186. public function jsonSerialize()
  187. {
  188. return $this->toString();
  189. }
  190. /**
  191. * Converts this UUID object to a string when the object is serialized
  192. * with `serialize()`
  193. *
  194. * @return string
  195. * @link http://php.net/manual/en/class.serializable.php
  196. */
  197. public function serialize()
  198. {
  199. return $this->toString();
  200. }
  201. /**
  202. * Re-constructs the object from its serialized form.
  203. *
  204. * @param string $serialized
  205. * @link http://php.net/manual/en/class.serializable.php
  206. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  207. */
  208. public function unserialize($serialized)
  209. {
  210. $uuid = self::fromString($serialized);
  211. $this->codec = $uuid->codec;
  212. $this->converter = $uuid->converter;
  213. $this->fields = $uuid->fields;
  214. }
  215. public function compareTo(UuidInterface $other)
  216. {
  217. $comparison = 0;
  218. if ($this->getMostSignificantBitsHex() < $other->getMostSignificantBitsHex()) {
  219. $comparison = -1;
  220. } elseif ($this->getMostSignificantBitsHex() > $other->getMostSignificantBitsHex()) {
  221. $comparison = 1;
  222. } elseif ($this->getLeastSignificantBitsHex() < $other->getLeastSignificantBitsHex()) {
  223. $comparison = -1;
  224. } elseif ($this->getLeastSignificantBitsHex() > $other->getLeastSignificantBitsHex()) {
  225. $comparison = 1;
  226. }
  227. return $comparison;
  228. }
  229. public function equals($other)
  230. {
  231. if (!($other instanceof UuidInterface)) {
  232. return false;
  233. }
  234. return ($this->compareTo($other) == 0);
  235. }
  236. public function getBytes()
  237. {
  238. return $this->codec->encodeBinary($this);
  239. }
  240. /**
  241. * Returns the high field of the clock sequence multiplexed with the variant
  242. * (bits 65-72 of the UUID).
  243. *
  244. * @return int Unsigned 8-bit integer value of clock_seq_hi_and_reserved
  245. */
  246. public function getClockSeqHiAndReserved()
  247. {
  248. return hexdec($this->getClockSeqHiAndReservedHex());
  249. }
  250. public function getClockSeqHiAndReservedHex()
  251. {
  252. return $this->fields['clock_seq_hi_and_reserved'];
  253. }
  254. /**
  255. * Returns the low field of the clock sequence (bits 73-80 of the UUID).
  256. *
  257. * @return int Unsigned 8-bit integer value of clock_seq_low
  258. */
  259. public function getClockSeqLow()
  260. {
  261. return hexdec($this->getClockSeqLowHex());
  262. }
  263. public function getClockSeqLowHex()
  264. {
  265. return $this->fields['clock_seq_low'];
  266. }
  267. /**
  268. * Returns the clock sequence value associated with this UUID.
  269. *
  270. * For UUID version 1, the clock sequence is used to help avoid
  271. * duplicates that could arise when the clock is set backwards in time
  272. * or if the node ID changes.
  273. *
  274. * For UUID version 3 or 5, the clock sequence is a 14-bit value
  275. * constructed from a name as described in RFC 4122, Section 4.3.
  276. *
  277. * For UUID version 4, clock sequence is a randomly or pseudo-randomly
  278. * generated 14-bit value as described in RFC 4122, Section 4.4.
  279. *
  280. * @return int Unsigned 14-bit integer value of clock sequence
  281. * @link http://tools.ietf.org/html/rfc4122#section-4.1.5
  282. */
  283. public function getClockSequence()
  284. {
  285. return (($this->getClockSeqHiAndReserved() & 0x3f) << 8)
  286. | $this->getClockSeqLow();
  287. }
  288. public function getClockSequenceHex()
  289. {
  290. return sprintf('%04x', $this->getClockSequence());
  291. }
  292. public function getNumberConverter()
  293. {
  294. return $this->converter;
  295. }
  296. /**
  297. * @inheritdoc
  298. */
  299. public function getDateTime()
  300. {
  301. if ($this->getVersion() != 1) {
  302. throw new UnsupportedOperationException('Not a time-based UUID');
  303. }
  304. $unixTime = ($this->getTimestamp() - 0x01b21dd213814000) / 1e7;
  305. $unixTime = number_format($unixTime, 0, '', '');
  306. return new \DateTime("@{$unixTime}");
  307. }
  308. /**
  309. * Returns an array of the fields of this UUID, with keys named according
  310. * to the RFC 4122 names for the fields.
  311. *
  312. * * **time_low**: The low field of the timestamp, an unsigned 32-bit integer
  313. * * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer
  314. * * **time_hi_and_version**: The high field of the timestamp multiplexed with
  315. * the version number, an unsigned 16-bit integer
  316. * * **clock_seq_hi_and_reserved**: The high field of the clock sequence
  317. * multiplexed with the variant, an unsigned 8-bit integer
  318. * * **clock_seq_low**: The low field of the clock sequence, an unsigned
  319. * 8-bit integer
  320. * * **node**: The spatially unique node identifier, an unsigned 48-bit
  321. * integer
  322. *
  323. * @return array The UUID fields represented as integer values
  324. * @link http://tools.ietf.org/html/rfc4122#section-4.1.2
  325. */
  326. public function getFields()
  327. {
  328. return array(
  329. 'time_low' => $this->getTimeLow(),
  330. 'time_mid' => $this->getTimeMid(),
  331. 'time_hi_and_version' => $this->getTimeHiAndVersion(),
  332. 'clock_seq_hi_and_reserved' => $this->getClockSeqHiAndReserved(),
  333. 'clock_seq_low' => $this->getClockSeqLow(),
  334. 'node' => $this->getNode(),
  335. );
  336. }
  337. public function getFieldsHex()
  338. {
  339. return $this->fields;
  340. }
  341. public function getHex()
  342. {
  343. return str_replace('-', '', $this->toString());
  344. }
  345. /**
  346. * @inheritdoc
  347. */
  348. public function getInteger()
  349. {
  350. return $this->converter->fromHex($this->getHex());
  351. }
  352. /**
  353. * Returns the least significant 64 bits of this UUID's 128 bit value.
  354. *
  355. * @return mixed Converted representation of the unsigned 64-bit integer value
  356. * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
  357. */
  358. public function getLeastSignificantBits()
  359. {
  360. return $this->converter->fromHex($this->getLeastSignificantBitsHex());
  361. }
  362. public function getLeastSignificantBitsHex()
  363. {
  364. return sprintf(
  365. '%02s%02s%012s',
  366. $this->fields['clock_seq_hi_and_reserved'],
  367. $this->fields['clock_seq_low'],
  368. $this->fields['node']
  369. );
  370. }
  371. /**
  372. * Returns the most significant 64 bits of this UUID's 128 bit value.
  373. *
  374. * @return mixed Converted representation of the unsigned 64-bit integer value
  375. * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
  376. */
  377. public function getMostSignificantBits()
  378. {
  379. return $this->converter->fromHex($this->getMostSignificantBitsHex());
  380. }
  381. public function getMostSignificantBitsHex()
  382. {
  383. return sprintf(
  384. '%08s%04s%04s',
  385. $this->fields['time_low'],
  386. $this->fields['time_mid'],
  387. $this->fields['time_hi_and_version']
  388. );
  389. }
  390. /**
  391. * Returns the node value associated with this UUID
  392. *
  393. * For UUID version 1, the node field consists of an IEEE 802 MAC
  394. * address, usually the host address. For systems with multiple IEEE
  395. * 802 addresses, any available one can be used. The lowest addressed
  396. * octet (octet number 10) contains the global/local bit and the
  397. * unicast/multicast bit, and is the first octet of the address
  398. * transmitted on an 802.3 LAN.
  399. *
  400. * For systems with no IEEE address, a randomly or pseudo-randomly
  401. * generated value may be used; see RFC 4122, Section 4.5. The
  402. * multicast bit must be set in such addresses, in order that they
  403. * will never conflict with addresses obtained from network cards.
  404. *
  405. * For UUID version 3 or 5, the node field is a 48-bit value constructed
  406. * from a name as described in RFC 4122, Section 4.3.
  407. *
  408. * For UUID version 4, the node field is a randomly or pseudo-randomly
  409. * generated 48-bit value as described in RFC 4122, Section 4.4.
  410. *
  411. * @return int Unsigned 48-bit integer value of node
  412. * @link http://tools.ietf.org/html/rfc4122#section-4.1.6
  413. */
  414. public function getNode()
  415. {
  416. return hexdec($this->getNodeHex());
  417. }
  418. public function getNodeHex()
  419. {
  420. return $this->fields['node'];
  421. }
  422. /**
  423. * Returns the high field of the timestamp multiplexed with the version
  424. * number (bits 49-64 of the UUID).
  425. *
  426. * @return int Unsigned 16-bit integer value of time_hi_and_version
  427. */
  428. public function getTimeHiAndVersion()
  429. {
  430. return hexdec($this->getTimeHiAndVersionHex());
  431. }
  432. public function getTimeHiAndVersionHex()
  433. {
  434. return $this->fields['time_hi_and_version'];
  435. }
  436. /**
  437. * Returns the low field of the timestamp (the first 32 bits of the UUID).
  438. *
  439. * @return int Unsigned 32-bit integer value of time_low
  440. */
  441. public function getTimeLow()
  442. {
  443. return hexdec($this->getTimeLowHex());
  444. }
  445. public function getTimeLowHex()
  446. {
  447. return $this->fields['time_low'];
  448. }
  449. /**
  450. * Returns the middle field of the timestamp (bits 33-48 of the UUID).
  451. *
  452. * @return int Unsigned 16-bit integer value of time_mid
  453. */
  454. public function getTimeMid()
  455. {
  456. return hexdec($this->getTimeMidHex());
  457. }
  458. public function getTimeMidHex()
  459. {
  460. return $this->fields['time_mid'];
  461. }
  462. /**
  463. * Returns the timestamp value associated with this UUID.
  464. *
  465. * The 60 bit timestamp value is constructed from the time_low,
  466. * time_mid, and time_hi fields of this UUID. The resulting
  467. * timestamp is measured in 100-nanosecond units since midnight,
  468. * October 15, 1582 UTC.
  469. *
  470. * The timestamp value is only meaningful in a time-based UUID, which
  471. * has version type 1. If this UUID is not a time-based UUID then
  472. * this method throws UnsupportedOperationException.
  473. *
  474. * @return int Unsigned 60-bit integer value of the timestamp
  475. * @throws UnsupportedOperationException If this UUID is not a version 1 UUID
  476. * @link http://tools.ietf.org/html/rfc4122#section-4.1.4
  477. */
  478. public function getTimestamp()
  479. {
  480. if ($this->getVersion() != 1) {
  481. throw new UnsupportedOperationException('Not a time-based UUID');
  482. }
  483. return hexdec($this->getTimestampHex());
  484. }
  485. /**
  486. * @inheritdoc
  487. */
  488. public function getTimestampHex()
  489. {
  490. if ($this->getVersion() != 1) {
  491. throw new UnsupportedOperationException('Not a time-based UUID');
  492. }
  493. return sprintf(
  494. '%03x%04s%08s',
  495. ($this->getTimeHiAndVersion() & 0x0fff),
  496. $this->fields['time_mid'],
  497. $this->fields['time_low']
  498. );
  499. }
  500. public function getUrn()
  501. {
  502. return 'urn:uuid:' . $this->toString();
  503. }
  504. public function getVariant()
  505. {
  506. $clockSeq = $this->getClockSeqHiAndReserved();
  507. if (0 === ($clockSeq & 0x80)) {
  508. $variant = self::RESERVED_NCS;
  509. } elseif (0 === ($clockSeq & 0x40)) {
  510. $variant = self::RFC_4122;
  511. } elseif (0 === ($clockSeq & 0x20)) {
  512. $variant = self::RESERVED_MICROSOFT;
  513. } else {
  514. $variant = self::RESERVED_FUTURE;
  515. }
  516. return $variant;
  517. }
  518. public function getVersion()
  519. {
  520. if ($this->getVariant() == self::RFC_4122) {
  521. return (int) (($this->getTimeHiAndVersion() >> 12) & 0x0f);
  522. }
  523. return null;
  524. }
  525. public function toString()
  526. {
  527. return $this->codec->encode($this);
  528. }
  529. /**
  530. * Returns the currently set factory used to create UUIDs.
  531. *
  532. * @return UuidFactoryInterface
  533. */
  534. public static function getFactory()
  535. {
  536. if (!self::$factory) {
  537. self::$factory = new UuidFactory();
  538. }
  539. return self::$factory;
  540. }
  541. /**
  542. * Sets the factory used to create UUIDs.
  543. *
  544. * @param UuidFactoryInterface $factory
  545. */
  546. public static function setFactory(UuidFactoryInterface $factory)
  547. {
  548. self::$factory = $factory;
  549. }
  550. /**
  551. * Creates a UUID from a byte string.
  552. *
  553. * @param string $bytes
  554. * @return UuidInterface
  555. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  556. * @throws \InvalidArgumentException
  557. */
  558. public static function fromBytes($bytes)
  559. {
  560. return self::getFactory()->fromBytes($bytes);
  561. }
  562. /**
  563. * Creates a UUID from the string standard representation.
  564. *
  565. * @param string $name A string that specifies a UUID
  566. * @return UuidInterface
  567. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  568. */
  569. public static function fromString($name)
  570. {
  571. return self::getFactory()->fromString($name);
  572. }
  573. /**
  574. * Creates a UUID from a 128-bit integer string.
  575. *
  576. * @param string $integer String representation of 128-bit integer
  577. * @return UuidInterface
  578. * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
  579. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  580. */
  581. public static function fromInteger($integer)
  582. {
  583. return self::getFactory()->fromInteger($integer);
  584. }
  585. /**
  586. * Check if a string is a valid UUID.
  587. *
  588. * @param string $uuid The string UUID to test
  589. * @return boolean
  590. */
  591. public static function isValid($uuid)
  592. {
  593. $uuid = str_replace(array('urn:', 'uuid:', '{', '}'), '', $uuid);
  594. if ($uuid == self::NIL) {
  595. return true;
  596. }
  597. if (!preg_match('/' . self::VALID_PATTERN . '/D', $uuid)) {
  598. return false;
  599. }
  600. return true;
  601. }
  602. /**
  603. * Generate a version 1 UUID from a host ID, sequence number, and the current time.
  604. *
  605. * @param int|string $node A 48-bit number representing the hardware address
  606. * This number may be represented as an integer or a hexadecimal string.
  607. * @param int $clockSeq A 14-bit number used to help avoid duplicates that
  608. * could arise when the clock is set backwards in time or if the node ID
  609. * changes.
  610. * @return UuidInterface
  611. * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if called on a 32-bit system and
  612. * `Moontoast\Math\BigNumber` is not present
  613. * @throws \InvalidArgumentException
  614. * @throws \Exception if it was not possible to gather sufficient entropy
  615. */
  616. public static function uuid1($node = null, $clockSeq = null)
  617. {
  618. return self::getFactory()->uuid1($node, $clockSeq);
  619. }
  620. /**
  621. * Generate a version 3 UUID based on the MD5 hash of a namespace identifier
  622. * (which is a UUID) and a name (which is a string).
  623. *
  624. * @param string $ns The UUID namespace in which to create the named UUID
  625. * @param string $name The name to create a UUID for
  626. * @return UuidInterface
  627. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  628. */
  629. public static function uuid3($ns, $name)
  630. {
  631. return self::getFactory()->uuid3($ns, $name);
  632. }
  633. /**
  634. * Generate a version 4 (random) UUID.
  635. *
  636. * @return UuidInterface
  637. * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
  638. * @throws \InvalidArgumentException
  639. * @throws \Exception
  640. */
  641. public static function uuid4()
  642. {
  643. return self::getFactory()->uuid4();
  644. }
  645. /**
  646. * Generate a version 5 UUID based on the SHA-1 hash of a namespace
  647. * identifier (which is a UUID) and a name (which is a string).
  648. *
  649. * @param string $ns The UUID namespace in which to create the named UUID
  650. * @param string $name The name to create a UUID for
  651. * @return UuidInterface
  652. * @throws \Ramsey\Uuid\Exception\InvalidUuidStringException
  653. */
  654. public static function uuid5($ns, $name)
  655. {
  656. return self::getFactory()->uuid5($ns, $name);
  657. }
  658. }