Skip to content

Commit

Permalink
feature #39850 [Uid] Add fromBase58(), fromBase32(), fromRfc4122() an…
Browse files Browse the repository at this point in the history
…d fromBinary() methods (fancyweb)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[Uid] Add fromBase58(), fromBase32(), fromRfc4122() and fromBinary() methods

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #39801
| License       | MIT
| Doc PR        | -

Commits
-------

c01ec7d [Uid] Add fromBase58(), fromBase32(), fromRfc4122() and fromBinary()
  • Loading branch information
nicolas-grekas committed Jan 20, 2021
2 parents dc9db1e + c01ec7d commit c5140c2
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,62 @@ abstract public static function isValid(string $uid): bool;
*/
abstract public static function fromString(string $uid): self;

/**
* @return static
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBinary(string $uid): self
{
if (16 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid binary uid provided.');
}

return static::fromString($uid);
}

/**
* @return static
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBase58(string $uid): self
{
if (22 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid base-58 uid provided.');
}

return static::fromString($uid);
}

/**
* @return static
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBase32(string $uid): self
{
if (26 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid base-32 uid provided.');
}

return static::fromString($uid);
}

/**
* @return static
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromRfc4122(string $uid): self
{
if (36 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
}

return static::fromString($uid);
}

/**
* Returns the identifier as a raw binary string.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3
---

* Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()`

5.2.0
-----

Expand Down
92 changes: 92 additions & 0 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,96 @@ public function testCompare()
$this->assertLessThan(0, $b->compare($c));
$this->assertGreaterThan(0, $c->compare($b));
}

public function testFromBinary()
{
$this->assertEquals(
Ulid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
Ulid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
);

foreach ([
'01EW2RYKDCT2SAK454KBR2QG08',
'1BVXue8CnY8ogucrHX3TeF',
'0177058f-4dac-d0b2-a990-a49af02bc008',
] as $ulid) {
try {
Ulid::fromBinary($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromBase58()
{
$this->assertEquals(
Ulid::fromString('1BVXue8CnY8ogucrHX3TeF'),
Ulid::fromBase58('1BVXue8CnY8ogucrHX3TeF')
);

foreach ([
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
'01EW2RYKDCT2SAK454KBR2QG08',
'0177058f-4dac-d0b2-a990-a49af02bc008',
] as $ulid) {
try {
Ulid::fromBase58($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromBase32()
{
$this->assertEquals(
Ulid::fromString('01EW2RYKDCT2SAK454KBR2QG08'),
Ulid::fromBase32('01EW2RYKDCT2SAK454KBR2QG08')
);

foreach ([
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
'1BVXue8CnY8ogucrHX3TeF',
'0177058f-4dac-d0b2-a990-a49af02bc008',
] as $ulid) {
try {
Ulid::fromBase32($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromRfc4122()
{
$this->assertEquals(
Ulid::fromString('0177058f-4dac-d0b2-a990-a49af02bc008'),
Ulid::fromRfc4122('0177058f-4dac-d0b2-a990-a49af02bc008')
);

foreach ([
"\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08",
'01EW2RYKDCT2SAK454KBR2QG08',
'1BVXue8CnY8ogucrHX3TeF',
] as $ulid) {
try {
Ulid::fromRfc4122($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}
}
92 changes: 92 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,96 @@ public function testNilUuid()
$this->assertInstanceOf(NilUuid::class, $uuid);
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
}

public function testFromBinary()
{
$this->assertEquals(
Uuid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
Uuid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
);

foreach ([
'01EW2RYKDCT2SAK454KBR2QG08',
'1BVXue8CnY8ogucrHX3TeF',
'0177058f-4dac-d0b2-a990-a49af02bc008',
] as $ulid) {
try {
Uuid::fromBinary($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromBase58()
{
$this->assertEquals(
UuidV1::fromString('94fSqj9oxGtsNbkfQNntwx'),
UuidV1::fromBase58('94fSqj9oxGtsNbkfQNntwx')
);

foreach ([
"\x41\x4C\x08\x92\x57\x1B\x11\xEB\xBF\x70\x93\xF9\xB0\x82\x2C\x57",
'219G494NRV27NVYW4KZ6R84B2Q',
'414c0892-571b-11eb-bf70-93f9b0822c57',
] as $ulid) {
try {
UuidV1::fromBase58($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromBase32()
{
$this->assertEquals(
UuidV5::fromString('2VN0S74HBDBB0AQRXAHFVG35KK'),
UuidV5::fromBase32('2VN0S74HBDBB0AQRXAHFVG35KK')
);

foreach ([
"\x5B\xA8\x32\x72\x45\x6D\x5A\xC0\xAB\xE3\xAA\x8B\xF7\x01\x96\x73",
'CKTRYycTes6WAqSQJsTDaz',
'5ba83272-456d-5ac0-abe3-aa8bf7019673',
] as $ulid) {
try {
Ulid::fromBase32($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}

public function testFromRfc4122()
{
$this->assertEquals(
UuidV6::fromString('1eb571b4-14c0-6893-bf70-2d4c83cf755a'),
UuidV6::fromRfc4122('1eb571b4-14c0-6893-bf70-2d4c83cf755a')
);

foreach ([
"\x1E\xB5\x71\xB4\x14\xC0\x68\x93\xBF\x70\x2D\x4C\x83\xCF\x75\x5A",
'0YPNRV8560D29VYW1D9J1WYXAT',
'4nwTLZ2TdMtTVDE5AwVjaR',
] as $ulid) {
try {
Ulid::fromRfc4122($ulid);

$this->fail();
} catch (\Throwable $e) {
}

$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}
}

0 comments on commit c5140c2

Please sign in to comment.