diff --git a/src/Symfony/Component/Uid/AbstractUid.php b/src/Symfony/Component/Uid/AbstractUid.php index 54732773df..0dfe99ea9f 100644 --- a/src/Symfony/Component/Uid/AbstractUid.php +++ b/src/Symfony/Component/Uid/AbstractUid.php @@ -37,6 +37,62 @@ abstract class AbstractUid implements \JsonSerializable */ 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. */ diff --git a/src/Symfony/Component/Uid/CHANGELOG.md b/src/Symfony/Component/Uid/CHANGELOG.md index b93119ec20..d81459ce75 100644 --- a/src/Symfony/Component/Uid/CHANGELOG.md +++ b/src/Symfony/Component/Uid/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.3 +--- + + * Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()` + 5.2.0 ----- diff --git a/src/Symfony/Component/Uid/Tests/UlidTest.php b/src/Symfony/Component/Uid/Tests/UlidTest.php index 58df676fe1..f5f993c09f 100644 --- a/src/Symfony/Component/Uid/Tests/UlidTest.php +++ b/src/Symfony/Component/Uid/Tests/UlidTest.php @@ -118,4 +118,96 @@ class UlidTest extends TestCase $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); + } + } } diff --git a/src/Symfony/Component/Uid/Tests/UuidTest.php b/src/Symfony/Component/Uid/Tests/UuidTest.php index 67834fe050..faa96e8b77 100644 --- a/src/Symfony/Component/Uid/Tests/UuidTest.php +++ b/src/Symfony/Component/Uid/Tests/UuidTest.php @@ -193,4 +193,96 @@ class UuidTest extends TestCase $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); + } + } }