functions.php 4.6 KB

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