Conditions
Inspired heavily by Laravel's Collections, the when()
and unless()
methods give you the possibility to invoke callbacks, when a condition evaluates to true
.
When
when()
invokes a callback, if it's first argument evaluates to true
.
use Aedart\Contracts\Http\Clients\Requests\Builder;
$builder = $client
->when(true, function(Builder $builder){
$builder->where('name', 'john');
});
If the first argument evaluate to false
, then the method will invoke it's "otherwise" callback, if one is provided.
use Aedart\Contracts\Http\Clients\Requests\Builder;
$builder = $client
->when(false, function(Builder $builder){
// Not invoked...
$builder->where('name', 'John');
}, function(Builder $builder){
// Invoked...
$builder->where('name', 'Simon');
});
Unless
The inverse method, unless()
, will invoke it's callback if the first argument is not true
.
use Aedart\Contracts\Http\Clients\Requests\Builder;
$builder = $client
->unless(false, function(Builder $builder){
$builder->where('name', 'john');
});
Consequently, if the first argument is true
, then it's "otherwise" callback is invoked, when provided.
use Aedart\Contracts\Http\Clients\Requests\Builder;
$builder = $client
->unless(true, function(Builder $builder){
// Not invoked...
$builder->where('name', 'John');
}, function(Builder $builder){
// Invoked...
$builder->where('name', 'Simon');
});