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

Commit 31a72cd

Browse files
committed
chore: simplified min and max parameter naming
1 parent 82360ae commit 31a72cd

File tree

8 files changed

+97
-97
lines changed

8 files changed

+97
-97
lines changed

docs/03-rules_choice.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Validates that a value (or multiple values) exist in a given set of choices.
66
Choice(
77
array $constraints,
88
bool $multiple = false,
9-
?int $minConstraint = null,
10-
?int $maxConstraint = null,
9+
?int $min = null,
10+
?int $max = null,
1111
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
1212
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
13-
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
14-
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
13+
string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numValues }} choices given.',
14+
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numValues }} choices given.'
1515
);
1616
```
1717

@@ -27,23 +27,23 @@ Validator::choice(['red', 'green', 'blue'], multiple: true)->validate(['red', 'b
2727
Validator::choice(['red', 'green', 'blue'], multiple: true)->validate(['red', 'yellow']); // false;
2828

2929
// Multiple with minimum number of choices
30-
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2)->validate(['red', 'blue']); // true
31-
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2)->validate(['red']); // false
30+
Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2)->validate(['red', 'blue']); // true
31+
Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2)->validate(['red']); // false
3232

3333
// Multiple with maximum number of choices
34-
Validator::choice(['red', 'green', 'blue'], multiple: true, maxConstraint: 2)->validate(['red', 'blue']); // true
35-
Validator::choice(['red', 'green', 'blue'], multiple: true, maxConstraint: 2)->validate(['red', 'green', 'blue']); // false
34+
Validator::choice(['red', 'green', 'blue'], multiple: true, max: 2)->validate(['red', 'blue']); // true
35+
Validator::choice(['red', 'green', 'blue'], multiple: true, max: 2)->validate(['red', 'green', 'blue']); // false
3636

3737
// Multiple with minimum and maximum number of choices
38-
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2, maxConstraint: 3)->validate(['red', 'blue']); // true
39-
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2, maxConstraint: 3)->validate(['red']); // false
38+
Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->validate(['red', 'blue']); // true
39+
Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->validate(['red']); // false
4040
```
4141

4242
> [!NOTE]
4343
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.
4444
4545
> [!NOTE]
46-
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
46+
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
4747
4848
## Options
4949

@@ -60,21 +60,21 @@ type: `bool` default: `false`
6060
If this option is `true`, validation against an `array` of input values is enabled.
6161
Each element of the input array must be a valid choice, otherwise it will fail.
6262

63-
### `minConstraint`
63+
### `min`
6464

6565
type: `?int` default: `null`
6666

6767
If `multiple` is `true`, set a minimum number of input values to be required.
6868

69-
For example, if `minConstraint` is 2, the input array must have at least 2 values.
69+
For example, if `min` is 2, the input array must have at least 2 values.
7070

71-
### `maxConstraint`
71+
### `max`
7272

7373
type: `?int` default: `null`
7474

7575
If `multiple` is `true`, set a maximum number of input values to be required.
7676

77-
For example, if `maxConstraint` is 2, the input array must have at most 2 values.
77+
For example, if `max` is 2, the input array must have at most 2 values.
7878

7979
### `message`
8080

@@ -106,37 +106,37 @@ The following parameters are available:
106106

107107
### `minMessage`
108108

109-
type: `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
109+
type: `string` default: `The {{ name }} value must have at least {{ min }} choices, {{ numValues }} choices given.`
110110

111-
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `minConstraint`.
111+
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `min`.
112112

113113
The following parameters are available:
114114

115-
| Parameter | Description |
116-
|-----------------------|--------------------------------------|
117-
| `{{ value }}` | The current invalid value |
118-
| `{{ numValues }}` | The current invalid number of values |
119-
| `{{ name }}` | Name of the invalid value |
120-
| `{{ constraints }}` | The array of valid choices |
121-
| `{{ minConstraint }}` | The minimum number of valid choices |
122-
| `{{ maxConstraint }}` | The maximum number of valid choices |
115+
| Parameter | Description |
116+
|---------------------|--------------------------------------|
117+
| `{{ value }}` | The current invalid value |
118+
| `{{ numValues }}` | The current invalid number of values |
119+
| `{{ name }}` | Name of the invalid value |
120+
| `{{ constraints }}` | The array of valid choices |
121+
| `{{ min }}` | The minimum number of valid choices |
122+
| `{{ max }}` | The maximum number of valid choices |
123123

124124
### `maxMessage`
125125

126-
type: `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
126+
type: `string` default: `The {{ name }} value must have at most {{ max }} choices, {{ numValues }} choices given.`
127127

