Skip to content

Commit

Permalink
Make it easier to create "InvalidRuleConstructorException"
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 23, 2024
1 parent 9cc57f8 commit 2c56575
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 16 deletions.
4 changes: 3 additions & 1 deletion library/Exceptions/InvalidRuleConstructorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
3 changes: 1 addition & 2 deletions library/Rules/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions library/Rules/CountryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sokil\IsoCodes\Database\Countries;

use function class_exists;
use function implode;
use function in_array;
use function is_string;

Expand Down Expand Up @@ -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
);
}

Expand Down
5 changes: 2 additions & 3 deletions library/Rules/CurrencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sokil\IsoCodes\Database\Currencies;

use function class_exists;
use function implode;
use function in_array;

#[Template(
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions library/Rules/LanguageCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sokil\IsoCodes\Database\Languages;

use function class_exists;
use function implode;
use function in_array;
use function is_string;

Expand Down Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/SubdivisionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Rules/CountryCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Rules/CurrencyCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Rules/LanguageCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 2c56575

Please sign in to comment.