diff --git a/src/Symfony/Component/Uid/CHANGELOG.md b/src/Symfony/Component/Uid/CHANGELOG.md index f53899b6061c2..91fc15701fe4a 100644 --- a/src/Symfony/Component/Uid/CHANGELOG.md +++ b/src/Symfony/Component/Uid/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +7.3 +--- + + * Add `Symfony\Component\Uid\InvalidUuidException` + * Add `Symfony\Component\Uid\InvalidUlidException` + 7.2 --- diff --git a/src/Symfony/Component/Uid/InvalidUidException.php b/src/Symfony/Component/Uid/InvalidUidException.php new file mode 100644 index 0000000000000..9b1a02be15ef9 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUidException.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +abstract class InvalidUidException extends \InvalidArgumentException +{ + public function __construct( + private readonly string $value, + string $message, + ) { + parent::__construct($message); + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/Symfony/Component/Uid/InvalidUlidException.php b/src/Symfony/Component/Uid/InvalidUlidException.php new file mode 100644 index 0000000000000..5512c5c75d4d3 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUlidException.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +final class InvalidUlidException extends InvalidUidException +{ + public function __construct(string $value) + { + parent::__construct($value, \sprintf('Invalid ULID: "%s".', $value)); + } +} diff --git a/src/Symfony/Component/Uid/InvalidUuidException.php b/src/Symfony/Component/Uid/InvalidUuidException.php new file mode 100644 index 0000000000000..316245ed280e3 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUuidException.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +final class InvalidUuidException extends InvalidUidException +{ + public function __construct(int $type, string $value) + { + parent::__construct($value, \sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value)); + } +} diff --git a/src/Symfony/Component/Uid/Tests/UlidTest.php b/src/Symfony/Component/Uid/Tests/UlidTest.php index 338b699159a77..e5b521db4e3ff 100644 --- a/src/Symfony/Component/Uid/Tests/UlidTest.php +++ b/src/Symfony/Component/Uid/Tests/UlidTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Uid\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Uid\InvalidUlidException; use Symfony\Component\Uid\MaxUlid; use Symfony\Component\Uid\NilUlid; use Symfony\Component\Uid\Tests\Fixtures\CustomUlid; @@ -41,7 +42,7 @@ public function testGenerate() public function testWithInvalidUlid() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidUlidException::class); $this->expectExceptionMessage('Invalid ULID: "this is not a ulid".'); new Ulid('this is not a ulid'); diff --git a/src/Symfony/Component/Uid/Ulid.php b/src/Symfony/Component/Uid/Ulid.php index 1240b019e28e2..3e208e8f2d65b 100644 --- a/src/Symfony/Component/Uid/Ulid.php +++ b/src/Symfony/Component/Uid/Ulid.php @@ -36,7 +36,7 @@ public function __construct(?string $ulid = null) $this->uid = $ulid; } else { if (!self::isValid($ulid)) { - throw new \InvalidArgumentException(\sprintf('Invalid ULID: "%s".', $ulid)); + throw new InvalidUlidException($ulid); } $this->uid = strtoupper($ulid); diff --git a/src/Symfony/Component/Uid/Uuid.php b/src/Symfony/Component/Uid/Uuid.php index c956156a3d580..8604ff3e38163 100644 --- a/src/Symfony/Component/Uid/Uuid.php +++ b/src/Symfony/Component/Uid/Uuid.php @@ -39,13 +39,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 \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid)); + throw new InvalidUuidException(static::TYPE, $uuid); } $this->uid = strtolower($uuid); if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) { - throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid)); + throw new InvalidUuidException(static::TYPE, $uuid); } }