InvalidArgumentException on non-number input.
Creates a predicate Closure that returns true when the passed value is a whole multiple of the pre-defined number. Zero is treated as a non-multiple and always returns false — that is a deliberate behaviour of this constructor.
/**
* @param int|float $multiple The fixed multiple to test against.
* @return Closure(int|float):bool
* @throws InvalidArgumentException If $multiple is not int or float.
*/
Numbers\isMultipleOf($multiple): ClosureWhen Numbers\isMultipleOf() is called, it returns the following Closure which can be used like a regular function.
/**
* @param int|float $value
* @return bool True when ($value % $multiple === 0) and $value !== 0.
* @throws InvalidArgumentException If $value is not int or float.
*/
$function (int|float $value): boolThis can be used to create a simple closure which can be used as a regular function.
// Create a predicate that checks for multiples of 3.
$isMultipleOf3 = Numbers\isMultipleOf(3);
// Called as a function.
var_dump($isMultipleOf3(12)); // true
var_dump($isMultipleOf3(13)); // false
var_dump($isMultipleOf3(0)); // false — zero is never a multiple here
// Used in a higher order function.
$onlyMultiples = array_filter([1, 3, 5, 6, 7, 9, 11, 12], $isMultipleOf3);
print_r($onlyMultiples); // [3, 6, 9, 12]This can be called inline using currying.
var_dump(Numbers\isMultipleOf(2)(12)); // trueIf you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.
$evens = array_filter([1, 2, 3, 4, 5, 6], Numbers\isMultipleOf(2));
print_r($evens); // [2, 4, 6]