Compose

Compose ( array $functions )

Chains functions together, using the output of each function as input to the next. Output is the output of the last function in the chain.

Parameters

functions
Array of functions that should be chained. If the array is multidimensional and/or contains another Compose it will be flattened.

Examples

Example #1

Basic usage.

<?php
use Webbhuset\Pipeline\Constructor as F;

$compose = F::Compose([
    F::Map('trim'),
    F::Filter('is_numeric'),
    F::Map('intval'),
]);

$input = ['1', '  23 ', 'hello', '4.444', 5.75, '+12e3'];

echo json_encode(iterator_to_array($compose($input)));

// Output: [1,23,4,5,12000]

Example #2

Demonstrating how multidimensional arrays and other Composes are flattened.

<?php
use Webbhuset\Pipeline\Constructor as F;

function getMapFunction()
{
    return [
        F::Map('trim'),
        F::Map('ucwords'),
    ];
}

$compose = F::Compose([
    getMapFunction(),
    F::Compose([
        F::Filter('is_numeric')
    ]),
    [],
    [
        [
            F::Map('intval'),
        ],
    ],
]);

/**
 * Result function would look like this:
 *  [
 *       F::Map('trim'),
 *       F::Map('ucwords'),
 *       F::Filter('is_numeric'),
 *       F::Map('intval'),
 *  ]
 */

See Also

  • Fork - Send every input value to multiple functions.