Multiplex¶
Multiplex ( callable $callback , array $functions )
Sends every input value to one of the inner functions based on the result of the callback function. Output is the output of the inner functions.
Parameters¶
- callback
scalar callback ( mixed $value )
A callback that returns the key of the function to which the value should be passed.
Examples¶
Example #1¶
Basic usage example.
<?php
use Webbhuset\Pipeline\Constructor as F;
$multiplex = F::Multiplex(
function ($value) {
return $value % 2 == 0 ? 'even' : 'odd';
},
[
'even' => F::Map(function ($value) {
return $value / 2;
}),
'odd' => F::Map(function ($value) {
return $value * 2;
}),
]
);
$input = [1, 2, 3, 4, 5, 6];
echo json_encode(iterator_to_array($multiplex($input)));
// Output: [2,1,6,2,10,3]
Example #2¶
By leaving a branch empty we can run functions for only some values.
<?php
use Webbhuset\Pipeline\Constructor as F;
$multiplex = F::Multiplex(
function ($value) {
return $value <= 10;
},
[
true => F::Map(function ($value) {
return $value * 2;
}),
false => [],
]
);
$input = [1, 22, 3, 44, 5];
echo json_encode(iterator_to_array($multiplex($input)));
// Output: [2,22,6,44,10]