Skip to content

Commit d3fb8d3

Browse files
committed
Add InvalidUidException
1 parent 5add177 commit d3fb8d3

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

src/Symfony/Component/Uid/AbstractUid.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14-
use Symfony\Component\Uid\Exception\InvalidArgumentException;
14+
use Symfony\Component\Uid\Exception\InvalidUidException;
1515

1616
/**
1717
* @author Nicolas Grekas <p@tchwork.com>
@@ -31,41 +31,41 @@ abstract public static function isValid(string $uid): bool;
3131
/**
3232
* Creates an AbstractUid from an identifier represented in any of the supported formats.
3333
*
34-
* @throws InvalidArgumentException When the passed value is not valid
34+
* @throws InvalidUidException When the passed value is not valid
3535
*/
3636
abstract public static function fromString(string $uid): static;
3737

3838
/**
39-
* @throws InvalidArgumentException When the passed value is not valid
39+
* @throws InvalidUidException When the passed value is not valid
4040
*/
4141
public static function fromBinary(string $uid): static
4242
{
4343
if (16 !== \strlen($uid)) {
44-
throw new InvalidArgumentException('Invalid binary uid provided.');
44+
throw new InvalidUidException($uid, 'Invalid binary uid provided.');
4545
}
4646

4747
return static::fromString($uid);
4848
}
4949

5050
/**
51-
* @throws InvalidArgumentException When the passed value is not valid
51+
* @throws InvalidUidException When the passed value is not valid
5252
*/
5353
public static function fromBase58(string $uid): static
5454
{
5555
if (22 !== \strlen($uid)) {
56-
throw new InvalidArgumentException('Invalid base-58 uid provided.');
56+
throw new InvalidUidException($uid, 'Invalid base-58 uid provided.');
5757
}
5858

5959
return static::fromString($uid);
6060
}
6161

