mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 23:59:51 +01:00
Since we have the ability to use `not` as a prefix, having rules that validate negative behaviour makes them a bit inflexible, verbose, and harder to understand. This commit will refactor the `NotEmpty`, and rename it to `Falsy`. It will no longer trim strings, because Blank does a much better job at it; it only simulates the behaviour of PHP’s native `empty()` function. Because `Falsy`, `Blank`, and `Undef` have similar behaviour, I created a page to demonstrate the difference and show when the user should use one or the other. Assisted-by: Cursor (claude-4.5-opus-high)
80 lines
1.9 KiB
Markdown
80 lines
1.9 KiB
Markdown
# Call
|
|
|
|
- `Call(callable $callable, Rule $rule)`
|
|
|
|
Validates the return of a [callable][] for a given input.
|
|
|
|
Consider the following variable:
|
|
|
|
```php
|
|
$url = 'http://www.google.com/search?q=respect.github.com';
|
|
```
|
|
|
|
To validate every part of this URL we could use the native `parse_url`
|
|
function to break its parts:
|
|
|
|
```php
|
|
$parts = parse_url($url);
|
|
```
|
|
|
|
This function returns an array containing `scheme`, `host`, `path` and `query`.
|
|
We can validate them this way:
|
|
|
|
```php
|
|
v::arrayVal()
|
|
->key('scheme', v::startsWith('http'))
|
|
->key('host', v::domain())
|
|
->key('path', v::stringType())
|
|
->key('query', v::notBlank());
|
|
```
|
|
|
|
Using `v::call()` you can do this in a single chain:
|
|
|
|
```php
|
|
v::call(
|
|
'parse_url',
|
|
v::arrayVal()
|
|
->key('scheme', v::startsWith('http'))
|
|
->key('host', v::domain())
|
|
->key('path', v::stringType())
|
|
->key('query', v::notBlank())
|
|
)->isValid($url);
|
|
```
|
|
|
|
## Templates
|
|
|
|
### `Call::TEMPLATE_STANDARD`
|
|
|
|
| Mode | Template |
|
|
| ---------- | ---------------------------------------------------------- |
|
|
| `default` | {{input}} must be a suitable argument for {{callable}} |
|
|
| `inverted` | {{input}} must not be a suitable argument for {{callable}} |
|
|
|
|
## Template placeholders
|
|
|
|
| Placeholder | Description |
|
|
| ----------- | ---------------------------------------------------------------- |
|
|
| `callable` | |
|
|
| `input` | |
|
|
| `subject` | The validated input or the custom validator name (if specified). |
|
|
|
|
## Categorization
|
|
|
|
- Callables
|
|
- Nesting
|
|
- Transformations
|
|
|
|
## Changelog
|
|
|
|
| Version | Description |
|
|
| ------: | ----------- |
|
|
| 0.3.9 | Created |
|
|
|
|
---
|
|
|
|
See also:
|
|
|
|
- [Callback](Callback.md)
|
|
- [Each](Each.md)
|
|
- [Lazy](Lazy.md)
|
|
- [Sorted](Sorted.md)
|