Skip to main content

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 the Bindable and mutates it directly.

Mappers::bindableIfNotNull() behaves identically but skips bindables whose value is null.

Parameters​

ParameterTypeDefaultDescription
$closureClosure—Receives the Bindable; manipulates it directly.
$nonExistingSkippedbooltrueSkip bindables that do not exist (!doesExist()).
$dirtySkippedbooltrueSkip 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
}