Oihana PHP

helpers

Table of Contents

Functions

conditions()  : array<string|int, callable>
Normalize a set of conditions into an array of callable functions.

Functions

conditions()

Normalize a set of conditions into an array of callable functions.

conditions([array<string|int, mixed>|callable|string|null $conditions = null ][, bool $throwable = false ]) : array<string|int, callable>

This helper accepts multiple forms of input (null, single callable, array of callables) and returns a consistent array of callable conditions. It is useful when you want to filter, validate, or compress data using multiple dynamic rules.

Parameters
$conditions : array<string|int, mixed>|callable|string|null = null

The conditions to normalize. Can be:

  • null: defaults to a single condition that checks for null values.
  • callable: a single condition function.
  • array: an array of callable functions.
  • string: (optional) a single callable function name or expression.
$throwable : bool = false

If true, invalid conditions will throw an InvalidArgumentException. If false, invalid conditions are silently ignored.

Tags
throws
InvalidArgumentException

If $throwable=true and any provided condition is invalid.

example

Default condition (null)

$conditions = conditions();
// Returns: [fn($value) => is_null($value)]
example

Single callable condition

$conditions = conditions(fn($v) => $v === '');
// Returns: [fn($v) => $v === '']
example

Array of conditions

$conditions = conditions([
    fn($v) => $v === null,
    fn($v) => $v === false
]);
// Returns: [fn($v) => $v === null, fn($v) => $v === false]
example

Non-callable in array with throwable=false

$conditions = conditions([fn($v) => true, 'not_callable'], false);
// Returns: [fn($v) => true], 'not_callable' is ignored
example

Invalid condition with throwable=true

// Throws InvalidArgumentException
$conditions = conditions('not_a_callable', true);
example

Mixing callables and null

$conditions = conditions([null, fn($v) => is_numeric($v)], false);
// Returns: [fn($v) => is_numeric($v)]
author

Marc Alcaraz

since
1.0.0
Return values
array<string|int, callable>

Array of callable conditions.


        
On this page

Search results