Skip to main content

ValueClosureMapper

Maps an input value through a user-supplied closure. The closure receives the current value and its return value becomes the new value.

The closure is invoked with the current value as its first argument. It may also type-hint Bindable, BindBoundary or BindContext to receive those context objects. If it returns a n2n\bind\plan\BindData, the value is converted to an array. No validation is performed.

Mappers::valueIfNotNull() behaves identically but skips null values — the closure is not invoked and the value stays null. Mappers::value() always invokes the closure, even for null.

Example: basic transformation​

$result = Bind::values('test')->map(Mappers::value(fn ($v) => strtoupper($v)))->toArray()->exec();
var_dump($result->get());

Output:

array(1) {
[0]=>
string(4) "TEST"
}

Example: valueIfNotNull skips null​

// valueIfNotNull() leaves null untouched; value() would instead invoke the closure with null.
$result = Bind::values('test', null)->map(Mappers::valueIfNotNull(fn ($v) => strtoupper($v)))->toArray()->exec();
var_dump($result->get());

Output:

array(2) {
[0]=>
string(4) "TEST"
[1]=>
NULL
}