UuidFactoryInterface.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Uuid;
  13. use DateTimeInterface;
  14. use Ramsey\Uuid\Type\Hexadecimal;
  15. use Ramsey\Uuid\Type\Integer as IntegerObject;
  16. use Ramsey\Uuid\Validator\ValidatorInterface;
  17. /**
  18. * UuidFactoryInterface defines the common functionality all `UuidFactory` instances must implement
  19. */
  20. interface UuidFactoryInterface
  21. {
  22. /**
  23. * Creates a UUID from a byte string
  24. *
  25. * @param string $bytes A binary string
  26. *
  27. * @return UuidInterface A UuidInterface instance created from a binary string representation
  28. *
  29. * @pure
  30. */
  31. public function fromBytes(string $bytes): UuidInterface;
  32. /**
  33. * Creates a UUID from a DateTimeInterface instance
  34. *
  35. * @param DateTimeInterface $dateTime The date and time
  36. * @param Hexadecimal | null $node A 48-bit number representing the hardware address
  37. * @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set
  38. * backwards in time or if the node ID changes
  39. *
  40. * @return UuidInterface A UuidInterface instance that represents a version 1 UUID created from a DateTimeInterface instance
  41. */
  42. public function fromDateTime(
  43. DateTimeInterface $dateTime,
  44. ?Hexadecimal $node = null,
  45. ?int $clockSeq = null,
  46. ): UuidInterface;
  47. /**
  48. * Creates a UUID from a 128-bit integer string
  49. *
  50. * @param string $integer String representation of 128-bit integer
  51. *
  52. * @return UuidInterface A UuidInterface instance created from the string representation of a 128-bit integer
  53. *
  54. * @pure
  55. */
  56. public function fromInteger(string $integer): UuidInterface;
  57. /**
  58. * Creates a UUID from the string standard representation
  59. *
  60. * @param string $uuid A hexadecimal string
  61. *
  62. * @return UuidInterface A UuidInterface instance created from a hexadecimal string representation
  63. *
  64. * @pure
  65. */
  66. public function fromString(string $uuid): UuidInterface;
  67. /**
  68. * Returns the validator used by the factory
  69. */
  70. public function getValidator(): ValidatorInterface;
  71. /**
  72. * Returns a version 1 (Gregorian time) UUID from a host ID, sequence number, and the current time
  73. *
  74. * @param Hexadecimal | int | string | null $node A 48-bit number representing the hardware address; this number may
  75. * be represented as an integer or a hexadecimal string
  76. * @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set
  77. * backwards in time or if the node ID changes
  78. *
  79. * @return UuidInterface A UuidInterface instance that represents a version 1 UUID
  80. */
  81. public function uuid1($node = null, ?int $clockSeq = null): UuidInterface;
  82. /**
  83. * Returns a version 2 (DCE Security) UUID from a local domain, local identifier, host ID, clock sequence, and the
  84. * current time
  85. *
  86. * @param int $localDomain The local domain to use when generating bytes, according to DCE Security
  87. * @param IntegerObject | null $localIdentifier The local identifier for the given domain; this may be a UID or GID
  88. * on POSIX systems, if the local domain is a person or group, or it may be a site-defined identifier if the
  89. * local domain is org
  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 UuidInterface A UuidInterface instance that represents a version 2 UUID
  95. */
  96. public function uuid2(
  97. int $localDomain,
  98. ?IntegerObject $localIdentifier = null,
  99. ?Hexadecimal $node = null,
  100. ?int $clockSeq = null,
  101. ): UuidInterface;
  102. /**
  103. * Returns a version 3 (name-based) UUID based on the MD5 hash of a namespace ID and a name
  104. *
  105. * @param UuidInterface | string $ns The namespace (must be a valid UUID)
  106. * @param string $name The name to use for creating a UUID
  107. *
  108. * @return UuidInterface A UuidInterface instance that represents a version 3 UUID
  109. *
  110. * @pure
  111. */
  112. public function uuid3($ns, string $name): UuidInterface;
  113. /**
  114. * Returns a version 4 (random) UUID
  115. *
  116. * @return UuidInterface A UuidInterface instance that represents a version 4 UUID
  117. */
  118. public function uuid4(): UuidInterface;
  119. /**
  120. * Returns a version 5 (name-based) UUID based on the SHA-1 hash of a namespace ID and a name
  121. *
  122. * @param UuidInterface | string $ns The namespace (must be a valid UUID)
  123. * @param string $name The name to use for creating a UUID
  124. *
  125. * @return UuidInterface A UuidInterface instance that represents a version 5 UUID
  126. *
  127. * @pure
  128. */
  129. public function uuid5($ns, string $name): UuidInterface;
  130. /**
  131. * Returns a version 6 (reordered Gregorian time) UUID from a host ID, sequence number, and the current time
  132. *
  133. * @param Hexadecimal | null $node A 48-bit number representing the hardware address
  134. * @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set
  135. * backwards in time or if the node ID changes
  136. *
  137. * @return UuidInterface A UuidInterface instance that represents a version 6 UUID
  138. */
  139. public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface;
  140. }