Higher order predicates library.
A "higher order function" is a function that either returns a function or takes a function as argument.
A "functional predicate" is a function that receives one or more arguments (subject) and returns
true or false.
This library is a collections of functions that return functional predicates.
In PHP there are some functions like array_map, array_filter, and so on, that take a functional
predicate as argument.
For example:
$data = [
'foo',
1,
true,
'bar',
[],
''
];
$strings = array_filter($data, 'is_string'); // ['foo', 'bar', '']This can be done thanks to the fact that a is_string is a named function.
But if we need something more complex, e.g. we also want to strip empty strings, we need to:
$strings = array_filter($data, function($item) {
return is_string($item) && $item !== '';
});One of the functions of this library is isType() that accepts a string representing a type
and returns a predicate that can be used to check subjects against that type.
Another of the functions of this library is isNotEmpty() that returns a predicate that verifies
non-empty values.
Another function is chain() that takes an arbitrary number of predicates and returns a predicate
that returns true when all the given predicates return true.
Using these 3 functions the code above can be written like this:
$strings = array_filter($data, Hop\chain(Hop\isType('string'), Hop\isNotEmpty()));All the functions in this library are in Hop namespace.
Served by Composer using toobo/hop.
Here a list of all the functions currently provided by library (namespace omitted):
always()Return a predicate that always returntruenever()Return a predicate that always returnfalseisEmpty()isNotEmpty()isTrueish()isFalsey()isBooleanLooking()
is($value)isNot($value)equals($value)notEquals($value)matches(string $regex)notMatches(string $regex)moreThan(int|float $limit)moreThanOrEqual(int|float $limit)lessThan(int|float $limit)lessThanOrEqual(int|float $limit)between(int|float $min, int|float $max)betweenInner(int|float $min, int|float $max)betweenLeft(int|float $min, int|float $max)betweenRight(int|float $min, int|float $max)
isType(string $type)Works with scalar types, classes and interfacesobjectIs(string $classOrInterface)Works with objects
filterVar(int $filter, $options = null)Returns a predicate that appliesfilter_var()to subject using given filter and options.isEmail()isUrl()isIp()isMac()
size(int $size)Verify elements count of arrays and countable objects and string lengthsmallerThan(int $size)smallerThanOrEqual(int $size)biggerThan(int $size)biggerThanOrEqual(int $size)sizeBetween(int $min, int $max)sizeBetweenInner(int $min, int $max)sizeBetweenLeft(int $min, int $max)sizeBetweenRight(int $min, int $max)
contains(string $subString)Verify a string contains a sub-stringstartsWith(string $subString)Verify a string starts with a sub-stringendsWith(string $subString)Verify a string ends with a sub-stringhas($item)Verify an string contains an itemheadIs(...$items)Verify first item(s) of an arraytailIs(...$items)Verify last item(s) of an arrayin(...$items)Verify an item is one of given itemsnotIn(...$items)Verify an item is none of given itemsintersect(...$items)Verify an array has an non-empty intersection with given itemsnotIntersect(...$items)Verify an array has an empty intersection with given items
hasKey(string $key)hasNotKey(string $key)hasAllKeys(string ...$keys)hasAnyOfKeys(string ...$keys)hasNoneOfKeys(string ...$keys)hasNotAllKeys(string ...$keys)valueForKeyIs(string $key, $value)valueForKeyIsNot(string $key, $value)applyOnValueForKey(string $key, callable $callback)
hasMethod(string $method)classHasMethod(string $method)methodReturns(string $method, $value, ...$params)
not(callable $predicate)Negate given predicatechain(callable ...$predicates)Combine predicates in AND modepool(callable ...$predicates)Combine predicates in OR mode
applyAfter(callable $transformation, callable $predicate)Returns a predicate which returns the result of the predicate, after it has been applied the input value, transformed by the$transformationfunction.applyAfterMethod(string $method, callable $predicate)
Hop is open source and released under MIT license. See LICENSE file for more info.