* * Full license available in separate LICENSE file */ namespace Kadet\Functional\Predicates; use function Kadet\Functional\predicate; use Kadet\Functional\Predicate\ClosurePredicate; /** * @param string $method * @param $predicate * @param array $args * * @return \Kadet\Functional\Predicate */ function method(string $method, $predicate, ...$args) { $predicate = predicate($predicate); return new ClosurePredicate(function ($object) use ($method, $predicate, $args) { return $predicate($object->{$method}($args)); }); } /** * @param string $property * @param $predicate * * @return \Kadet\Functional\Predicate */ function property(string $property, $predicate) { $predicate = predicate($predicate); return new ClosurePredicate(function ($object) use ($property, $predicate) { if (isset($object->$property)) { return $predicate($object->$property); } elseif (method_exists($object, $getter = 'get'.ucfirst($property))) { return $predicate($object->$getter()); } else { return false; } }); } function instance(string $class) { return new ClosurePredicate(function ($value) use ($class) { return $value instanceof $class; }); }