-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAbstractUid.php
More file actions
212 lines (184 loc) · 5.52 KB
/
AbstractUid.php
File metadata and controls
212 lines (184 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
use Symfony\Component\Uid\Exception\InvalidArgumentException;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
{
/**
* The identifier in its canonic representation.
*/
protected string $uid;
/**
* Whether the passed value is valid for the constructor of the current class.
*/
abstract public static function isValid(string $uid): bool;
/**
* Creates an AbstractUid from an identifier represented in any of the supported formats.
*
* @throws InvalidArgumentException When the passed value is not valid
*/
abstract public static function fromString(string $uid): static;
/**
* @throws InvalidArgumentException When the passed value is not valid
*/
public static function fromBinary(string $uid): static
{
if (16 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid binary uid provided.');
}
return static::fromString($uid);
}
/**
* @throws InvalidArgumentException When the passed value is not valid
*/
public static function fromBase58(string $uid): static
{
if (22 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid base-58 uid provided.');
}
return static::fromString($uid);
}
/**
* @throws InvalidArgumentException When the passed value is not valid
*/
public static function fromBase32(string $uid): static
{
if (26 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid base-32 uid provided.');
}
return static::fromString($uid);
}
/**
* @param string $uid A valid RFC 9562/4122 uid
*
* @throws InvalidArgumentException When the passed value is not valid
*/
public static function fromRfc4122(string $uid): static
{
if (36 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
}
return static::fromString($uid);
}
/**
* Returns the identifier as a raw binary string.
*
* @return non-empty-string
*/
abstract public function toBinary(): string;
/**
* Returns the identifier as a base58 case-sensitive string.
*
* @example 2AifFTC3zXgZzK5fPrrprL (len=22)
*
* @return non-empty-string
*/
public function toBase58(): string
{
return strtr(\sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
}
/**
* Returns the identifier as a base32 case-insensitive string.
*
* @see https://tools.ietf.org/html/rfc4648#section-6
*
* @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
*
* @return non-empty-string
*/
public function toBase32(): string
{
$uid = bin2hex($this->toBinary());
$uid = \sprintf('%02s%04s%04s%04s%04s%04s%04s',
base_convert(substr($uid, 0, 2), 16, 32),
base_convert(substr($uid, 2, 5), 16, 32),
base_convert(substr($uid, 7, 5), 16, 32),
base_convert(substr($uid, 12, 5), 16, 32),
base_convert(substr($uid, 17, 5), 16, 32),
base_convert(substr($uid, 22, 5), 16, 32),
base_convert(substr($uid, 27, 5), 16, 32)
);
return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
}
/**
* Returns the identifier as a RFC 9562/4122 case-insensitive string.
*
* @see https://datatracker.ietf.org/doc/html/rfc9562/#section-4
*
* @example 09748193-048a-4bfb-b825-8528cf74fdc1 (len=36)
*
* @return non-empty-string
*/
public function toRfc4122(): string
{
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($this->toBinary());
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
/**
* Returns the identifier as a prefixed hexadecimal case insensitive string.
*
* @example 0x09748193048a4bfbb8258528cf74fdc1 (len=34)
*
* @return non-empty-string
*/
public function toHex(): string
{
return '0x'.bin2hex($this->toBinary());
}
/**
* Returns whether the argument is an AbstractUid and contains the same value as the current instance.
*/
public function equals(mixed $other): bool
{
if (!$other instanceof self) {
return false;
}
return $this->uid === $other->uid;
}
/**
* @return non-empty-string
*/
public function hash(): string
{
return $this->uid;
}
public function compare(self $other): int
{
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
}
/**
* @return non-empty-string
*/
final public function toString(): string
{
return $this->__toString();
}
/**
* @return non-empty-string
*/
public function __toString(): string
{
return $this->uid;
}
/**
* @return non-empty-string
*/
public function jsonSerialize(): string
{
return $this->uid;
}
}