| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Uid; |
| 13 |
|
| 14 |
use Symfony\Component\Uid\Exception\InvalidArgumentException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
protected string $uid; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
abstract public static function isValid(string $uid): bool; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
abstract public static function fromString(string $uid): static; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public static function fromBinary(string $uid): static |
| 42 |
{ |
| 43 |
if (16 !== \strlen($uid)) { |
| 44 |
throw new InvalidArgumentException('Invalid binary uid provided.'); |
| 45 |
} |
| 46 |
|
| 47 |
return static::fromString($uid); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static function fromBase58(string $uid): static |
| 54 |
{ |
| 55 |
if (22 !== \strlen($uid)) { |
| 56 |
throw new InvalidArgumentException('Invalid base-58 uid provided.'); |
| 57 |
} |
| 58 |
|
| 59 |
return static::fromString($uid); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static function fromBase32(string $uid): static |
| 66 |
{ |
| 67 |
if (26 !== \strlen($uid)) { |
| 68 |
throw new InvalidArgumentException('Invalid base-32 uid provided.'); |
| 69 |
} |
| 70 |
|
| 71 |
return static::fromString($uid); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static function fromRfc4122(string $uid): static |
| 80 |
{ |
| 81 |
if (36 !== \strlen($uid)) { |
| 82 |
throw new InvalidArgumentException('Invalid RFC4122 uid provided.'); |
| 83 |
} |
| 84 |
|
| 85 |
return static::fromString($uid); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
abstract public function toBinary(): string; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function toBase58(): string |
| 103 |
{ |
| 104 |
return strtr(\sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1'); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function toBase32(): string |
| 117 |
{ |
| 118 |
$uid = bin2hex($this->toBinary()); |
| 119 |
$uid = \sprintf('%02s%04s%04s%04s%04s%04s%04s', |
| 120 |
base_convert(substr($uid, 0, 2), 16, 32), |
| 121 |
base_convert(substr($uid, 2, 5), 16, 32), |
| 122 |
base_convert(substr($uid, 7, 5), 16, 32), |
| 123 |
base_convert(substr($uid, 12, 5), 16, 32), |
| 124 |
base_convert(substr($uid, 17, 5), 16, 32), |
| 125 |
base_convert(substr($uid, 22, 5), 16, 32), |
| 126 |
base_convert(substr($uid, 27, 5), 16, 32) |
| 127 |
); |
| 128 |
|
| 129 |
return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function toRfc4122(): string |
| 142 |
{ |
| 143 |
|
| 144 |
$uuid = bin2hex($this->toBinary()); |
| 145 |
$uuid = substr_replace($uuid, '-', 8, 0); |
| 146 |
$uuid = substr_replace($uuid, '-', 13, 0); |
| 147 |
$uuid = substr_replace($uuid, '-', 18, 0); |
| 148 |
|
| 149 |
return substr_replace($uuid, '-', 23, 0); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function toHex(): string |
| 160 |
{ |
| 161 |
return '0x'.bin2hex($this->toBinary()); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function equals(mixed $other): bool |
| 168 |
{ |
| 169 |
if (!$other instanceof self) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
return $this->uid === $other->uid; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function hash(): string |
| 180 |
{ |
| 181 |
return $this->uid; |
| 182 |
} |
| 183 |
|
| 184 |
public function compare(self $other): int |
| 185 |
{ |
| 186 |
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
final public function toString(): string |
| 193 |
{ |
| 194 |
return $this->__toString(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function __toString(): string |
| 201 |
{ |
| 202 |
return $this->uid; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function jsonSerialize(): string |
| 209 |
{ |
| 210 |
return $this->uid; |
| 211 |
} |
| 212 |
} |