Debian Codegraph @

lib/3rdParty/composer/symfony/uid/AbstractUid.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Uid;
13
14 use Symfony\Component\Uid\Exception\InvalidArgumentException;
15
16 /**
17 * @author Nicolas Grekas <[email protected]>
18 */
19 abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
20 {
21 /**
22 * The identifier in its canonic representation.
23 */
24 protected string $uid;
25
26 /**
27 * Whether the passed value is valid for the constructor of the current class.
28 */
29 abstract public static function isValid(string $uid): bool;
30
31 /**
32 * Creates an AbstractUid from an identifier represented in any of the supported formats.
33 *
34 * @throws InvalidArgumentException When the passed value is not valid
35 */
36 abstract public static function fromString(string $uid): static;
37
38 /**
39 * @throws InvalidArgumentException When the passed value is not valid
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 * @throws InvalidArgumentException When the passed value is not valid
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 * @throws InvalidArgumentException When the passed value is not valid
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 * @param string $uid A valid RFC 9562/4122 uid
76 *
77 * @throws InvalidArgumentException When the passed value is not valid
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 * Returns the identifier as a raw binary string.
90 *
91 * @return non-empty-string
92 */
93 abstract public function toBinary(): string;
94
95 /**
96 * Returns the identifier as a base58 case-sensitive string.
97 *
98 * @example 2AifFTC3zXgZzK5fPrrprL (len=22)
99 *
100 * @return non-empty-string
101 */
102 public function toBase58(): string
103 {
104 return strtr(\sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
105 }
106
107 /**
108 * Returns the identifier as a base32 case-insensitive string.
109 *
110 * @see https://tools.ietf.org/html/rfc4648#section-6
111 *
112 * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
113 *
114 * @return non-empty-string
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 * Returns the identifier as a RFC 9562/4122 case-insensitive string.
134 *
135 * @see https://datatracker.ietf.org/doc/html/rfc9562/#section-4
136 *
137 * @example 09748193-048a-4bfb-b825-8528cf74fdc1 (len=36)
138 *
139 * @return non-empty-string
140 */
141 public function toRfc4122(): string
142 {
143 // don't use uuid_unparse(), it's slower
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 * Returns the identifier as a prefixed hexadecimal case insensitive string.
154 *
155 * @example 0x09748193048a4bfbb8258528cf74fdc1 (len=34)
156 *
157 * @return non-empty-string
158 */
159 public function toHex(): string
160 {
161 return '0x'.bin2hex($this->toBinary());
162 }
163
164 /**
165 * Returns whether the argument is an AbstractUid and contains the same value as the current instance.
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 * @return non-empty-string
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 * @return non-empty-string
191 */
192 final public function toString(): string
193 {
194 return $this->__toString();
195 }
196
197 /**
198 * @return non-empty-string
199 */
200 public function __toString(): string
201 {
202 return $this->uid;
203 }
204
205 /**
206 * @return non-empty-string
207 */
208 public function jsonSerialize(): string
209 {
210 return $this->uid;
211 }
212 }