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

Commit bb6dc1d

Browse files
committed
chore: refactored exceptions
1 parent 131a3e5 commit bb6dc1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+186
-226
lines changed

docs/03-rules_choice.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->val
4242
> [!NOTE]
4343
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.
4444
45-
> [!NOTE]
46-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
47-
4845
## Options
4946

5047
### `constraints`
@@ -106,7 +103,7 @@ The following parameters are available:
106103

107104
### `minMessage`
108105

109-
type: `?string` default: `The {{ name }} value must have at least {{ min }} choices.`
106+
type: `?string` default: `The {{ name }} value must have at least {{ min }} choice(s).`
110107

111108
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `min`.
112109

@@ -123,7 +120,7 @@ The following parameters are available:
123120

124121
### `maxMessage`
125122

126-
type: `?string` default: `The {{ name }} value must have at most {{ max }} choices.`
123+
type: `?string` default: `The {{ name }} value must have at most {{ max }} choice(s).`
127124

128125
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `max`.
129126

docs/03-rules_count.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
3232
> [!NOTE]
3333
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
3434
35-
> [!NOTE]
36-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
37-
3835
> [!NOTE]
3936
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
4037

docs/03-rules_each-key.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type: `?string` default: `Invalid key: {{ message }}`
3939
Message that will be shown if at least one input value key is invalid according to the given `validator`.
4040

4141
```php
42-
// throws: Invalid key: The color key value should be of type "string", 1 given.
42+
// throws: Invalid key 1: The value should be of type "string".
4343
Validator::eachKey(
4444
Validator::type('string')
4545
)->assert(['red' => '#f00', 1 => '#0f0'], 'color');

docs/03-rules_length.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ Validator::length(max: 1, countUnit: 'graphemes')->validate('🔥'); // true
4343
> [!NOTE]
4444
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
4545
46-
> [!NOTE]
47-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
48-
4946
> [!NOTE]
5047
> An `UnexpectedValueException` will be thrown when the `charset` value is not a valid option.
5148
> Check all the supported character encodings [here](https://www.php.net/manual/en/mbstring.supported-encodings.php).
@@ -107,7 +104,7 @@ Validator::length(max: 3, normalizer: fn($value) => trim($value))->validate('abc
107104

108105
### `minMessage`
109106

110-
type: `?string` default: `The {{ name }} value should have {{ min }} characters or more.`
107+
type: `?string` default: `The {{ name }} value should have {{ min }} character(s) or more.`
111108

112109
Message that will be shown when the input value has fewer characters than the defined in `min`.
113110

@@ -125,7 +122,7 @@ The following parameters are available:
125122

126123
### `maxMessage`
127124

128-
type: `?string` default: `The {{ name }} value should have {{ max }} characters or less.`
125+
type: `?string` default: `The {{ name }} value should have {{ max }} character(s) or less.`
129126

130127
Message that will be shown when the input value has more characters than the defined in `max`.
131128

docs/03-rules_range.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(
3636
> [!NOTE]
3737
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`.
3838
39-
> [!NOTE]
40-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
41-
4239
## Options
4340

4441
### `min`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class InvalidOptionException extends UnexpectedValueException
6+
{
7+
public function __construct(string $name, string|array|null $expected = null)
8+
{
9+
$message = \sprintf('The "%s" option is not valid.', $name);
10+
11+
if (\is_array($expected)) {
12+
$message = \sprintf('%s Accepted values are: "%s".', $message, \implode('", "', $expected));
13+
}
14+
else if (\is_string($expected)) {
15+
$message = \sprintf('%s %s', $message, $expected);
16+
}
17+
18+
parent::__construct($message);
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class OptionDefinitionException extends UnexpectedValueException {}

src/Exception/UnexpectedComparableException.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
class UnexpectedComparableException extends UnexpectedValueException
66
{
7-
public function __construct(string $value1, string $value2)
7+
public function __construct(mixed $value1, mixed $value2)
88
{
9-
$message = \sprintf('Cannot compare a type "%s" with a type "%s".', $value1, $value2);
9+
$message = \sprintf(
10+
'Cannot compare a type "%s" with a type "%s".',
11+
\get_debug_type($value1),
12+
\get_debug_type($value2)
13+
);
1014

1115
parent::__construct($message);
1216
}

src/Exception/UnexpectedOptionException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Rule/AbstractComparisonRule.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ abstract class AbstractComparisonRule extends AbstractRule
1212
public function assert(mixed $value, ?string $name = null): void
1313
{
1414
if (!$this->isComparable($value, $this->constraint)) {
15-
throw new UnexpectedComparableException(
16-
get_debug_type($value),
17-
get_debug_type($this->constraint)
18-
);
15+
throw new UnexpectedComparableException($value, $this->constraint);
1916
}
2017

2118
if (!$this->compareValues($value, $this->constraint)) {

0 commit comments

Comments
 (0)