Uuid.php 22 KB

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