Simplify InvalidRuleConstructorException

The message wasn't looking very user friendly, so I changed it a little
bit.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-03-24 17:45:54 +01:00
parent 630e6ecb6d
commit ea605b61b6
No known key found for this signature in database
GPG key ID: 221E9281655813A6
10 changed files with 39 additions and 22 deletions

View file

@ -10,13 +10,37 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use function array_map;
use function Respect\Stringifier\stringify;
use function array_pop;
use function count;
use function current;
use function implode;
use function is_scalar;
use function sprintf;
final class InvalidRuleConstructorException extends ComponentException implements Exception
{
public function __construct(string $message, mixed ...$arguments)
/** @param string|array<string> ...$arguments */
public function __construct(string $message, string|array ...$arguments)
{
parent::__construct(sprintf($message, ...array_map(static fn($argument) => stringify($argument), $arguments)));
parent::__construct(sprintf(
$message,
...array_map(
static function (array|string $value) {
if (is_scalar($value)) {
return $value;
}
if (count($value) === 1) {
return '"' . current($value) . '"';
}
$items = array_map(static fn($item) => '"' . $item . '"', $value);
$items[] = 'and ' . array_pop($items);
return implode(count($items) > 2 ? ', ' : ' ', $items);
},
$arguments,
)
));
}
}

View file

@ -44,7 +44,7 @@ final class CountryCode extends Standard
$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,
$availableOptions
);

View file

@ -43,7 +43,7 @@ final class CurrencyCode extends Standard
$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,
$availableSets
);

View file

@ -45,7 +45,7 @@ final class LanguageCode extends Standard
$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,
$availableSets
);

View file

@ -45,7 +45,7 @@ final class SubdivisionCode extends Standard
$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;

View file

@ -25,7 +25,7 @@ final class CharsetTest extends RuleTestCase
public function itShouldThrowsExceptionWhenCharsetIsNotValid(): void
{
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Invalid charset provided: `["UTF-9"]`');
$this->expectExceptionMessage('Invalid charset provided: "UTF-9"');
new Charset('UTF-8', 'UTF-9');
}

View file

@ -24,7 +24,7 @@ final class CountryCodeTest extends RuleTestCase
{
$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", and "numeric")'
);
// @phpstan-ignore-next-line

View file

@ -24,7 +24,7 @@ final class CurrencyCodeTest extends RuleTestCase
{
$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" and "numeric")'
);
// @phpstan-ignore-next-line

View file

@ -23,10 +23,12 @@ 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(
'"whatever" is not a valid set for ISO 639-3 (Available: "alpha-2" and "alpha-3")'
);
// @phpstan-ignore-next-line
new LanguageCode('foo');
new LanguageCode('whatever');
}
/** @return iterable<array{LanguageCode, mixed}> */

View file

@ -20,7 +20,7 @@ use Respect\Validation\Test\RuleTestCase;
final class SubdivisionCodeTest extends RuleTestCase
{
#[Test]
public function shouldThrowsExceptionWhenInvalidFormat(): void
public function shouldNotAcceptWrongNamesOnConstructor(): void
{
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('"whatever" is not a supported country code');
@ -28,15 +28,6 @@ final class SubdivisionCodeTest extends RuleTestCase
new SubdivisionCode('whatever');
}
#[Test]
public function shouldNotAcceptWrongNamesOnConstructor(): void
{
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('"JK" is not a supported country code');
new SubdivisionCode('JK');
}
/** @return iterable<array{SubdivisionCode, mixed}> */
public static function providerForValidInput(): iterable
{