BindableClosureMapper
Runs a user-supplied closure for each bindable, passing the Bindable itself rather than just its value, so it can be manipulated directly (e.g. setValue(), setExist(), addError()).
The closure receives the Bindable as its first argument and may type-hint BindBoundary or BindContext for those context objects. It must return null, true or a n2n\bind\mapper\MapResult to continue, or false to abort the whole mapping (the result becomes invalid). Take care not to return the value of setValue() — use a closure body with no return, or return null/true explicitly. No validation is performed.
Where
Mappers::value()receives the value and returns the new value,Mappers::bindable()receives theBindableand mutates it directly.
Mappers::bindableIfNotNull()behaves identically but skips bindables whose value isnull.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| $closure | Closure | — | Receives the Bindable; manipulates it directly. |
| $nonExistingSkipped | bool | true | Skip bindables that do not exist (!doesExist()). |
| $dirtySkipped | bool | true | Skip bindables that are dirty (isDirty()). |
Example: basic transformation
$result = Bind::values('test')->map(Mappers::bindable(function (Bindable $b) {
$b->setValue(strtoupper($b->getValue()));
}))->toArray()->exec();
var_dump($result->get());
Output:
array(1) {
[0]=>
string(4) "TEST"
}
Example: bindableIfNotNull skips null
// bindableIfNotNull() leaves null bindables untouched.
$result = Bind::values('test', null)->map(Mappers::bindableIfNotNull(function (Bindable $b) {
$b->setValue(strtoupper($b->getValue()));
}))->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
string(4) "TEST"
[1]=>
NULL
}