8000 [Uid] Add fromBase58(), fromBase32(), fromRfc4122() and fromBinary() · symfony/symfony@3b18f90 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 3b18f90

Browse files
fancywebnicolas-grekas
authored andcommitted
[Uid] Add fromBase58(), fromBase32(), fromRfc4122() and fromBinary()
1 parent 4818b28 commit 3b18f90

File tree

4 files changed

+248
-0
lines changed
  • 4 files changed

    +248
    -0
    lines changed

    src/Symfony/Component/Uid/AbstractUid.php

    Lines changed: 59 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -37,6 +37,65 @@ 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+
    public static function fromBinary(string $uid): self
    46+
    {
    47+
    8000 if (16 !== \strlen($uid)) {
    48+
    throw new \InvalidArgumentException('Invalid binary uid provided.');
    49+
    }
    50+
    51+
    return static::fromString($uid);
    52+
    }
    53+
    54+
    /**
    55+
    * @return static
    56+
    *
    57+
    * @throws \InvalidArgumentException When the passed value is not valid
    58+
    */
    59+
    public static function fromBase58(string $uid): self
    60+
    {
    61+
    if (22 !== \strlen($uid)) {
    62+
    throw new \InvalidArgumentException('Invalid base-58 uid provided.');
    63+
    }
    64+
    65+
    return static::fromString($uid);
    66+
    }
    67+
    68+
    69+
    /**
    70+
    * @return static
    71+
    *
    72+
    * @throws \InvalidArgumentException When the passed value is not valid
    73+
    */
    74+
    public static function fromBase32(string $uid): self
    75+
    {
    76+
    if (26 !== \strlen($uid)) {
    77+
    throw new \InvalidArgumentException('Invalid base-32 uid provided.');
    78+
    }
    79+
    80+
    return static::fromString($uid);
    81+
    }
    82+
    83+
    84+
    /**
    85+
    * @return static
    86+
    *
    87+
    * @throws \InvalidArgumentException When the passed value is not valid
    88+
    */
    89+
    public static function fromRfc4122(string $uid): self
    90+
    {
    91+
    if (36 !== \strlen($uid)) {
    92+
    throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
    93+
    }
    94+
    95+
    return static::fromString($uid);
    96+
    }
    97+
    98+
    4099
    /**
    41100
    * Returns the identifier as a raw binary string.
    42101
    */

    src/Symfony/Component/Uid/CHANGELOG.md

    Lines changed: 5 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,6 +1,11 @@
    11
    CHANGELOG
    22
    =========
    33

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

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

    Lines 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

    Lines 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
    }

    0 commit comments

    Comments
     (0)
    0