6262
/**
63-
* @throws InvalidArgumentException When the passed value is not valid
63+
* @throws InvalidUidException When the passed value is not valid
6464
*/
6565
public static function fromBase32(string $uid): static
6666
{
6767
if (26 !== \strlen($uid)) {
68-
throw new InvalidArgumentException('Invalid base-32 uid provided.');
68+
throw new InvalidUidException($uid, 'Invalid base-32 uid provided.');
6969
}
7070

7171
return static::fromString($uid);
@@ -74,12 +74,12 @@ public static function fromBase32(string $uid): static
7474
/**
7575
* @param string $uid A valid RFC 9562/4122 uid
7676
*
77-
* @throws InvalidArgumentException When the passed value is not valid
77+
* @throws InvalidUidException When the passed value is not valid
7878
*/
7979
public static function fromRfc4122(string $uid): static
8080
{
8181
if (36 !== \strlen($uid)) {
82-
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
82+
throw new InvalidUidException($uid, 'Invalid RFC4122 uid provided.');
8383
}
8484

8585
return static::fromString($uid);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Uid\Exception;
13+
14+
final class InvalidUidException extends InvalidArgumentException
15+
{
16+
public function __construct(
17+
public readonly string $uid,
18+
string $message,
19+
) {
20+
parent::__construct($message);
21+
}
22+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
use Symfony\Component\Uid\Exception\InvalidUidException;
1616
use Symfony\Component\Uid\MaxUlid;
1717
use Symfony\Component\Uid\NilUlid;
1818
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
@@ -42,7 +42,7 @@ public function testGenerate()
4242

4343
public function testWithInvalidUlid()
4444
{
45-
$this->expectException(InvalidArgumentException::class);
45+
$this->expectException(InvalidUidException::class);
4646
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');
4747

4848
new Ulid('this is not a ulid');
@@ -152,7 +152,7 @@ public function testFromBinary()
152152
*/
153153
public function testFromBinaryInvalidFormat(string $ulid)
154154
{
155-
$this->expectException(InvalidArgumentException::class);
155+
$this->expectException(InvalidUidException::class);
156156

157157
Ulid::fromBinary($ulid);
158158
}
@@ -179,7 +179,7 @@ public function testFromBase58()
179179
*/
180180
public function testFromBase58InvalidFormat(string $ulid)
181181
{
182-
$this->expectException(InvalidArgumentException::class);
182+
$this->expectException(InvalidUidException::class);
183183

184184
Ulid::fromBase58($ulid);
185185
}
@@ -206,7 +206,7 @@ public function testFromBase32()
206206
*/
207207
public function testFromBase32InvalidFormat(string $ulid)
208208
{
209-
$this->expectException(InvalidArgumentException::class);
209+
$this->expectException(InvalidUidException::class);
210210

211211
Ulid::fromBase32($ulid);
212212
}
@@ -233,7 +233,7 @@ public function testFromRfc4122()
233233
*/
234234
public function testFromRfc4122InvalidFormat(string $ulid)
235235
{
236-
$this->expectException(InvalidArgumentException::class);
236+
$this->expectException(InvalidUidException::class);
237237

238238
Ulid::fromRfc4122($ulid);
239239
}

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
use Symfony\Component\Uid\Exception\InvalidUidException;
16+
use Symfony\Component\Uid\Exception\LogicException;
1617
use Symfony\Component\Uid\MaxUuid;
1718
use Symfony\Component\Uid\NilUuid;
1819
use Symfony\Component\Uid\Tests\Fixtures\CustomUuid;
@@ -36,7 +37,7 @@ class UuidTest extends TestCase
3637
*/
3738
public function testConstructorWithInvalidUuid(string $uuid)
3839
{
39-
$this->expectException(InvalidArgumentException::class);
40+
$this->expectException(InvalidUidException::class);
4041
$this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');
4142

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

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

6566
new $class($uuid);
@@ -380,11 +381,11 @@ public function testFromBinary()
380381
/**
381382
* @dataProvider provideInvalidBinaryFormat
382383
*/
383-
public function testFromBinaryInvalidFormat(string $ulid)
384+
public function testFromBinaryInvalidFormat(string $uuid)
384385
{
385-
$this->expectException(InvalidArgumentException::class);
386+
$this->expectException(InvalidUidException::class);
386387

387-
Uuid::fromBinary($ulid);
388+
Uuid::fromBinary($uuid);
388389
}
389390

390391
public static function provideInvalidBinaryFormat(): array
@@ -407,11 +408,11 @@ public function testFromBase58()
407408
/**
408409
* @dataProvider provideInvalidBase58Format
409410
*/
410-
public function testFromBase58InvalidFormat(string $ulid)
411+
public function testFromBase58InvalidFormat(string $uuid)
411412
{
412-
$this->expectException(InvalidArgumentException::class);
413+
$this->expectException(InvalidUidException::class);
413414

414-
Uuid::fromBase58($ulid);
415+
Uuid::fromBase58($uuid);
415416
}
416417

417418
public static function provideInvalidBase58Format(): array
@@ -434,11 +435,11 @@ public function testFromBase32()
434435
/**
435436
* @dataProvider provideInvalidBase32Format
436437
*/
437-
public function testFromBase32InvalidFormat(string $ulid)
438+
public function testFromBase32InvalidFormat(string $uuid)
438439
{
439-
$this->expectException(InvalidArgumentException::class);
440+
$this->expectException(InvalidUidException::class);
440441

441-
Uuid::fromBase32($ulid);
442+
Uuid::fromBase32($uuid);
442443
}
443444

444445
public static function provideInvalidBase32Format(): array
@@ -461,11 +462,11 @@ public function testFromRfc4122()
461462
/**
462463
* @dataProvider provideInvalidRfc4122Format
463464
*/
464-
public function testFromRfc4122InvalidFormat(string $ulid)
465+
public function testFromRfc4122InvalidFormat(string $uuid)
465466
{
466-
$this->expectException(InvalidArgumentException::class);
467+
$this->expectException(InvalidUidException::class);
467468

468-
Uuid::fromRfc4122($ulid);
469+
Uuid::fromRfc4122($uuid);
469470
}
470471

471472
public static function provideInvalidRfc4122Format(): array
@@ -510,7 +511,7 @@ public function testV1ToV6()
510511

511512
public function testV1ToV7BeforeUnixEpochThrows()
512513
{
513-
$this->expectException(InvalidArgumentException::class);
514+
$this->expectException(LogicException::class);
514515
$this->expectExceptionMessage('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
515516

516517
(new UuidV1('9aba8000-ff00-11b0-b3db-3b3fc83afdfc'))->toV7(); // Timestamp is 1969-01-01 00:00:00.0000000

src/Symfony/Component/Uid/Ulid.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Uid;
1313

1414
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
use Symfony\Component\Uid\Exception\InvalidUidException;
1516

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

4445
$this->uid = strtoupper($ulid);

src/Symfony/Component/Uid/Uuid.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14-
use Symfony\Component\Uid\Exception\InvalidArgumentException;
14+
use Symfony\Component\Uid\Exception\InvalidUidException;
1515

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

4343
if (false === $type || (static::TYPE ?: $type) !== $type) {
44-
throw new InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
44+
throw new InvalidUidException($uuid, \sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
4545
}
4646

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

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

src/Symfony/Component/Uid/UuidV6.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14-
use Symfony\Component\Uid\Exception\InvalidArgumentException;
14+
use Symfony\Component\Uid\Exception\LogicException;
1515

1616
/**
1717
* A v6 UUID is lexicographically sortable and contains a 60-bit timestamp and 62 extra unique bits.
@@ -50,7 +50,7 @@ public function toV7(): UuidV7
5050
$uuid = $this->uid;
5151
$time = BinaryUtil::hexToNumericString('0'.substr($uuid, 0, 8).substr($uuid, 9, 4).substr($uuid, 15, 3));
5252
if ('-' === $time[0]) {
53-
throw new InvalidArgumentException('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
53+
throw new LogicException('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
5454
}
5555

5656
$ms = \strlen($time) > 4 ? substr($time, 0, -4) : '0';

0 commit comments

Comments
 (0)