mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
This commit introduces the `Trimmed` validator that ensures a string cannot start or end with a list of specific values. The default values used are a selected list of Unicode invisible characters. To support this change, the StartsWith and EndsWith validators were modified so they can also support multiple values to check for. While StartsWith and EndsWith are more generic, and also perform start-of-array and end-of-array kinds of checks, Trimmed is more focused on string inputs, which tailors to a more specific use case.
87 lines
2.7 KiB
Markdown
87 lines
2.7 KiB
Markdown
<!--
|
||
SPDX-License-Identifier: MIT
|
||
SPDX-FileCopyrightText: (c) Respect Project Contributors
|
||
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
||
-->
|
||
|
||
# EndsWith
|
||
|
||
- `EndsWith(mixed $endValue)`
|
||
- `EndsWith(mixed $endValue, mixed ...$endValues)`
|
||
|
||
This validator is similar to `Contains()`, but validates
|
||
only if one of the values is at the end of the input. Only
|
||
string inputs and string end values are checked; non‑string
|
||
values are considered invalid but will not produce PHP errors
|
||
thanks to internal type guards.
|
||
|
||
For strings (non-string inputs are always rejected):
|
||
|
||
```php
|
||
v::endsWith('ipsum')->assert('lorem ipsum');
|
||
// Validation passes successfully
|
||
|
||
v::endsWith(', PhD', ', doctor')->assert('Jane Doe, PhD');
|
||
// Validation passes successfully
|
||
```
|
||
|
||
For arrays:
|
||
|
||
```php
|
||
v::endsWith('ipsum')->assert(['lorem', 'ipsum']);
|
||
// Validation passes successfully
|
||
|
||
v::endsWith('.', ';')->assert(['this', 'is', 'a', 'tokenized', 'phrase', '.']);
|
||
// Validation passes successfully
|
||
|
||
v::endsWith('.', ';')->assert(['this', 'is', 'a', 'tokenized', 'phrase']);
|
||
// → `["this", "is", "a", "tokenized", "phrase"]` must end with "." or ";"
|
||
```
|
||
|
||
Message template for this validator includes `{{endValue}}` and `{{endValues}}`.
|
||
|
||
## Templates
|
||
|
||
### `EndsWith::TEMPLATE_STANDARD`
|
||
|
||
| Mode | Template |
|
||
| ---------: | :----------------------------------------- |
|
||
| `default` | {{subject}} must end with {{endValue}} |
|
||
| `inverted` | {{subject}} must not end with {{endValue}} |
|
||
|
||
### `EndsWith::TEMPLATE_MULTIPLE_VALUES`
|
||
|
||
| Mode | Template |
|
||
| ---------: | :------------------------------------------------------- |
|
||
| `default` | {{subject}} must end with {{endValues|list:or}} |
|
||
| `inverted` | {{subject}} must not end with {{endValues|list:or}} |
|
||
|
||
## Template placeholders
|
||
|
||
| Placeholder | Description |
|
||
| ----------- | ---------------------------------------------------------------- |
|
||
| `subject` | The validated input or the custom validator name (if specified). |
|
||
| `endValue` | The value that will be checked to be at the end of the input. |
|
||
| `endValues` | Additional values to check. |
|
||
|
||
## Categorization
|
||
|
||
- Arrays
|
||
- Strings
|
||
|
||
## Changelog
|
||
|
||
| Version | Description |
|
||
| ------: | :---------------------------------- |
|
||
| 3.1.0 | Added support for multiple values |
|
||
| 3.0.0 | Case-insensitive comparison removed |
|
||
| 0.3.9 | Created |
|
||
|
||
## See Also
|
||
|
||
- [Contains](Contains.md)
|
||
- [In](In.md)
|
||
- [Regex](Regex.md)
|
||
- [StartsWith](StartsWith.md)
|
||
- [Trimmed](Trimmed.md)
|