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

Skip to content

[Uid] Unify InvalidUuidException and InvalidUlidException #60328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Uid;

use Symfony\Component\Uid\Exception\InvalidArgumentException;
use Symfony\Component\Uid\Exception\InvalidUidException;

/**
* @author Nicolas Grekas <[email protected]>
Expand All @@ -31,41 +31,41 @@ abstract public static function isValid(string $uid): bool;
/**
* Creates an AbstractUid from an identifier represented in any of the supported formats.
*
* @throws InvalidArgumentException When the passed value is not valid
* @throws InvalidUidException When the passed value is not valid
*/
abstract public static function fromString(string $uid): static;

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

return static::fromString($uid);
}

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

return static::fromString($uid);
}

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

return static::fromString($uid);
Expand All @@ -74,12 +74,12 @@ public static function fromBase32(string $uid): static
/**
* @param string $uid A valid RFC 9562/4122 uid
*
* @throws InvalidArgumentException When the passed value is not valid
* @throws InvalidUidException When the passed value is not valid
*/
public static function fromRfc4122(string $uid): static
{
if (36 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
throw new InvalidUidException('Invalid RFC4122 uid provided.');
}

return static::fromString($uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

namespace Symfony\Component\Uid\Exception;

class InvalidUlidException extends InvalidArgumentException
class InvalidUidException extends InvalidArgumentException
{
public function __construct(string $value)
{
parent::__construct(\sprintf('Invalid ULID: "%s".', $value));
}
}
22 changes: 0 additions & 22 deletions src/Symfony/Component/Uid/Exception/InvalidUuidException.php

This file was deleted.

13 changes: 6 additions & 7 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
namespace Symfony\Component\Uid\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Exception\InvalidArgumentException;
use Symfony\Component\Uid\Exception\InvalidUlidException;
use Symfony\Component\Uid\Exception\InvalidUidException;
use Symfony\Component\Uid\MaxUlid;
use Symfony\Component\Uid\NilUlid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
Expand Down Expand Up @@ -43,7 +42,7 @@ public function testGenerate()

public function testWithInvalidUlid()
{
$this->expectException(InvalidUlidException::class);
$this->expectException(InvalidUidException::class);
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');

new Ulid('this is not a ulid');
Expand Down Expand Up @@ -153,7 +152,7 @@ public function testFromBinary()
*/
public function testFromBinaryInvalidFormat(string $ulid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Ulid::fromBinary($ulid);
}
Expand All @@ -180,7 +179,7 @@ public function testFromBase58()
*/
public function testFromBase58InvalidFormat(string $ulid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Ulid::fromBase58($ulid);
}
Expand All @@ -207,7 +206,7 @@ public function testFromBase32()
*/
public function testFromBase32InvalidFormat(string $ulid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Ulid::fromBase32($ulid);
}
Expand All @@ -234,7 +233,7 @@ public function testFromRfc4122()
*/
public function testFromRfc4122InvalidFormat(string $ulid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Ulid::fromRfc4122($ulid);
}
Expand Down
29 changes: 15 additions & 14 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Exception\InvalidArgumentException;
use Symfony\Component\Uid\Exception\InvalidUidException;
use Symfony\Component\Uid\MaxUuid;
use Symfony\Component\Uid\NilUuid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUuid;
Expand All @@ -36,7 +37,7 @@ class UuidTest extends TestCase
*/
public function testConstructorWithInvalidUuid(string $uuid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);
$this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');

Uuid::fromString($uuid);
Expand All @@ -59,7 +60,7 @@ public function testInvalidVariant(string $uuid)
$uuid = (string) $uuid;
$class = Uuid::class.'V'.$uuid[14];

$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);
$this->expectExceptionMessage('Invalid UUIDv'.$uuid[14].': "'.$uuid.'".');

new $class($uuid);
Expand Down Expand Up @@ -380,11 +381,11 @@ public function testFromBinary()
/**
* @dataProvider provideInvalidBinaryFormat
*/
public function testFromBinaryInvalidFormat(string $ulid)
public function testFromBinaryInvalidFormat(string $uuid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Uuid::fromBinary($ulid);
Uuid::fromBinary($uuid);
}

public static function provideInvalidBinaryFormat(): array
Expand All @@ -407,11 +408,11 @@ public function testFromBase58()
/**
* @dataProvider provideInvalidBase58Format
*/
public function testFromBase58InvalidFormat(string $ulid)
public function testFromBase58InvalidFormat(string $uuid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Uuid::fromBase58($ulid);
Uuid::fromBase58($uuid);
}

public static function provideInvalidBase58Format(): array
Expand All @@ -434,11 +435,11 @@ public function testFromBase32()
/**
* @dataProvider provideInvalidBase32Format
*/
public function testFromBase32InvalidFormat(string $ulid)
public function testFromBase32InvalidFormat(string $uuid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Uuid::fromBase32($ulid);
Uuid::fromBase32($uuid);
}

public static function provideInvalidBase32Format(): array
Expand All @@ -461,11 +462,11 @@ public function testFromRfc4122()
/**
* @dataProvider provideInvalidRfc4122Format
*/
public function testFromRfc4122InvalidFormat(string $ulid)
public function testFromRfc4122InvalidFormat(string $uuid)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidUidException::class);

Uuid::fromRfc4122($ulid);
Uuid::fromRfc4122($uuid);
}

public static function provideInvalidRfc4122Format(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Uid;

use Symfony\Component\Uid\Exception\InvalidArgumentException;
use Symfony\Component\Uid\Exception\InvalidUlidException;
use Symfony\Component\Uid\Exception\InvalidUidException;

/**
* A ULID is lexicographically sortable and contains a 48-bit timestamp and 80-bit of crypto-random entropy.
Expand All @@ -39,7 +39,7 @@ public function __construct(?string $ulid = null)
$this->uid = $ulid;
} else {
if (!self::isValid($ulid)) {
throw new InvalidUlidException($ulid);
throw new InvalidUidException(\sprintf('Invalid ULID: "%s".', $ulid));
}

$this->uid = strtoupper($ulid);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Uid;

use Symfony\Component\Uid\Exception\InvalidUuidException;
use Symfony\Component\Uid\Exception\InvalidUidException;

/**
* @author Grégoire Pineau <[email protected]>
Expand Down Expand Up @@ -41,13 +41,13 @@ public function __construct(string $uuid, bool $checkVariant = false)
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;

if (false === $type || (static::TYPE ?: $type) !== $type) {
throw new InvalidUuidException(static::TYPE, $uuid);
throw new InvalidUidException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}

$this->uid = strtolower($uuid);

if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
throw new InvalidUuidException(static::TYPE, $uuid);
throw new InvalidUidException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}
}

Expand Down
Loading