mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
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:
parent
630e6ecb6d
commit
ea605b61b6
|
@ -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,
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}> */
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue