Oihana PHP

chainCallables.php

Table of Contents

Functions

chainCallables()  : callable|null
Chains multiple callables to be executed in sequence.

Functions

chainCallables()

Chains multiple callables to be executed in sequence.

chainCallables([array<string|int, mixed> $callables = [] ]) : callable|null

Creates a pipeline where each callable's output becomes the next callable's input. The first callable receives the arguments passed to the chain; subsequent callables receive only the result from the previous callable.

Returns null if the array is empty or if any callable cannot be resolved. Execution stops at the first unresolvable callable in the chain.

Parameters
$callables : array<string|int, mixed> = []

An array of callables to execute in sequence. Each can be a string, array, object, or Closure.

Tags
example
use function oihana\core\callables\chainCallables;

// Example 1: Data transformation pipeline
$pipe = chainCallables([
    'trim',
    'strtoupper',
    fn($str) => str_repeat($str, 2)
]);
echo $pipe('  hello  '); // "HELLOHELLO"

// Example 2: Mathematical operations
$calculate = chainCallables([
    fn($x) => $x * 2,
    fn($x) => $x + 10,
    fn($x) => sqrt($x)
]);
echo $calculate(5); // sqrt((5*2)+10) = sqrt(20) ≈ 4.47

// Example 3: With static methods
$pipe = chainCallables
([
    'MyClass::parse',
    'MyClass::validate',
    'MyClass::transform'
]);
$result = $pipe($input);

// Example 4: With object methods
$handler = new Handler();
$pipe = chainCallables
([
    [ $handler , 'preprocess'  ] ,
    [ $handler , 'process'     ] ,
    [ $handler , 'postprocess' ]
]);
$result = $pipe($data);
see
wrapCallable()

To add decorators around a callable

author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
callable|null

A new callable that executes the chain, or null if invalid


        
On this page

Search results