Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6753afb

Browse filesBrowse files
committed
[Uid] Add fromBase58(), fromBase32(), fromRfc4122() and fromBinary() methods
1 parent f1df709 commit 6753afb
Copy full SHA for 6753afb

File tree

6 files changed

+333
-16
lines changed
Filter options

6 files changed

+333
-16
lines changed

‎src/Symfony/Component/Uid/AbstractUid.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/AbstractUid.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ abstract public static function isValid(string $uid): bool;
3737
*/
3838
abstract public static function fromString(string $uid): self;
3939

40+
/**
41+
* @return static
42+
*
43+
* @throws \InvalidArgumentException When the passed value is not valid
44+
*/
45+
abstract public static function fromBinary(string $uid): self;
46+
47+
/**
48+
* @return static
49+
*
50+
* @throws \InvalidArgumentException When the passed value is not valid
51+
*/
52+
abstract public static function fromBase58(string $uid): self;
53+
54+
/**
55+
* @return static
56+
*
57+
* @throws \InvalidArgumentException When the passed value is not valid
58+
*/
59+
abstract public static function fromBase32(string $uid): self;
60+
61+
/**
62+
* @return static
63+
*
64+
* @throws \InvalidArgumentException When the passed value is not valid
65+
*/
66+
abstract public static function fromRfc4122(string $uid): self;
67+
4068
/**
4169
* Returns the identifier as a raw binary string.
4270
*/

‎src/Symfony/Component/Uid/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* added `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()` methods
8+
49
5.2.0
510
-----
611

‎src/Symfony/Component/Uid/Tests/UlidTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UlidTest.php
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,96 @@ public function testCompare()
118118
$this->assertLessThan(0, $b->compare($c));
119119
$this->assertGreaterThan(0, $c->compare($b));
120120
}
121+
122+
public function testFromBinary()
123+
{
124+
$this->assertEquals(
125+
Ulid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
126+
Ulid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
127+
);
128+
129+
foreach ([
130+
'01EW2RYKDCT2SAK454KBR2QG08',
131+
'1BVXue8CnY8ogucrHX3TeF',
132+
'0177058f-4dac-d0b2-a990-a49af02bc008',
133+
] as $ulid) {
134+
try {
135+
Ulid::fromBinary($ulid);
136+
137+
$this->fail();
138+
} catch (\Throwable $e) {
139+
}
140+
141+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
142+
}
143+
}
144+
145+
public function testFromBase58()
146+
{
147+
$this->assertEquals(
148+
Ulid::fromString('1BVXue8CnY8ogucrHX3TeF'),
149+
Ulid::fromBase58('1BVXue8CnY8ogucrHX3TeF')
150+
);
151+
152+
foreach ([
153+
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
154+
'01EW2RYKDCT2SAK454KBR2QG08',
155+
'0177058f-4dac-d0b2-a990-a49af02bc008',
156+
] as $ulid) {
157+
try {
158+
Ulid::fromBase58($ulid);
159+
160+
$this->fail();
161+
} catch (\Throwable $e) {
162+
}
163+
164+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
165+
}
166+
}
167+
168+
public function testFromBase32()
169+
{
170+
$this->assertEquals(
171+
Ulid::fromString('01EW2RYKDCT2SAK454KBR2QG08'),
172+
Ulid::fromBase32('01EW2RYKDCT2SAK454KBR2QG08')
173+
);
174+
175+
foreach ([
176+
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
177+
'1BVXue8CnY8ogucrHX3TeF',
178+
'0177058f-4dac-d0b2-a990-a49af02bc008',
179+
] as $ulid) {
180+
try {
181+
Ulid::fromBase32($ulid);
182+
183+
$this->fail();
184+
} catch (\Throwable $e) {
185+
}
186+
187+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
188+
}
189+
}
190+
191+
public function testFromRfc4122()
192+
{
193+
$this->assertEquals(
194+
Ulid::fromString('0177058f-4dac-d0b2-a990-a49af02bc008'),
195+
Ulid::fromRfc4122('0177058f-4dac-d0b2-a990-a49af02bc008')
196+
);
197+
198+
foreach ([
199+
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
200+
'01EW2RYKDCT2SAK454KBR2QG08',
201+
'1BVXue8CnY8ogucrHX3TeF',
202+
] as $ulid) {
203+
try {
204+
Ulid::fromRfc4122($ulid);
205+
206+
$this->fail();
207+
} catch (\Throwable $e) {
208+
}
209+
210+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
211+
}
212+
}
121213
}

