Oihana PHP

conditions.php

Table of Contents

Functions

conditions()  : array<string|int, mixed>
Normalizes conditions into an array of callable functions.

Functions

conditions()

Normalizes conditions into an array of callable functions.

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

This function takes a variety of input types (null, callable, or array of callables) and returns a normalized array of callable conditions. It is useful for validating or filtering data based on multiple conditions.

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

The conditions to normalize. Can be:

  • null: defaults to a condition that checks for null values.
  • callable: a single condition function.
  • array: an array of condition functions.
$throwable : bool = false

If true, throws exceptions for invalid conditions. If false, silently filters out invalid conditions.

Tags
example

Basic usage with default condition

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

Usage with a single callable condition

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

Usage with an array of conditions

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

Usage with non-callable in array and throwable=false

$conditions = conditions([fn($value) => true, 'not_callable'], false);
// Returns: [fn($value) => true]
author

Marc Alcaraz (ekameleon)

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

An array of callable conditions.


        
On this page

Search results