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

Commit 3efac2a

Browse files
authored
Merge pull request #50 from programmatordev/YAPV-33-create-count-rule
Create Count rule
2 parents 82360ae + 43550c0 commit 3efac2a

14 files changed

+396
-103
lines changed

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
## Basic Rules
1111

12+
- [Count](03-rules_count.md)
1213
- [NotBlank](03-rules_not-blank.md)
1314
- [Type](03-rules_type.md)
1415

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, {{ numElements }} choices given.',
14+
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} 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, {{ numElements }} 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+
| `{{ name }}` | Name of the invalid value |
119+
| `{{ constraints }}` | The array of valid choices |
120+
| `{{ min }}` | The minimum number of valid choices |
121+
| `{{ max }}` | The maximum number of valid choices |
122+
| `{{ numElements }}` | The current invalid number of elements |
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, {{ numElements }} 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+
| `{{ name }}` | Name of the invalid value |
136+
| `{{ constraints }}` | The array of valid choices |
137+
| `{{ min }}` | The minimum number of valid choices |
138+
| `{{ max }}` | The maximum number of valid choices |
139+
| `{{ numElements }}` | The current invalid number of elements |
140140

141141
## Changelog
142142

docs/03-rules_count.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Count
2+
3+
Validates that the number of elements of an `array`, or object implementing `\Countable`, is between a minimum and maximum value.
4+
5+
```php
6+
Count(
7+
?int $min = null,
8+
?int $max = null,
9+
?string $minMessage = null,
10+
?string $maxMessage = null,
11+
?string $exactMessage = null
12+
);
13+
```
14+
15+
## Basic Usage
16+
17+
```php
18+
// min value
19+
Validator::count(min: 1)->validate(['a', 'b', 'c']); // true
20+
Validator::count(min: 5)->validate(['a', 'b', 'c']); // false
21+
22+
// max value
23+
Validator::count(max: 5)->validate(['a', 'b', 'c']); // true
24+
Validator::count(max: 1)->validate(['a', 'b', 'c']); // false
25+
26+
// min and max value
27+
Validator::count(min: 2, max: 4)->validate(['a', 'b', 'c']); // true
28+
29+
// exact value
30+
Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
31+
```
32+
33+
> [!NOTE]
34+
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
35+
36+
> [!NOTE]
37+
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
38+
39+
> [!NOTE]
40+
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
41+
42+
## Options
43+
44+
### `min`
45+
46+
type: `?int` default: `null`
47+
48+
It defines the minimum number of elements required.
49+
50+
For example, if `min` is 2, the input value must have at least 2 elements.
51+
52+
### `max`
53+
54+
type: `?int` default: `null`
55+
56+
It defines the maximum number of elements required.
57+
58+
For example, if `max` is 2, the input value must have at most 2 elements.
59+
60+
### `minMessage`
61+
62+
type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.`
63+
64+
Message that will be shown when the input value has fewer elements than the defined in `min`.
65+
66+
The following parameters are available:
67+
68+
| Parameter | Description |
69+
|---------------------|----------------------------------------|
70+
| `{{ value }}` | The current invalid value |
71+
| `{{ name }}` | Name of the invalid value |
72+
| `{{ min }}` | The minimum number of valid elements |
73+
| `{{ max }}` | The maximum number of valid elements |
74+
| `{{ numElements }}` | The current invalid number of elements |
75+
76+
### `maxMessage`
77+
78+
type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less, {{ numElements }} elements given.`
79+
80+
Message that will be shown when the input value has more elements than the defined in `max`.
81+
82+
The following parameters are available:
83+
84+
| Parameter | Description |
85+
|---------------------|----------------------------------------|
86+
| `{{ value }}` | The current invalid value |
87+
| `{{ name }}` | Name of the invalid value |
88+
| `{{ min }}` | The minimum number of valid elements |
89+
| `{{ max }}` | The maximum number of valid elements |
90+
| `{{ numElements }}` | The current invalid number of elements |
91+
92+
### `exactMessage`
93+
94+
type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements, {{ numElements }} elements given.`
95+
96+
Message that will be shown when `min` and `max` options have the same value and the input value has a different number of elements.
97+
98+
The following parameters are available:
99+
100+
| Parameter | Description |
101+
|---------------------|----------------------------------------|
102+
| `{{ value }}` | The current invalid value |
103+
| `{{ name }}` | Name of the invalid value |
104+
| `{{ min }}` | The minimum number of valid elements |
105+
| `{{ max }}` | The maximum number of valid elements |
106+
| `{{ numElements }}` | The current invalid number of elements |
107+
108+
## Changelog
109+
110+
- `0.7.0` Created

docs/03-rules_each-value.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## EachValue
22

3-
Validates every element of an `array` or object implementing `\Traversable` with a given set of rules.
3+
Validates every element of an `array`, or object implementing `\Traversable`, with a given set of rules.
44

55
```php
66
EachValue(

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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ 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, {{ numElements }} choices given.',
18+
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.'
19+
): ChainedValidatorInterface&Validator;
20+
21+
public function count(
22+
?int $min = null,
23+
?int $max = null,
24+
?string $minMessage = null,
25+
?string $maxMessage = null,
26+
?string $exactMessage = null
1927
): ChainedValidatorInterface&Validator;
2028

2129
public function country(
@@ -65,9 +73,9 @@ public function notBlank(
6573
): ChainedValidatorInterface&Validator;
6674

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

7381
public function rule(

src/Exception/CountException.php

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 CountException extends ValidationException {}

0 commit comments

Comments
 (0)