Predicate is a simple library which aims to make using PHP's array_map
and
array_filter
functions a more pleasent experience - and resulting in cleaner
code.
Predicate is a collection of helper functions for common filter and transform actions.
Take this code:
$customers = array_filter(
$allUsers,
function ($user) {
return $user->getType() === 'customer';
}
);
Using predicate this looks like this:
$customer = array_filter($allUsers, Filter::hasMethodReturning('getType', 'customer'));
Using composer:
composer require tomphp/predicate
All filters are defined as static methods on TomPHP\Predicate\Filter
.
So far the following filters are provided:
isNull()
notNull()
isSameAs($expected)
notSameAs($expected)
isLike($expected)
notLike($expected)
hasMethodReturning(string $methodName, $expected, bool strict = true)
notHasMethodReturning(string $methodName, $expected, bool strict = true)
By convention: all functions starting with is
are negated by replacing the
pis
with not
, and all function staring with has
are negated prefixing
not
.
Transforms are for use with array_map
.
Transforms are used to replace code like this:
$names = array_map(
function ($user) {
return $user->getName();
},
$allUsers
);
With code like this:
$names = array_map(Transform::callMethod('getName'), $allUsers);
Transform::classMethod('getName');
// Generates:
function ($object) {
return $object->getName;
}
Transform::getEntry('name');
// Generates:
function ($array) {
return $array['name'];
}
Transform::getEntry(['user', 'name']);
// Generates:
function ($array) {
return $array['user']['name'];
}
Transform::getEntry('strtolower');
// Generates:
function ($value) {
return strtolower($value);
}
$callable
can be any of the following:
'functionName'
function ($value) { /* ... */ }
[$object, 'methodName']
['ClassName', 'staticMethodName']