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

Commit c0fc5eb

Browse files
committed
docs: added Count rule
1 parent 8c081aa commit c0fc5eb

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

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

0 commit comments

Comments
 (0)