functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * phpcs:disable Squiz.Functions.GlobalFunction
  11. */
  12. declare(strict_types=1);
  13. namespace Ramsey\Uuid;
  14. use DateTimeInterface;
  15. use Ramsey\Uuid\Type\Hexadecimal;
  16. use Ramsey\Uuid\Type\Integer as IntegerObject;
  17. /**
  18. * Returns a version 1 (Gregorian time) UUID from a host ID, sequence number,
  19. * and the current time
  20. *
  21. * @param Hexadecimal|int|string|null $node A 48-bit number representing the
  22. * hardware address; this number may be represented as an integer or a
  23. * hexadecimal string
  24. * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
  25. * could arise when the clock is set backwards in time or if the node ID
  26. * changes
  27. *
  28. * @return non-empty-string Version 1 UUID as a string
  29. */
  30. function v1($node = null, ?int $clockSeq = null): string
  31. {
  32. return Uuid::uuid1($node, $clockSeq)->toString();
  33. }
  34. /**
  35. * Returns a version 2 (DCE Security) UUID from a local domain, local
  36. * identifier, host ID, clock sequence, and the current time
  37. *
  38. * @param int $localDomain The local domain to use when generating bytes,
  39. * according to DCE Security
  40. * @param IntegerObject|null $localIdentifier The local identifier for the
  41. * given domain; this may be a UID or GID on POSIX systems, if the local
  42. * domain is person or group, or it may be a site-defined identifier
  43. * if the local domain is org
  44. * @param Hexadecimal|null $node A 48-bit number representing the hardware
  45. * address
  46. * @param int|null $clockSeq A 14-bit number used to help avoid duplicates
  47. * that could arise when the clock is set backwards in time or if the
  48. * node ID changes
  49. *
  50. * @return non-empty-string Version 2 UUID as a string
  51. */
  52. function v2(
  53. int $localDomain,
  54. ?IntegerObject $localIdentifier = null,
  55. ?Hexadecimal $node = null,
  56. ?int $clockSeq = null
  57. ): string {
  58. return Uuid::uuid2($localDomain, $localIdentifier, $node, $clockSeq)->toString();
  59. }
  60. /**
  61. * Returns a version 3 (name-based) UUID based on the MD5 hash of a
  62. * namespace ID and a name
  63. *
  64. * @param string|UuidInterface $ns The namespace (must be a valid UUID)
  65. *
  66. * @return non-empty-string Version 3 UUID as a string
  67. *
  68. * @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
  69. * but under constant factory setups, this method operates in functionally pure manners
  70. */
  71. function v3($ns, string $name): string
  72. {
  73. return Uuid::uuid3($ns, $name)->toString();
  74. }
  75. /**
  76. * Returns a version 4 (random) UUID
  77. *
  78. * @return non-empty-string Version 4 UUID as a string
  79. */
  80. function v4(): string
  81. {
  82. return Uuid::uuid4()->toString();
  83. }
  84. /**
  85. * Returns a version 5 (name-based) UUID based on the SHA-1 hash of a
  86. * namespace ID and a name
  87. *
  88. * @param string|UuidInterface $ns The namespace (must be a valid UUID)
  89. *
  90. * @return non-empty-string Version 5 UUID as a string
  91. *
  92. * @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
  93. * but under constant factory setups, this method operates in functionally pure manners
  94. */
  95. function v5($ns, string $name): string
  96. {
  97. return Uuid::uuid5($ns, $name)->toString();
  98. }
  99. /**
  100. * Returns a version 6 (reordered time) UUID from a host ID, sequence number,
  101. * and the current time
  102. *
  103. * @param Hexadecimal|null $node A 48-bit number representing the hardware
  104. * address
  105. * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
  106. * could arise when the clock is set backwards in time or if the node ID
  107. * changes
  108. *
  109. * @return non-empty-string Version 6 UUID as a string
  110. */
  111. function v6(?Hexadecimal $node = null, ?int $clockSeq = null): string
  112. {
  113. return Uuid::uuid6($node, $clockSeq)->toString();
  114. }
  115. /**
  116. * Returns a version 7 (Unix Epoch time) UUID
  117. *
  118. * @param DateTimeInterface|null $dateTime An optional date/time from which
  119. * to create the version 7 UUID. If not provided, the UUID is generated
  120. * using the current date/time.
  121. *
  122. * @return non-empty-string Version 7 UUID as a string
  123. */
  124. function v7(?DateTimeInterface $dateTime = null): string
  125. {
  126. return Uuid::uuid7($dateTime)->toString();
  127. }
  128. /**
  129. * Returns a version 8 (custom) UUID
  130. *
  131. * The bytes provided may contain any value according to your application's
  132. * needs. Be aware, however, that other applications may not understand the
  133. * semantics of the value.
  134. *
  135. * @param string $bytes A 16-byte octet string. This is an open blob
  136. * of data that you may fill with 128 bits of information. Be aware,
  137. * however, bits 48 through 51 will be replaced with the UUID version
  138. * field, and bits 64 and 65 will be replaced with the UUID variant. You
  139. * MUST NOT rely on these bits for your application needs.
  140. *
  141. * @return non-empty-string Version 8 UUID as a string
  142. */
  143. function v8(string $bytes): string
  144. {
  145. return Uuid::uuid8($bytes)->toString();
  146. }