Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/03-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Basic Rules

- [Count](03-rules_count.md)
- [NotBlank](03-rules_not-blank.md)
- [Type](03-rules_type.md)

Expand Down
70 changes: 35 additions & 35 deletions docs/03-rules_choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Validates that a value (or multiple values) exist in a given set of choices.
Choice(
array $constraints,
bool $multiple = false,
?int $minConstraint = null,
?int $maxConstraint = null,
?int $min = null,
?int $max = null,
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.',
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.'
);
```

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

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

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

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

> [!NOTE]
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.

## Options

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

### `minConstraint`
### `min`

type: `?int` default: `null`

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

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

### `maxConstraint`
### `max`

type: `?int` default: `null`

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

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

### `message`

Expand Down Expand Up @@ -106,37 +106,37 @@ The following parameters are available:

### `minMessage`

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

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

The following parameters are available:

| Parameter | Description |
|-----------------------|--------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ numValues }}` | The current invalid number of values |
| `{{ name }}` | Name of the invalid value |
| `{{ constraints }}` | The array of valid choices |
| `{{ minConstraint }}` | The minimum number of valid choices |
| `{{ maxConstraint }}` | The maximum number of valid choices |
| Parameter | Description |
|---------------------|----------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ constraints }}` | The array of valid choices |
| `{{ min }}` | The minimum number of valid choices |
| `{{ max }}` | The maximum number of valid choices |
| `{{ numElements }}` | The current invalid number of elements |

### `maxMessage`

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

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

The following parameters are available:

| Parameter | Description |
|-----------------------|--------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ numValues }}` | The current invalid number of values |
| `{{ name }}` | Name of the invalid value |
| `{{ constraints }}` | The array of valid choices |
| `{{ minConstraint }}` | The minimum number of valid choices |
| `{{ maxConstraint }}` | The maximum number of valid choices |
| Parameter | Description |
|---------------------|----------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ constraints }}` | The array of valid choices |
| `{{ min }}` | The minimum number of valid choices |
| `{{ max }}` | The maximum number of valid choices |
| `{{ numElements }}` | The current invalid number of elements |

## Changelog

Expand Down
110 changes: 110 additions & 0 deletions docs/03-rules_count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Count

Validates that the number of elements of an `array`, or object implementing `\Countable`, is between a minimum and maximum value.

```php
Count(
?int $min = null,
?int $max = null,
?string $minMessage = null,
?string $maxMessage = null,
?string $exactMessage = null
);
```

## Basic Usage

```php
// min value
Validator::count(min: 1)->validate(['a', 'b', 'c']); // true
Validator::count(min: 5)->validate(['a', 'b', 'c']); // false

// max value
Validator::count(max: 5)->validate(['a', 'b', 'c']); // true
Validator::count(max: 1)->validate(['a', 'b', 'c']); // false

// min and max value
Validator::count(min: 2, max: 4)->validate(['a', 'b', 'c']); // true

// exact value
Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
```

> [!NOTE]
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.

## Options

### `min`

type: `?int` default: `null`

It defines the minimum number of elements required.

For example, if `min` is 2, the input value must have at least 2 elements.

### `max`

type: `?int` default: `null`

It defines the maximum number of elements required.

For example, if `max` is 2, the input value must have at most 2 elements.

### `minMessage`

type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.`

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

The following parameters are available:

| Parameter | Description |
|---------------------|----------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ min }}` | The minimum number of valid elements |
| `{{ max }}` | The maximum number of valid elements |
| `{{ numElements }}` | The current invalid number of elements |

### `maxMessage`

type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less, {{ numElements }} elements given.`

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

The following parameters are available:

| Parameter | Description |
|---------------------|----------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ min }}` | The minimum number of valid elements |
| `{{ max }}` | The maximum number of valid elements |
| `{{ numElements }}` | The current invalid number of elements |

### `exactMessage`

type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements, {{ numElements }} elements given.`

Message that will be shown when `min` and `max` options have the same value and the input value has a different number of elements.

The following parameters are available:

| Parameter | Description |
|---------------------|----------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ min }}` | The minimum number of valid elements |
| `{{ max }}` | The maximum number of valid elements |
| `{{ numElements }}` | The current invalid number of elements |

## Changelog

- `0.7.0` Created
2 changes: 1 addition & 1 deletion docs/03-rules_each-value.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## EachValue

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

```php
EachValue(
Expand Down
28 changes: 14 additions & 14 deletions docs/03-rules_range.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Can compare between strings, numbers and dates.

```php
Range(
mixed $minConstraint,
mixed $maxConstraint,
string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
mixed $min,
mixed $max,
string $message = 'The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.'
);
```

Expand Down Expand Up @@ -37,18 +37,18 @@ Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.

## Options

### `minConstraint`
### `min`

type: `mixed` `required`

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

### `maxConstraint`
### `max`

type: `mixed` `required`

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

### `message`

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

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

The following parameters are available:

| Parameter | Description |
|-----------------------|---------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ minConstraint }}` | The minimum range value |
| `{{ maxConstraint }}` | The maximum range value |
| Parameter | Description |
|---------------|---------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ min }}` | The minimum range value |
| `{{ max }}` | The maximum range value |

## Changelog

Expand Down
22 changes: 15 additions & 7 deletions src/ChainedValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ interface ChainedValidatorInterface
public function choice(
array $constraints,
bool $multiple = false,
?int $minConstraint = null,
?int $maxConstraint = null,
?int $min = null,
?int $max = null,
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
string $minMessage = 'The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.',
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.'
): ChainedValidatorInterface&Validator;

public function count(
?int $min = null,
?int $max = null,
?string $minMessage = null,
?string $maxMessage = null,
?string $exactMessage = null
): ChainedValidatorInterface&Validator;

public function country(
Expand Down Expand Up @@ -65,9 +73,9 @@ public function notBlank(
): ChainedValidatorInterface&Validator;

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

public function rule(
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/CountException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class CountException extends ValidationException {}
Loading