From f4a50b969a8612ef41e97df1bab97fd92febd8d7 Mon Sep 17 00:00:00 2001 From: Yevhen Sidelnyk Date: Thu, 1 May 2025 15:53:34 +0300 Subject: [PATCH] feat: add value to InvalidArgumentException --- src/Symfony/Component/Uid/AbstractUid.php | 8 ++++---- src/Symfony/Component/Uid/BinaryUtil.php | 4 ++-- src/Symfony/Component/Uid/CHANGELOG.md | 1 + .../Component/Uid/Exception/InvalidArgumentException.php | 6 ++++++ .../Component/Uid/Exception/InvalidUlidException.php | 2 +- .../Component/Uid/Exception/InvalidUuidException.php | 2 +- src/Symfony/Component/Uid/Ulid.php | 2 +- src/Symfony/Component/Uid/UuidV6.php | 2 +- src/Symfony/Component/Uid/UuidV7.php | 2 +- 9 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Uid/AbstractUid.php b/src/Symfony/Component/Uid/AbstractUid.php index fa35031eaa789..38ee2d05bc954 100644 --- a/src/Symfony/Component/Uid/AbstractUid.php +++ b/src/Symfony/Component/Uid/AbstractUid.php @@ -41,7 +41,7 @@ abstract public static function fromString(string $uid): static; public static function fromBinary(string $uid): static { if (16 !== \strlen($uid)) { - throw new InvalidArgumentException('Invalid binary uid provided.'); + throw new InvalidArgumentException($uid, 'Invalid binary uid provided.'); } return static::fromString($uid); @@ -53,7 +53,7 @@ public static function fromBinary(string $uid): static public static function fromBase58(string $uid): static { if (22 !== \strlen($uid)) { - throw new InvalidArgumentException('Invalid base-58 uid provided.'); + throw new InvalidArgumentException($uid, 'Invalid base-58 uid provided.'); } return static::fromString($uid); @@ -65,7 +65,7 @@ public static function fromBase58(string $uid): static public static function fromBase32(string $uid): static { if (26 !== \strlen($uid)) { - throw new InvalidArgumentException('Invalid base-32 uid provided.'); + throw new InvalidArgumentException($uid, 'Invalid base-32 uid provided.'); } return static::fromString($uid); @@ -79,7 +79,7 @@ public static function fromBase32(string $uid): static public static function fromRfc4122(string $uid): static { if (36 !== \strlen($uid)) { - throw new InvalidArgumentException('Invalid RFC4122 uid provided.'); + throw new InvalidArgumentException($uid, 'Invalid RFC4122 uid provided.'); } return static::fromString($uid); diff --git a/src/Symfony/Component/Uid/BinaryUtil.php b/src/Symfony/Component/Uid/BinaryUtil.php index 7d1e524e5e43e..a363aa5ea78e9 100644 --- a/src/Symfony/Component/Uid/BinaryUtil.php +++ b/src/Symfony/Component/Uid/BinaryUtil.php @@ -164,7 +164,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string { if (\PHP_INT_SIZE >= 8) { if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) { - throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.'); + throw new InvalidArgumentException($time, 'The given UUID date cannot be earlier than 1582-10-15.'); } return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT); @@ -173,7 +173,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string $time = $time->format('Uu0'); $negative = '-' === $time[0]; if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) { - throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.'); + throw new InvalidArgumentException($time, 'The given UUID date cannot be earlier than 1582-10-15.'); } $time = self::fromBase($time, self::BASE10); $time = str_pad($time, 8, "\0", \STR_PAD_LEFT); diff --git a/src/Symfony/Component/Uid/CHANGELOG.md b/src/Symfony/Component/Uid/CHANGELOG.md index 31291948419c5..8f8bca9540134 100644 --- a/src/Symfony/Component/Uid/CHANGELOG.md +++ b/src/Symfony/Component/Uid/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add component-specific exception hierarchy + * Add `InvalidArgumentException::$value` 7.2 --- diff --git a/src/Symfony/Component/Uid/Exception/InvalidArgumentException.php b/src/Symfony/Component/Uid/Exception/InvalidArgumentException.php index c28737bea8b2a..d4037c36bbce3 100644 --- a/src/Symfony/Component/Uid/Exception/InvalidArgumentException.php +++ b/src/Symfony/Component/Uid/Exception/InvalidArgumentException.php @@ -13,4 +13,10 @@ class InvalidArgumentException extends \InvalidArgumentException { + public function __construct( + public readonly mixed $value, + string $message, + ) { + parent::__construct($message); + } } diff --git a/src/Symfony/Component/Uid/Exception/InvalidUlidException.php b/src/Symfony/Component/Uid/Exception/InvalidUlidException.php index cfb42ac5867a7..4c11c481c12eb 100644 --- a/src/Symfony/Component/Uid/Exception/InvalidUlidException.php +++ b/src/Symfony/Component/Uid/Exception/InvalidUlidException.php @@ -15,6 +15,6 @@ class InvalidUlidException extends InvalidArgumentException { public function __construct(string $value) { - parent::__construct(\sprintf('Invalid ULID: "%s".', $value)); + parent::__construct($value, \sprintf('Invalid ULID: "%s".', $value)); } } diff --git a/src/Symfony/Component/Uid/Exception/InvalidUuidException.php b/src/Symfony/Component/Uid/Exception/InvalidUuidException.php index 97009412b9c63..26b79f39cd6e2 100644 --- a/src/Symfony/Component/Uid/Exception/InvalidUuidException.php +++ b/src/Symfony/Component/Uid/Exception/InvalidUuidException.php @@ -17,6 +17,6 @@ public function __construct( public readonly int $type, string $value, ) { - parent::__construct(\sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value)); + parent::__construct($value, \sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value)); } } diff --git a/src/Symfony/Component/Uid/Ulid.php b/src/Symfony/Component/Uid/Ulid.php index 9170d429b0eb7..7bb035aaa9eb3 100644 --- a/src/Symfony/Component/Uid/Ulid.php +++ b/src/Symfony/Component/Uid/Ulid.php @@ -157,7 +157,7 @@ public static function generate(?\DateTimeInterface $time = null): string $time = microtime(false); $time = substr($time, 11).substr($time, 2, 3); } elseif (0 > $time = $time->format('Uv')) { - throw new InvalidArgumentException('The timestamp must be positive.'); + throw new InvalidArgumentException($time, 'The timestamp must be positive.'); } if ($time > self::$time || (null !== $mtime && $time !== self::$time)) { diff --git a/src/Symfony/Component/Uid/UuidV6.php b/src/Symfony/Component/Uid/UuidV6.php index ea65ae4120289..2ea5962307e27 100644 --- a/src/Symfony/Component/Uid/UuidV6.php +++ b/src/Symfony/Component/Uid/UuidV6.php @@ -50,7 +50,7 @@ public function toV7(): UuidV7 $uuid = $this->uid; $time = BinaryUtil::hexToNumericString('0'.substr($uuid, 0, 8).substr($uuid, 9, 4).substr($uuid, 15, 3)); if ('-' === $time[0]) { - throw new InvalidArgumentException('Cannot convert UUID to v7: its timestamp is before the Unix epoch.'); + throw new InvalidArgumentException($uuid, 'Cannot convert UUID to v7: its timestamp is before the Unix epoch.'); } $ms = \strlen($time) > 4 ? substr($time, 0, -4) : '0'; diff --git a/src/Symfony/Component/Uid/UuidV7.php b/src/Symfony/Component/Uid/UuidV7.php index 0a6f01be1f234..7b7c3785954e3 100644 --- a/src/Symfony/Component/Uid/UuidV7.php +++ b/src/Symfony/Component/Uid/UuidV7.php @@ -57,7 +57,7 @@ public static function generate(?\DateTimeInterface $time = null): string $time = microtime(false); $time = substr($time, 11).substr($time, 2, 3); } elseif (0 > $time = $time->format('Uv')) { - throw new InvalidArgumentException('The timestamp must be positive.'); + throw new InvalidArgumentException($time, 'The timestamp must be positive.'); } if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {