|
| 1 | +# Regex |
| 2 | + |
| 3 | +Validates that a given regular expression pattern is valid. |
| 4 | + |
| 5 | +```php |
| 6 | +Regex( |
| 7 | + string $pattern, |
| 8 | + bool $match = true, |
| 9 | + ?callable $normalizer = null, |
| 10 | + ?string $message = null |
| 11 | +); |
| 12 | +``` |
| 13 | + |
| 14 | +## Basic Usage |
| 15 | + |
| 16 | +```php |
| 17 | +Validator::regex('/[a-z]/')->validate('abc'); // true |
| 18 | +Validator::regex('/[a-z]/')->validate('123'); // false |
| 19 | + |
| 20 | +// if match is false, assert that the pattern does not match |
| 21 | +// in this case, assert that the value does not contain any lowercase letters |
| 22 | +Validator::regex('/[a-z]/', match: false)->validate('abc'); // false |
| 23 | +Validator::regex('/[a-z]/', match: false)->validate('123'); // true |
| 24 | +``` |
| 25 | + |
| 26 | +> [!NOTE] |
| 27 | +> An `UnexpectedValueException` will be thrown if the `pattern` is not a valid regular expression. |
| 28 | +
|
| 29 | +> [!NOTE] |
| 30 | +> An `UnexpectedValueException` will be thrown when the input value is not a `string` or an object implementing `\Stringable`. |
| 31 | +
|
| 32 | +## Options |
| 33 | + |
| 34 | +### `pattern` |
| 35 | + |
| 36 | +type: `string` |
| 37 | + |
| 38 | +Regular expression pattern to be matched against. |
| 39 | + |
| 40 | +### `match` |
| 41 | + |
| 42 | +type: `bool` default: `true` |
| 43 | + |
| 44 | +- `true` the validation will pass if the given input value matches the regular expression pattern. |
| 45 | +- `false` the validation will pass if the given input value *does not* match the regular expression pattern. |
| 46 | + |
| 47 | +### `normalizer` |
| 48 | + |
| 49 | +type: `callable` default: `null` |
| 50 | + |
| 51 | +Allows to define a `callable` that will be applied to the value before checking if it is valid. |
| 52 | + |
| 53 | +For example, use `trim`, or pass your own function, to not evaluate whitespaces at the end of a string: |
| 54 | + |
| 55 | +```php |
| 56 | +// allow all chars except whitespaces |
| 57 | +Validator::length(pattern: '/^\S*$/')->validate('abc '); // false |
| 58 | + |
| 59 | +Validator::length(pattern: '/^\S*$/', normalizer: 'trim')->validate('abc '); // true |
| 60 | +Validator::length(pattern: '/^\S*$/', normalizer: fn($value) => trim($value))->validate('abc '); // true |
| 61 | +``` |
| 62 | + |
| 63 | +### `message` |
| 64 | + |
| 65 | +type: `?string` default: `The {{ name }} value is not valid.` |
| 66 | + |
| 67 | +Message that will be shown when the input value does not match the regular expression pattern. |
| 68 | + |
| 69 | +The following parameters are available: |
| 70 | + |
| 71 | +| Parameter | Description | |
| 72 | +|-----------------|---------------------------| |
| 73 | +| `{{ value }}` | The current invalid value | |
| 74 | +| `{{ name }}` | Name of the invalid value | |
| 75 | +| `{{ pattern }}` | The given pattern | |
| 76 | + |
| 77 | +## Changelog |
| 78 | + |
| 79 | +- `0.8.0` Created |
0 commit comments