Skip to content

Commit e86ffe3

Browse files
rela589nnicolas-grekas
authored andcommitted
[Uid] Add component-specific exception classes
1 parent 1088f53 commit e86ffe3

17 files changed

+135
-33
lines changed

src/Symfony/Component/Uid/AbstractUid.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @author Nicolas Grekas <p@tchwork.com>
1618
*/
@@ -29,41 +31,41 @@ abstract public static function isValid(string $uid): bool;
2931
/**
3032
* Creates an AbstractUid from an identifier represented in any of the supported formats.
3133
*
32-
* @throws \InvalidArgumentException When the passed value is not valid
34+
* @throws InvalidArgumentException When the passed value is not valid
3335
*/
3436
abstract public static function fromString(string $uid): static;
3537

3638
/**
37-
* @throws \InvalidArgumentException When the passed value is not valid
39+
* @throws InvalidArgumentException When the passed value is not valid
3840
*/
3941
public static function fromBinary(string $uid): static
4042
{
4143
if (16 !== \strlen($uid)) {
42-
throw new \InvalidArgumentException('Invalid binary uid provided.');
44+
throw new InvalidArgumentException('Invalid binary uid provided.');
4345
}
4446

4547
return static::fromString($uid);
4648
}
4749

4850
/**
49-
* @throws \InvalidArgumentException When the passed value is not valid
51+
* @throws InvalidArgumentException When the passed value is not valid
5052
*/
5153
public static function fromBase58(string $uid): static
5254
{
5355
if (22 !== \strlen($uid)) {
54-
throw new \InvalidArgumentException('Invalid base-58 uid provided.');
56+
throw new InvalidArgumentException('Invalid base-58 uid provided.');
5557
}
5658

5759
return static::fromString($uid);
5860
}
5961

6062
/**
61-
* @throws \InvalidArgumentException When the passed value is not valid
63+
* @throws InvalidArgumentException When the passed value is not valid
6264
*/
6365
public static function fromBase32(string $uid): static
6466
{
6567
if (26 !== \strlen($uid)) {
66-
throw new \InvalidArgumentException('Invalid base-32 uid provided.');
68+
throw new InvalidArgumentException('Invalid base-32 uid provided.');
6769
}
6870

6971
return static::fromString($uid);
@@ -72,12 +74,12 @@ public static function fromBase32(string $uid): static
7274
/**
7375
* @param string $uid A valid RFC 9562/4122 uid
7476
*
75-
* @throws \InvalidArgumentException When the passed value is not valid
77+
* @throws InvalidArgumentException When the passed value is not valid
7678
*/
7779
public static function fromRfc4122(string $uid): static
7880
{
7981
if (36 !== \strlen($uid)) {
80-
throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
82+
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
8183
}
8284

8385
return static::fromString($uid);

src/Symfony/Component/Uid/BinaryUtil.php

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

1212
namespace Symfony\Component\Uid;
1313

14+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @internal
1618
*
@@ -162,7 +164,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
162164
{
163165
if (\PHP_INT_SIZE >= 8) {
164166
if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
165-
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
167+
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
166168
}
167169

168170
return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
@@ -171,7 +173,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
171173
$time = $time->format('Uu0');
172174
$negative = '-' === $time[0];
173175
if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
174-
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
176+
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
175177
}
176178
$time = self::fromBase($time, self::BASE10);
177179
$time = str_pad($time, 8, "\0", \STR_PAD_LEFT);

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+
7.3
5+
---
6+
7+
* Add component-specific exception hierarchy
8+
49
7.2
510
---
611

src/Symfony/Component/Uid/Command/GenerateUuidCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\Uid\Exception\LogicException;
2324
use Symfony\Component\Uid\Factory\UuidFactory;
2425
use Symfony\Component\Uid\Uuid;
2526

@@ -146,7 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146147
$create = function () use ($namespace, $name): Uuid {
147148
try {
148149
$factory = $this->factory->nameBased($namespace);
149-
} catch (\LogicException) {
150+
} catch (LogicException) {
150151
throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.');
151152
}
152153

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
class InvalidArgumentException extends \InvalidArgumentException
15+
{
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
class InvalidUlidException extends InvalidArgumentException
15+
{
16+
public function __construct(string $value)
17+
{
18+
parent::__construct(\sprintf('Invalid ULID: "%s".', $value));
19+
}
20+
}
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+
class InvalidUuidException extends InvalidArgumentException
15+
{
16+
public function __construct(
17+
public readonly int $type,
18+
string $value,
19+
) {
20+
parent::__construct(\sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value));
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
class LogicException extends \LogicException
15+
{
16+
}

src/Symfony/Component/Uid/Factory/UuidFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Uid\Factory;
1313

14+
use Symfony\Component\Uid\Exception\LogicException;
1415
use Symfony\Component\Uid\Uuid;
1516
use Symfony\Component\Uid\UuidV1;
1617
use Symfony\Component\Uid\UuidV4;
@@ -67,12 +68,15 @@ public function timeBased(Uuid|string|null $node = null): TimeBasedUuidFactory
6768
return new TimeBasedUuidFactory($this->timeBasedClass, $node);
6869
}
6970

71+
/**
72+
* @throws LogicException When no namespace is defined
73+
*/
7074
public function nameBased(Uuid|string|null $namespace = null): NameBasedUuidFactory
7175
{
7276
$namespace ??= $this->nameBasedNamespace;
7377

7478
if (null === $namespace) {
75-
throw new \LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
79+
throw new LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
7680
}
7781

7882
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));

src/Symfony/Component/Uid/Tests/Factory/UlidFactoryTest.php

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

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
1516
use Symfony\Component\Uid\Factory\UlidFactory;
1617

1718
final class UlidFactoryTest extends TestCase
@@ -36,7 +37,7 @@ public function testCreate()
3637

3738
public function testCreateWithInvalidTimestamp()
3839
{
39-
$this->expectException(\InvalidArgumentException::class);
40+
$this->expectException(InvalidArgumentException::class);
4041
$this->expectExceptionMessage('The timestamp must be positive.');
4142

4243
(new UlidFactory())->create(new \DateTimeImmutable('@-1000'));

0 commit comments

Comments
 (0)