Skip to main content

rename

Rewrites the keys of the props map. Mappers::rename($propsMap) takes an ['old' => 'new', …] map and, for each listed prop that is present, rewrites its bindable's name to the new one. Props not in the map keep their names; old names that do not exist as bindables are silently ignored.

$result = Bind::attrs(['a' => 'A', 'b' => 'B'])
->optProps(['a', 'b'], Mappers::rename(['a' => 'alpha', 'b' => 'beta']))
->toArray()
->exec();
var_dump($result->get());

Output:

array(2) {
["alpha"]=>
string(1) "A"
["beta"]=>
string(1) "B"
}

Because rename operates on the props map (it is a thin wrapper around propsClosure), it composes with the rest of the chain — put further mappers after it to transform the values under their new names. When two old names map to the same new name, the last one written wins; entries whose old key is absent from the input are skipped, so the map can safely list more renames than there are props.