Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ public function testIsValid()
$this->assertTrue(UuidV4::isValid(self::A_UUID_V4));
}

public function testIsValidWithNilUuid()
{
$this->assertTrue(Uuid::isValid('00000000-0000-0000-0000-000000000000'));
$this->assertTrue(NilUuid::isValid('00000000-0000-0000-0000-000000000000'));

$this->assertFalse(UuidV1::isValid('00000000-0000-0000-0000-000000000000'));
$this->assertFalse(UuidV4::isValid('00000000-0000-0000-0000-000000000000'));
}

public function testIsValidWithMaxUuid()
{
$this->assertTrue(Uuid::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
$this->assertTrue(Uuid::isValid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'));
$this->assertTrue(Uuid::isValid('fFFFFFFF-ffff-FFFF-FFFF-FFFFffFFFFFF'));

$this->assertFalse(UuidV5::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
$this->assertFalse(UuidV6::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
}

public function testEquals()
{
$uuid1 = new UuidV1(self::A_UUID_V1);
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function fromString(string $uuid): parent
$uuid = substr_replace($uuid, '-', 18, 0);
$uuid = substr_replace($uuid, '-', 23, 0);
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
$ulid = new Ulid('00000000000000000000000000');
$ulid = new NilUlid();
$ulid->uid = strtoupper($uuid);
$uuid = $ulid->toRfc4122();
}
Expand Down Expand Up @@ -117,6 +117,14 @@ final public static function v6(): UuidV6

public static function isValid(string $uuid): bool
{
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
return true;
}

if (__CLASS__ === static::class && 'ffffffff-ffff-ffff-ffff-ffffffffffff' === strtr($uuid, 'F', 'f')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (self::MAX === strtr($uuid, 'F', 'f') && (\in_array(static::class, [__CLASS__, MaxUuid::class], true))) { on 6.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return true;
}

if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
return false;
}
Expand Down