Download the PHP package toobo/hop without Composer
On this page you can find all versions of the php package toobo/hop. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package hop
Short Description Higher-order functional predicates.
License MIT
Homepage https://github.com/Toobo/Hop
Informations about the package hop
Hop
Higher order predicates library.
What?
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.
Why?
In PHP there are some functions like array_map
, array_filter
, and so on, that take a functional
predicate as argument.
For example:
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:
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:
All the functions in this library are in Hop
namespace.
Installation
Served by Composer using toobo/hop
.
List of functions
Here a list of all the functions currently provided by library (namespace omitted):
General
always()
Return a predicate that always returntrue
never()
Return a predicate that always returnfalse
isEmpty()
isNotEmpty()
isTrueish()
isFalsey()
isBooleanLooking()
Comparison
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)
Type check
isType(string $type)
Works with scalar types, classes and interfacesobjectIs(string $classOrInterface)
Works with objects
Var filtering check
filterVar(int $filter, $options = null)
Returns a predicate that appliesfilter_var()
to subject using given filter and options.isEmail()
isUrl()
isIp()
isMac()
Size check
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)
Elements check (for arrays and strings)
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
Array keys
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)
Object methods
hasMethod(string $method)
classHasMethod(string $method)
methodReturns(string $method, $value, ...$params)
Predicates composition
not(callable $predicate)
Negate given predicatechain(callable ...$predicates)
Combine predicates in AND modepool(callable ...$predicates)
Combine predicates in OR mode
Misc
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$transformation
function.applyAfterMethod(string $method, callable $predicate)
License
Hop is open source and released under MIT license. See LICENSE file for more info.