Skip to main content

ValueAsBindDataClosureMapper

A single-bindable mapper for values that are themselves arrays: wraps the bindable's array value in a BindData, passes it to a closure, and writes the closure's return value back onto the same bindable. A null value short-circuits and the closure is not called.

The closure receives a BindData (so you can use typed accessors like reqString('key')) and may return any value — typically an array.

This is the single-bindable counterpart of Mappers::propsAsBindDataClosure() (see PropsClosureMapper), which operates on multiple props keyed by prop name. Here the BindData is keyed by the single bindable's array keys.

Example: transforming a sub-array​

$result = Bind::attrs(['prop' => ['key1' => 'value1', 'key2' => 'value2']])
->prop('prop', Mappers::valueAsBindDataClosure(function (BindData $bindData) {
return ['key1' => 'holeradio1', 'key2' => 'holeradio2'];
}))
->toArray()
->exec();
var_dump($result->get());

Output:

array(1) {
["prop"]=>
array(2) {
["key1"]=>
string(10) "holeradio1"
["key2"]=>
string(10) "holeradio2"
}
}