diff --git a/README.md b/README.md index 7475cc8..6648af4 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,9 @@ $uuid = service('uuid')->fromString('550e8400e29b41d4a716446655440000'); // From ULID string $ulid = service('uuid')->fromString('01ARZ3NDEKTSV4RRFFQ69G5FAV'); +// From UUID Base32 encoding +$uuid = service('uuid')->fromBase32('01JPRHWQCKFB7V1KMWP7PRRSMG'); + // From unknown format (string or binary) $uuid = service('uuid')->fromValue($unknownValue); ``` diff --git a/src/Uuid.php b/src/Uuid.php index 5962941..6f945f9 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -48,6 +48,11 @@ public function fromValue(string $value): UuidWrapper return new UuidWrapper(SymfonyUuid::fromBinary($value)); } + public function fromBase32(string $uuid): UuidWrapper + { + return new UuidWrapper(SymfonyUuid::fromBase32($uuid)); + } + public function isValid(string $value): bool { // Check if it's a valid ULID diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 893982e..80ad86a 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -8,6 +8,7 @@ use Michalsn\CodeIgniterUuid\Enums\UuidVersion; use Michalsn\CodeIgniterUuid\Exceptions\CodeIgniterUuidException; use Michalsn\CodeIgniterUuid\Uuid; +use Symfony\Component\Uid\Exception\InvalidArgumentException; use Symfony\Component\Uid\Ulid; use Symfony\Component\Uid\UuidV1; use Symfony\Component\Uid\UuidV3; @@ -216,6 +217,33 @@ public function testFromValueWithStringWithoutHyphens() $this->assertSame($uuid->toRfc4122(), $result->toRfc4122()); } + public function testFromBase32() + { + $uuid = $this->uuid->generate('v7'); + + $result = $this->uuid->fromBase32($uuid->toBase32()); + + $this->assertInstanceOf(UuidV7::class, $result->unwrap()); + $this->assertSame($uuid->toRfc4122(), $result->toRfc4122()); + } + + public function testFromBase32WithLowercaseValue() + { + $uuid = $this->uuid->generate('v7'); + + $result = $this->uuid->fromBase32(strtolower($uuid->toBase32())); + + $this->assertInstanceOf(UuidV7::class, $result->unwrap()); + $this->assertSame($uuid->toRfc4122(), $result->toRfc4122()); + } + + public function testFromBase32WithInvalidValue() + { + $this->expectException(InvalidArgumentException::class); + + $this->uuid->fromBase32('not-a-uuid'); + } + public function testGenerateUlid() { $ulid = $this->uuid->generate('ulid');