From 2c56575ce30beb6cf9d1ecaadc2516c32657d54e Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Fri, 23 Feb 2024 02:27:41 +0100 Subject: [PATCH] Make it easier to create "InvalidRuleConstructorException" Signed-off-by: Henrique Moody --- library/Exceptions/InvalidRuleConstructorException.php | 4 +++- library/Rules/Charset.php | 3 +-- library/Rules/CountryCode.php | 5 ++--- library/Rules/CurrencyCode.php | 5 ++--- library/Rules/LanguageCode.php | 5 ++--- library/Rules/SubdivisionCode.php | 2 +- tests/unit/Rules/CountryCodeTest.php | 2 +- tests/unit/Rules/CurrencyCodeTest.php | 2 +- tests/unit/Rules/LanguageCodeTest.php | 2 +- 9 files changed, 14 insertions(+), 16 deletions(-) diff --git a/library/Exceptions/InvalidRuleConstructorException.php b/library/Exceptions/InvalidRuleConstructorException.php index 00b3f4a64..ec0721316 100644 --- a/library/Exceptions/InvalidRuleConstructorException.php +++ b/library/Exceptions/InvalidRuleConstructorException.php @@ -9,12 +9,14 @@ namespace Respect\Validation\Exceptions; +use function array_map; +use function Respect\Stringifier\stringify; use function sprintf; final class InvalidRuleConstructorException extends ComponentException implements Exception { public function __construct(string $message, mixed ...$arguments) { - parent::__construct(sprintf($message, ...$arguments)); + parent::__construct(sprintf($message, ...array_map(static fn($argument) => stringify($argument), $arguments))); } } diff --git a/library/Rules/Charset.php b/library/Rules/Charset.php index 0d43d7b48..df8f8f0c9 100644 --- a/library/Rules/Charset.php +++ b/library/Rules/Charset.php @@ -20,7 +20,6 @@ use function in_array; use function mb_detect_encoding; use function mb_list_encodings; -use function Respect\Stringifier\stringify; #[Template( '{{name}} must be in the {{charset|raw}} charset', @@ -37,7 +36,7 @@ public function __construct(string $charset, string ...$charsets) $charsets = array_merge([$charset], $charsets); $diff = array_diff($charsets, $available); if (count($diff) > 0) { - throw new InvalidRuleConstructorException('Invalid charset provided: %s', stringify(array_values($diff))); + throw new InvalidRuleConstructorException('Invalid charset provided: %s', array_values($diff)); } $this->charset = $charsets; diff --git a/library/Rules/CountryCode.php b/library/Rules/CountryCode.php index 2b1b8508f..4c4892285 100644 --- a/library/Rules/CountryCode.php +++ b/library/Rules/CountryCode.php @@ -16,7 +16,6 @@ use Sokil\IsoCodes\Database\Countries; use function class_exists; -use function implode; use function in_array; use function is_string; @@ -44,9 +43,9 @@ public function __construct( $availableOptions = ['alpha-2', 'alpha-3', 'numeric']; if (!in_array($set, $availableOptions, true)) { throw new InvalidRuleConstructorException( - '"%s" is not a valid set for ISO 3166-1 (Available: %s)', + '%s is not a valid set for ISO 3166-1 (Available: %s)', $set, - implode(', ', $availableOptions) + $availableOptions ); } diff --git a/library/Rules/CurrencyCode.php b/library/Rules/CurrencyCode.php index 2a1658de6..d448ea530 100644 --- a/library/Rules/CurrencyCode.php +++ b/library/Rules/CurrencyCode.php @@ -16,7 +16,6 @@ use Sokil\IsoCodes\Database\Currencies; use function class_exists; -use function implode; use function in_array; #[Template( @@ -43,9 +42,9 @@ public function __construct( $availableSets = ['alpha-3', 'numeric']; if (!in_array($set, $availableSets, true)) { throw new InvalidRuleConstructorException( - '"%s" is not a valid set for ISO 4217 (Available: %s)', + '%s is not a valid set for ISO 4217 (Available: %s)', $set, - implode(', ', $availableSets) + $availableSets ); } $this->currencies = $currencies ?? new Currencies(); diff --git a/library/Rules/LanguageCode.php b/library/Rules/LanguageCode.php index 0f6b02643..d6cc4a6c8 100644 --- a/library/Rules/LanguageCode.php +++ b/library/Rules/LanguageCode.php @@ -17,7 +17,6 @@ use Sokil\IsoCodes\Database\Languages; use function class_exists; -use function implode; use function in_array; use function is_string; @@ -45,9 +44,9 @@ public function __construct( $availableSets = ['alpha-2', 'alpha-3']; if (!in_array($set, $availableSets, true)) { throw new InvalidRuleConstructorException( - '"%s" is not a valid set for ISO 639-3 (Available: %s)', + '%s is not a valid set for ISO 639-3 (Available: %s)', $set, - implode(', ', $availableSets) + $availableSets ); } diff --git a/library/Rules/SubdivisionCode.php b/library/Rules/SubdivisionCode.php index 1d4d701a4..577f25d06 100644 --- a/library/Rules/SubdivisionCode.php +++ b/library/Rules/SubdivisionCode.php @@ -44,7 +44,7 @@ public function __construct(string $countryCode, ?Countries $countries = null, ? $countries ??= new Countries(); $country = $countries->getByAlpha2($countryCode); if ($country === null) { - throw new InvalidRuleConstructorException('"%s" is not a supported country code', $countryCode); + throw new InvalidRuleConstructorException('%s is not a supported country code', $countryCode); } $this->country = $country; diff --git a/tests/unit/Rules/CountryCodeTest.php b/tests/unit/Rules/CountryCodeTest.php index 209cefee3..318d0a92d 100644 --- a/tests/unit/Rules/CountryCodeTest.php +++ b/tests/unit/Rules/CountryCodeTest.php @@ -24,7 +24,7 @@ public function itShouldThrowsExceptionWhenInvalidFormat(): void { $this->expectException(InvalidRuleConstructorException::class); $this->expectExceptionMessage( - '"whatever" is not a valid set for ISO 3166-1 (Available: alpha-2, alpha-3, numeric)' + '"whatever" is not a valid set for ISO 3166-1 (Available: `["alpha-2", "alpha-3", "numeric"]`)' ); // @phpstan-ignore-next-line diff --git a/tests/unit/Rules/CurrencyCodeTest.php b/tests/unit/Rules/CurrencyCodeTest.php index 7fc11b916..d32917bb3 100644 --- a/tests/unit/Rules/CurrencyCodeTest.php +++ b/tests/unit/Rules/CurrencyCodeTest.php @@ -24,7 +24,7 @@ public function itShouldThrowsExceptionWhenInvalidFormat(): void { $this->expectException(InvalidRuleConstructorException::class); $this->expectExceptionMessage( - '"whatever" is not a valid set for ISO 4217 (Available: alpha-3, numeric)' + '"whatever" is not a valid set for ISO 4217 (Available: `["alpha-3", "numeric"]`)' ); // @phpstan-ignore-next-line diff --git a/tests/unit/Rules/LanguageCodeTest.php b/tests/unit/Rules/LanguageCodeTest.php index 38cfcb380..07b377a61 100644 --- a/tests/unit/Rules/LanguageCodeTest.php +++ b/tests/unit/Rules/LanguageCodeTest.php @@ -23,7 +23,7 @@ final class LanguageCodeTest extends RuleTestCase public function itShouldThrowAnExceptionWhenSetIsInvalid(): void { $this->expectException(InvalidRuleConstructorException::class); - $this->expectExceptionMessage('"foo" is not a valid set for ISO 639-3 (Available: alpha-2, alpha-3)'); + $this->expectExceptionMessage('"foo" is not a valid set for ISO 639-3 (Available: `["alpha-2", "alpha-3"]`)'); // @phpstan-ignore-next-line new LanguageCode('foo');