Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6bf755d

Browse files
committed
chore: refactored common message exceptions
1 parent 2b78f11 commit 6bf755d

File tree

8 files changed

+39
-31
lines changed

8 files changed

+39
-31
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class UnexpectedOptionException extends UnexpectedValueException
6+
{
7+
public function __construct(string $name, array $expected, string $given)
8+
{
9+
$message = \sprintf('Invalid %s "%s". Accepted values are: "%s".', $name, $given, \implode(", ", $expected));
10+
11+
parent::__construct($message);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class UnexpectedTypeException extends UnexpectedValueException
6+
{
7+
public function __construct(string $expected, string $given)
8+
{
9+
$message = \sprintf('Expected value of type "%s", "%s" given.', $expected, $given);
10+
11+
parent::__construct($message);
12+
}
13+
}

src/Rule/Choice.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ChoiceException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedTypeException;
67
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
78

89
class Choice extends AbstractRule implements RuleInterface
@@ -21,9 +22,7 @@ public function __construct(
2122
public function assert(mixed $value, ?string $name = null): void
2223
{
2324
if ($this->multiple && !\is_array($value)) {
24-
throw new UnexpectedValueException(
25-
\sprintf('Expected value of type "array" when using multiple choices, "%s" given', get_debug_type($value))
26-
);
25+
throw new UnexpectedTypeException('array', get_debug_type($value));
2726
}
2827

2928
if (

src/Rule/Country.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\CountryException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedOptionException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedTypeException;
78
use Symfony\Component\Intl\Countries;
89

910
class Country extends AbstractRule implements RuleInterface
@@ -24,19 +25,11 @@ public function __construct(
2425
public function assert(mixed $value, ?string $name = null): void
2526
{
2627
if (!\in_array($this->code, self::CODE_OPTIONS)) {
27-
throw new UnexpectedValueException(
28-
\sprintf(
29-
'Invalid code "%s". Accepted values are: "%s".',
30-
$this->code,
31-
\implode(", ", self::CODE_OPTIONS)
32-
)
33-
);
28+
throw new UnexpectedOptionException('code', self::CODE_OPTIONS, $this->code);
3429
}
3530

3631
if (!\is_string($value)) {
37-
throw new UnexpectedValueException(
38-
\sprintf('Expected value of type "string", "%s" given.', get_debug_type($value))
39-
);
32+
throw new UnexpectedTypeException('string', get_debug_type($value));
4033
}
4134

4235
// Keep original value for parameters

src/Rule/EachKey.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachKeyException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedTypeException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
99

@@ -17,9 +17,7 @@ public function __construct(
1717
public function assert(mixed $value, ?string $name = null): void
1818
{
1919
if (!\is_iterable($value)) {
20-
throw new UnexpectedValueException(
21-
\sprintf('Expected value of type "array|\Traversable", "%s" given.', get_debug_type($value))
22-
);
20+
throw new UnexpectedTypeException('array|\Traversable', get_debug_type($value));
2321
}
2422

2523
try {

src/Rule/EachValue.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachValueException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedTypeException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
99

@@ -17,9 +17,7 @@ public function __construct(
1717
public function assert(mixed $value, ?string $name = null): void
1818
{
1919
if (!\is_iterable($value)) {
20-
throw new UnexpectedValueException(
21-
\sprintf('Expected value of type "array|\Traversable", "%s" given.', get_debug_type($value))
22-
);
20+
throw new UnexpectedTypeException('array|\Traversable', get_debug_type($value));
2321
}
2422

2523
try {

src/Rule/Type.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\TypeException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedOptionException;
77

88
class Type extends AbstractRule implements RuleInterface
99
{
@@ -88,13 +88,7 @@ public function assert(mixed $value, ?string $name = null): void
8888
}
8989

9090
if (!isset(self::TYPE_FUNCTIONS[$constraint]) && !\class_exists($constraint) && !\interface_exists($constraint)) {
91-
throw new UnexpectedValueException(
92-
\sprintf(
93-
'Invalid constraint type "%s". Accepted values are: "%s"',
94-
$constraint,
95-
\implode('", "', \array_keys(self::TYPE_FUNCTIONS))
96-
)
97-
);
91+
throw new UnexpectedOptionException('constraint type', self::TYPE_FUNCTIONS, $constraint);
9892
}
9993
}
10094

tests/ChoiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ChoiceTest extends AbstractTest
1919
public static function provideRuleUnexpectedValueData(): \Generator
2020
{
2121
$constraints = [1, 2, 3, 4, 5];
22-
$multipleMessage = '/Expected value of type "array" when using multiple choices, "(.*)" given/';
22+
$multipleMessage = '/Expected value of type "array", "(.*)" given/';
2323
$constraintMessage = '/Max constraint value must be greater than or equal to min constraint value./';
2424

2525
yield 'multiple not array' => [new Choice($constraints, true), 1, $multipleMessage];

0 commit comments

Comments
 (0)