respect-validation/docs/Call.md
Henrique Moody 2fac861aa1
Do not trigger PHP errors in the "Call" rule
This commit make sure that when the callable is executed by the "Call"
rule and PHP triggers an error, the user does not have to deal with it,
and instead the rule will throw "CallException".

Because of the many changes that were made, it didn't make sense to keep
the class "Call" extending the "AbstractRelated" class.

One thing that is a bit problematic with this rule - and with other
rules as well - is that Validation only knows details of a validation
when it fails, because of that we cannot invert the validations that
passed, meaning that the "Not" rule cannot give the proper response to
a validation that passed. This is a know issue that can only be fixed
is we provide a way for Validation do have more granularity control.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-21 01:22:38 +02:00

1.1 KiB

Call

  • Call(callable $callable, Rule $rule)

Validates the return of a callable for a given input.

Consider the following variable:

$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:

$parts = parse_url($url);

This function returns an array containing scheme, host, path and query. We can validate them this way:

v::arrayVal()
    ->key('scheme', v::startsWith('http'))
    ->key('host', v::domain())
    ->key('path', v::stringType())
    ->key('query', v::notEmpty());

Using v::call() you can do this in a single chain:

v::call(
    'parse_url',
     v::arrayVal()->key('scheme', v::startsWith('http'))
        ->key('host',   v::domain())
        ->key('path',   v::stringType())
        ->key('query',  v::notEmpty())
)->validate($url);

Changelog

Version Description
0.3.9 Created

See also: