GeneralFunctions\composeTypeSafe()

higher-order composer variadic returns Closure pure
((T → bool), ...(T → T)) → (T → T | null)
At a glance — Composition with a type gate between every step. Useful when you want stronger guarantees than composeSafe's null-only check — e.g. "every step must still be a string".

Like compose() but guards each intermediate value with a validator callable. If the validator fails on a value, the chain aborts and returns null.

/**
  * @param callable(mixed):bool $validator
  * @param callable(mixed):mixed ...$callables
  * @return Closure(mixed):mixed
  */
GeneralFunctions\composeTypeSafe(callable $validator, callable ...$callables): Closure

Returned Closure

When GeneralFunctions\composeTypeSafe() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param mixed $value
  * @return mixed|null
  */
$function ($value)

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$stringPipeline = GeneralFunctions\composeTypeSafe(
  'is_string',
  'trim',
  'strtolower',
  Strings\replaceWith(' ', '-')
);

echo $stringPipeline('  Hello World '); // 'hello-world'

var_dump($stringPipeline(42)); // NULL — 42 is not a string, chain halted

Details

General Functions

Releated Composition Functions