128-
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `maxConstraint`.
128+
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `max`.
129129

130130
The following parameters are available:
131131

132-
| Parameter | Description |
133-
|-----------------------|--------------------------------------|
134-
| `{{ value }}` | The current invalid value |
135-
| `{{ numValues }}` | The current invalid number of values |
136-
| `{{ name }}` | Name of the invalid value |
137-
| `{{ constraints }}` | The array of valid choices |
138-
| `{{ minConstraint }}` | The minimum number of valid choices |
139-
| `{{ maxConstraint }}` | The maximum number of valid choices |
132+
| Parameter | Description |
133+
|---------------------|--------------------------------------|
134+
| `{{ value }}` | The current invalid value |
135+
| `{{ numValues }}` | The current invalid number of values |
136+
| `{{ name }}` | Name of the invalid value |
137+
| `{{ constraints }}` | The array of valid choices |
138+
| `{{ min }}` | The minimum number of valid choices |
139+
| `{{ max }}` | The maximum number of valid choices |
140140

141141
## Changelog
142142

docs/03-rules_range.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Can compare between strings, numbers and dates.
55

66
```php
77
Range(
8-
mixed $minConstraint,
9-
mixed $maxConstraint,
10-
string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
8+
mixed $min,
9+
mixed $max,
10+
string $message = 'The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.'
1111
);
1212
```
1313

