SubMergeMapper / SubMergeToObjectMapper
Reassemble descendant bindables back into the current bindable, then remove the descendants. They are the inverse of Mappers::subProps() / Mappers::subForeach() / Mappers::valueToSubValues(), which split a value into children.
Mappers::subMerge()merges the descendant values (keyed by the path slice relative to the current bindable) into an array and sets it as the current bindable's value.Mappers::subMergeToObject($closure)does the same but writes the descendant values onto a real object. The closure returns the object instance to populate; the descendant bindables are written onto its properties.
Example: merging descendants into an array
$result = Bind::attrs(['sub' => ['sub2' => ['huii1' => 'foobar', 'huii2' => 'foobar2']]])
->toValue($v)
->props(['sub/sub2/huii1', 'sub/sub2/huii2'])
->root(Mappers::subMerge())
->exec();
var_dump($result->get());
Output:
array(1) {
["sub"]=>
array(1) {
["sub2"]=>
array(2) {
["huii1"]=>
string(6) "foobar"
["huii2"]=>
string(7) "foobar2"
}
}
}
Example: merging descendants into an object
$targetObj = new MergeTestClass();
$targetValue = null;
Bind::attrs(['string' => 'test', 'int' => 123, 'array' => [2, 3]])
->toValue($targetValue)
->props(['string', 'int', 'array'])
->root(Mappers::subMergeToObject(fn () => $targetObj))
->exec();
var_dump($targetValue);
The descendant values are written onto $targetObj's properties (string = 'test', int = '123' coerced to the property type, array = [2, 3]).
subMergeToObjectis the final step inObjectUnmarshalTrait'sunmarshal()pipeline:subPropsForClassconverts each child, thensubMergeToObjectassembles them into a fresh instance of the class. See UnmarshalMapper.