AbstractUid.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Uid;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. */
  14. abstract class AbstractUid implements \JsonSerializable, \Stringable
  15. {
  16. /**
  17. * The identifier in its canonic representation.
  18. */
  19. protected $uid;
  20. /**
  21. * Whether the passed value is valid for the constructor of the current class.
  22. */
  23. abstract public static function isValid(string $uid): bool;
  24. /**
  25. * Creates an AbstractUid from an identifier represented in any of the supported formats.
  26. *
  27. * @throws \InvalidArgumentException When the passed value is not valid
  28. */
  29. abstract public static function fromString(string $uid): static;
  30. /**
  31. * @throws \InvalidArgumentException When the passed value is not valid
  32. */
  33. public static function fromBinary(string $uid): static
  34. {
  35. if (16 !== \strlen($uid)) {
  36. throw new \InvalidArgumentException('Invalid binary uid provided.');
  37. }
  38. return static::fromString($uid);
  39. }
  40. /**
  41. * @throws \InvalidArgumentException When the passed value is not valid
  42. */
  43. public static function fromBase58(string $uid): static
  44. {
  45. if (22 !== \strlen($uid)) {
  46. throw new \InvalidArgumentException('Invalid base-58 uid provided.');
  47. }
  48. return static::fromString($uid);
  49. }
  50. /**
  51. * @throws \InvalidArgumentException When the passed value is not valid
  52. */
  53. public static function fromBase32(string $uid): static
  54. {
  55. if (26 !== \strlen($uid)) {
  56. throw new \InvalidArgumentException('Invalid base-32 uid provided.');
  57. }
  58. return static::fromString($uid);
  59. }
  60. /**
  61. * @param string $uid A valid RFC 9562/4122 uid
  62. *
  63. * @throws \InvalidArgumentException When the passed value is not valid
  64. */
  65. public static function fromRfc4122(string $uid): static
  66. {
  67. if (36 !== \strlen($uid)) {
  68. throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
  69. }
  70. return static::fromString($uid);
  71. }
  72. /**
  73. * Returns the identifier as a raw binary string.
  74. */
  75. abstract public function toBinary(): string;
  76. /**
  77. * Returns the identifier as a base58 case sensitive string.
  78. *
  79. * @example 2AifFTC3zXgZzK5fPrrprL (len=22)
  80. */
  81. public function toBase58(): string
  82. {
  83. return strtr(\sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
  84. }
  85. /**
  86. * Returns the identifier as a base32 case insensitive string.
  87. *
  88. * @see https://tools.ietf.org/html/rfc4648#section-6
  89. *
  90. * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
  91. */
  92. public function toBase32(): string
  93. {
  94. $uid = bin2hex($this->toBinary());
  95. $uid = \sprintf('%02s%04s%04s%04s%04s%04s%04s',
  96. base_convert(substr($uid, 0, 2), 16, 32),
  97. base_convert(substr($uid, 2, 5), 16, 32),
  98. base_convert(substr($uid, 7, 5), 16, 32),
  99. base_convert(substr($uid, 12, 5), 16, 32),
  100. base_convert(substr($uid, 17, 5), 16, 32),
  101. base_convert(substr($uid, 22, 5), 16, 32),
  102. base_convert(substr($uid, 27, 5), 16, 32)
  103. );
  104. return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
  105. }
  106. /**
  107. * Returns the identifier as a RFC 9562/4122 case insensitive string.
  108. *
  109. * @see https://datatracker.ietf.org/doc/html/rfc9562/#section-4
  110. *
  111. * @example 09748193-048a-4bfb-b825-8528cf74fdc1 (len=36)
  112. */
  113. public function toRfc4122(): string
  114. {
  115. // don't use uuid_unparse(), it's slower
  116. $uuid = bin2hex($this->toBinary());
  117. $uuid = substr_replace($uuid, '-', 8, 0);
  118. $uuid = substr_replace($uuid, '-', 13, 0);
  119. $uuid = substr_replace($uuid, '-', 18, 0);
  120. return substr_replace($uuid, '-', 23, 0);
  121. }
  122. /**
  123. * Returns the identifier as a prefixed hexadecimal case insensitive string.
  124. *
  125. * @example 0x09748193048a4bfbb8258528cf74fdc1 (len=34)
  126. */
  127. public function toHex(): string
  128. {
  129. return '0x'.bin2hex($this->toBinary());
  130. }
  131. /**
  132. * Returns whether the argument is an AbstractUid and contains the same value as the current instance.
  133. */
  134. public function equals(mixed $other): bool
  135. {
  136. if (!$other instanceof self) {
  137. return false;
  138. }
  139. return $this->uid === $other->uid;
  140. }
  141. public function compare(self $other): int
  142. {
  143. return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
  144. }
  145. public function __toString(): string
  146. {
  147. return $this->uid;
  148. }
  149. public function jsonSerialize(): string
  150. {
  151. return $this->uid;
  152. }
  153. }