@@ -37,18 +37,18 @@ Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(
3737
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`.
3838
3939
> [!NOTE]
40-
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
40+
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
4141
4242
## Options
4343

44-
### `minConstraint`
44+
### `min`
4545

4646
type: `mixed` `required`
4747

4848
It defines the minimum range value.
4949
Can be a `string`, `int`, `float` or `DateTimeInterface` object.
5050

51-
### `maxConstraint`
51+
### `max`
5252

5353
type: `mixed` `required`
5454

@@ -57,18 +57,18 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
5757

5858
### `message`
5959

60-
type: `string` default: `The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.`
60+
type: `string` default: `The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.`
6161

62-
Message that will be shown if the value is not between the minimum and maximum constraint values.
62+
Message that will be shown if the value is not between the minimum and maximum values.
6363

6464
The following parameters are available:
6565

66-
| Parameter | Description |
67-
|-----------------------|---------------------------|
68-
| `{{ value }}` | The current invalid value |
69-
| `{{ name }}` | Name of the invalid value |
70-
| `{{ minConstraint }}` | The minimum range value |
71-
| `{{ maxConstraint }}` | The maximum range value |
66+
| Parameter | Description |
67+
|---------------|---------------------------|
68+
| `{{ value }}` | The current invalid value |
69+
| `{{ name }}` | Name of the invalid value |
70+
| `{{ min }}` | The minimum range value |
71+
| `{{ max }}` | The maximum range value |
7272

7373
## Changelog
7474

src/ChainedValidatorInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ interface ChainedValidatorInterface
1010
public function choice(
1111
array $constraints,
1212
bool $multiple = false,
13-
?int $minConstraint = null,
14-
?int $maxConstraint = null,
13+
?int $min = null,
14+
?int $max = null,
1515
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
1616
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
17-
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
18-
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
17+
string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numValues }} choices given.',
18+
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numValues }} choices given.'
1919
): ChainedValidatorInterface&Validator;
2020

2121
public function country(
@@ -65,9 +65,9 @@ public function notBlank(
6565
): ChainedValidatorInterface&Validator;
6666

6767
public function range(
68-
mixed $minConstraint,
69-
mixed $maxConstraint,
70-
string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
68+
mixed $min,
69+
mixed $max,
70+
string $message = 'The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.'
7171
): ChainedValidatorInterface&Validator;
7272

7373
public function rule(

src/Rule/Choice.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class Choice extends AbstractRule implements RuleInterface
1111
public function __construct(
1212
private readonly array $constraints,
1313
private readonly bool $multiple = false,
14-
private readonly ?int $minConstraint = null,
15-
private readonly ?int $maxConstraint = null,
14+
private readonly ?int $min = null,
15+
private readonly ?int $max = null,
1616
private readonly string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
1717
private readonly string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
18-
private readonly string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
19-
private readonly string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
18+
private readonly string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numValues }} choices given.',
19+
private readonly string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numValues }} choices given.'
2020
) {}
2121

2222
public function assert(mixed $value, ?string $name = null): void
@@ -27,12 +27,12 @@ public function assert(mixed $value, ?string $name = null): void
2727

2828
if (
2929
$this->multiple
30-
&& $this->minConstraint !== null
31-
&& $this->maxConstraint !== null
32-
&& $this->minConstraint > $this->maxConstraint
30+
&& $this->min !== null
31+
&& $this->max !== null
32+
&& $this->min > $this->max
3333
) {
3434
throw new UnexpectedValueException(
35-
'Max constraint value must be greater than or equal to min constraint value.'
35+
'Maximum value must be greater than or equal to minimum value.'
3636
);
3737
}
3838

@@ -52,30 +52,30 @@ public function assert(mixed $value, ?string $name = null): void
5252

5353
$numValues = \count($value);
5454

55-
if ($this->minConstraint !== null && $numValues < $this->minConstraint) {
55+
if ($this->min !== null && $numValues < $this->min) {
5656
throw new ChoiceException(
5757
message: $this->minMessage,
5858
parameters: [
5959
'value' => $value,
6060
'numValues' => $numValues,
6161
'name' => $name,
6262
'constraints' => $this->constraints,
63-
'minConstraint' => $this->minConstraint,
64-
'maxConstraint' => $this->maxConstraint
63+
'min' => $this->min,
64+
'max' => $this->max
6565
]
6666
);
6767
}
6868

69-
if ($this->maxConstraint !== null && $numValues > $this->maxConstraint) {
69+
if ($this->max !== null && $numValues > $this->max) {
7070
throw new ChoiceException(
7171
message: $this->maxMessage,
7272
parameters: [
7373
'value' => $value,
7474
'numValues' => $numValues,
7575
'name' => $name,
7676
'constraints' => $this->constraints,
77-
'minConstraint' => $this->minConstraint,
78-
'maxConstraint' => $this->maxConstraint
77+
'min' => $this->min,
78+
'max' => $this->max
7979
]
8080
);
8181
}

src/Rule/Range.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@ class Range extends AbstractRule implements RuleInterface
1313
use ComparableTrait;
1414

1515
public function __construct(
16-
private readonly mixed $minConstraint,
17-
private readonly mixed $maxConstraint,
18-
private readonly string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
16+
private readonly mixed $min,
17+
private readonly mixed $max,
18+
private readonly string $message = 'The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.'
1919
) {}
2020

2121
public function assert(mixed $value, ?string $name = null): void
2222
{
23-
if (!$this->isComparable($this->minConstraint, $this->maxConstraint)) {
23+
if (!$this->isComparable($this->min, $this->max)) {
2424
throw new UnexpectedComparableException(
25-
get_debug_type($this->minConstraint),
26-
get_debug_type($this->maxConstraint)
25+
get_debug_type($this->min),
26+
get_debug_type($this->max)
2727
);
2828
}
2929

30-
if (!Validator::greaterThan($this->minConstraint)->validate($this->maxConstraint)) {
30+
if (!Validator::greaterThan($this->min)->validate($this->max)) {
3131
throw new UnexpectedValueException(
32-
'Max constraint value must be greater than min constraint value.'
32+
'Maximum value must be greater than minimum value.'
3333
);
3434
}
3535

36-
if (!Validator::greaterThanOrEqual($this->minConstraint)->lessThanOrEqual($this->maxConstraint)->validate($value)) {
36+
if (!Validator::greaterThanOrEqual($this->min)->lessThanOrEqual($this->max)->validate($value)) {
3737
throw new RangeException(
3838
message: $this->message,
3939
parameters: [
4040
'value' => $value,
4141
'name' => $name,
42-
'minConstraint' => $this->minConstraint,
43-
'maxConstraint' => $this->maxConstraint
42+
'min' => $this->min,
43+
'max' => $this->max
4444
]
4545
);
4646
}

src/StaticValidatorInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ interface StaticValidatorInterface
99
public static function choice(
1010
array $constraints,
1111
bool $multiple = false,
12-
?int $minConstraint = null,
13-
?int $maxConstraint = null,
12+
?int $min = null,
13+
?int $max = null,
1414
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
1515
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
16-
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
17-
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
16+
string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numValues }} choices given.',
17+
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numValues }} choices given.'
1818
): ChainedValidatorInterface&Validator;
1919

2020
public static function country(
@@ -64,9 +64,9 @@ public static function notBlank(
6464
): ChainedValidatorInterface&Validator;
6565

6666
public static function range(
67-
mixed $minConstraint,
68-
mixed $maxConstraint,
69-
string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
67+
mixed $min,
68+
mixed $max,
69+
string $message = 'The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.'
7070
): ChainedValidatorInterface&Validator;
7171

7272
public static function rule(

0 commit comments

Comments
 (0)