‎src/Symfony/Component/Uid/Tests/UuidTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UuidTest.php
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,96 @@ public function testNilUuid()
193193
$this->assertInstanceOf(NilUuid::class, $uuid);
194194
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
195195
}
196+
197+
public function testFromBinary()
198+
{
199+
$this->assertEquals(
200+
Uuid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
201+
Uuid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
202+
);
203+
204+
foreach ([
205+
'01EW2RYKDCT2SAK454KBR2QG08',
206+
'1BVXue8CnY8ogucrHX3TeF',
207+
'0177058f-4dac-d0b2-a990-a49af02bc008',
208+
] as $ulid) {
209+
try {
210+
Uuid::fromBinary($ulid);
211+
212+
$this->fail();
213+
} catch (\Throwable $e) {
214+
}
215+
216+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
217+
}
218+
}
219+
220+
public function testFromBase58()
221+
{
222+
$this->assertEquals(
223+
UuidV1::fromString('94fSqj9oxGtsNbkfQNntwx'),
224+
UuidV1::fromBase58('94fSqj9oxGtsNbkfQNntwx')
225+
);
226+
227+
foreach ([
228+
"\x41\x4C\x08\x92\x57\x1B\x11\xEB\xBF\x70\x93\xF9\xB0\x82\x2C\x57",
229+
'219G494NRV27NVYW4KZ6R84B2Q',
230+
'414c0892-571b-11eb-bf70-93f9b0822c57',
231+
] as $ulid) {
232+
try {
233+
UuidV1::fromBase58($ulid);
234+
235+
$this->fail();
236+
} catch (\Throwable $e) {
237+
}
238+
239+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
240+
}
241+
}
242+
243+
public function testFromBase32()
244+
{
245+
$this->assertEquals(
246+
UuidV5::fromString('2VN0S74HBDBB0AQRXAHFVG35KK'),
247+
UuidV5::fromBase32('2VN0S74HBDBB0AQRXAHFVG35KK')
248+
);
249+
250+
foreach ([
251+
"\x5B\xA8\x32\x72\x45\x6D\x5A\xC0\xAB\xE3\xAA\x8B\xF7\x01\x96\x73",
252+
'CKTRYycTes6WAqSQJsTDaz',
253+
'5ba83272-456d-5ac0-abe3-aa8bf7019673',
254+
] as $ulid) {
255+
try {
256+
Ulid::fromBase32($ulid);
257+
258+
$this->fail();
259+
} catch (\Throwable $e) {
260+
}
261+
262+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
263+
}
264+
}
265+
266+
public function testFromRfc4122()
267+
{
268+
$this->assertEquals(
269+
UuidV6::fromString('1eb571b4-14c0-6893-bf70-2d4c83cf755a'),
270+
UuidV6::fromRfc4122('1eb571b4-14c0-6893-bf70-2d4c83cf755a')
271+
);
272+
273+
foreach ([
274+
"\x1E\xB5\x71\xB4\x14\xC0\x68\x93\xBF\x70\x2D\x4C\x83\xCF\x75\x5A",
275+
'0YPNRV8560D29VYW1D9J1WYXAT',
276+
'4nwTLZ2TdMtTVDE5AwVjaR',
277+
] as $ulid) {
278+
try {
279+
Ulid::fromRfc4122($ulid);
280+
281+
$this->fail();
282+
} catch (\Throwable $e) {
283+
}
284+
285+
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
286+
}
287+
}
196288
}

‎src/Symfony/Component/Uid/Ulid.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Ulid.php
+54-5Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,31 @@ public static function isValid(string $ulid): bool
5858
*/
5959
public static function fromString(string $ulid): parent
6060
{
61-
if (36 === \strlen($ulid) && Uuid::isValid($ulid)) {
62-
$ulid = (new Uuid($ulid))->toBinary();
63-
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
64-
$ulid = BinaryUtil::fromBase($ulid, BinaryUtil::BASE58);
61+
try {
62+
return static::fromRfc4122($ulid);
63+
} catch (\InvalidArgumentException $e) {
6564
}
6665

66+
try {
67+
return static::fromBase58($ulid);
68+
} catch (\InvalidArgumentException $e) {
69+
}
70+
71+
try {
72+
return static::fromBinary($ulid);
73+
} catch (\InvalidArgumentException $e) {
74+
}
75+
76+
return static::fromBase32($ulid);
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public static function fromBinary(string $ulid): AbstractUid
83+
{
6784
if (16 !== \strlen($ulid)) {
68-
return new static($ulid);
85+
throw new \InvalidArgumentException();
6986
}
7087

7188
$ulid = bin2hex($ulid);
@@ -82,6 +99,38 @@ public static function fromString(string $ulid): parent
8299
return new self(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
83100
}
84101

102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public static function fromBase58(string $ulid): AbstractUid
106+
{
107+
if (22 !== \strlen($ulid) || 22 !== strspn($ulid, BinaryUtil::BASE58[''])) {
108+
throw new \InvalidArgumentException();
109+
}
110+
111+
return static::fromBinary(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58));
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public static function fromBase32(string $ulid): AbstractUid
118+
{
119+
return new static($ulid);
120+
}
121+
122+
/**
123+
* {@inheritdoc}
124+
*/
125+
public static function fromRfc4122(string $ulid): AbstractUid
126+
{
127+
if (36 !== \strlen($ulid) || !Uuid::isValid($ulid)) {
128+
throw new \InvalidArgumentException();
129+
}
130+
131+
return static::fromBinary((new Uuid($ulid))->toBinary());
132+
}
133+
85134
public function toBinary(): string
86135
{
87136
$ulid = strtr($this->uid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.