PHPackages                             gmazzap/pentothal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. gmazzap/pentothal

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

gmazzap/pentothal
=================

Higher-order functional predicates.

1.1.0(10y ago)79241MITPHPPHP &gt;=5.5

Since Jan 11Pushed 10y ago2 watchersCompare

[ Source](https://github.com/gmazzap/Pentothal)[ Packagist](https://packagist.org/packages/gmazzap/pentothal)[ Docs](https://github.com/Giuseppe-Mazzapica/Pentothal)[ RSS](/packages/gmazzap-pentothal/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

Pentothal
=========

[](#pentothal)

---

[![travis-ci status](https://camo.githubusercontent.com/e360bb379cf60aef19ec66a0a4abd0c1683b36fc9ea1b4368bf47f255062441c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f47697573657070652d4d617a7a61706963612f50656e746f7468616c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Giuseppe-Mazzapica/Pentothal)[![codecov.io](https://camo.githubusercontent.com/8fe2a7ffe34556bc9324030b1b7f441c93be04e48cbabe20cd62169bc40caee6/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f47697573657070652d4d617a7a61706963612f50656e746f7468616c2e7376673f7374796c653d666c61742d737175617265)](http://codecov.io/github/Giuseppe-Mazzapica/Pentothal?branch=master)[![license](https://camo.githubusercontent.com/d604197ccc77cd3853f68049533113c691b4f342559bf6ed8117cefb8a36d80f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f676d617a7a61702f70656e746f7468616c2e7376673f7374796c653d666c61742d737175617265)](http://opensource.org/licenses/MIT)[![release](https://camo.githubusercontent.com/4b4158ac00b27a4da32a5a96d9765b82c875d6749491264176f0eb0b5223c203/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f47697573657070652d4d617a7a61706963612f50656e746f7468616c2e7376673f7374796c653d666c61742d737175617265)](https://github.com/Giuseppe-Mazzapica/Pentothal/releases/latest)

---

> Higher order predicates library.

---

What?
=====

[](#what)

An "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?
====

[](#why)

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 typoe.

Another of the functions of this library is `isNotEmpty()` that returns a predicate that verifies non-empty values.

Another function is `combine()` that takes an arbitrary number of predicates and returns a predicate that returns `true` when all the combined predicates return true.

Using these 3 functions the code above can be written like this:

```
use Pentothal as P;

$strings = array_filter($data, P\combine(P\isType('string'), P\isNotEmpty()));
```

All the functions in this library are in `Pentothal` namespace.

Installation
============

[](#installation)

Served by Composer using `gmazzap/pentothal`.

List of functions
=================

[](#list-of-functions)

Here a list of all the functions currently provided by library (namespace omitted):

### General

[](#general)

- `always()` Return a predicate that always return `true`
- `never()` Return a predicate that always return `false`
- `isEmpty()`
- `isNotEmpty()`

### Comparison

[](#comparison)

- `isSame($value)`
- `isNotSame($value)`
- `isEqual($value)`
- `isNotEqual($value)`
- `match(string $regex)`
- `notMatch(string $regex)`

### Type check

[](#type-check)

- `isType(string $type)` Works with scalar types, classes and interfaces
- `isNotType(string $type)`
- `isInt()`
- `isNotInt()`
- `isFloat()`
- `isNotFloat()`
- `isNumber()`
- `isNotNumber()`
- `isString()`
- `isNotString()`
- `isBool()`
- `isNotBool()`
- `isNull()`
- `isNotNull()`
- `isObject()`
- `isNotObject()`
- `isArray()`
- `isNotArray()`

### Var filtering check

[](#var-filtering-check)

- `filterVar(int $filter, $options = null)` Returns a predicate that applies `filter_var()` to subject using given filter and options.
- `isEmail()`
- `isNotEmail()`
- `isUrl()`
- `isNotUrl()`
- `isIp()`
- `isNotIp()`
- `isMac()`
- `isNotMac()`

### Size check

[](#size-check)

- `size(int $size)` Verify elements count of arrays and countable objects and string length
- `sizeMax(int $size)`
- `sizeMaxStrict(int $size)`
- `sizeMin(int $size)`
- `sizeMinStrict(int $size)`
- `between(int $min, int $max)`
- `notBetween(int $min, int $max)`
- `betweenInner(int $min, int $max)`
- `notBetweenInner(int $min, int $max)`
- `betweenLeft(int $min, int $max)`
- `notBetweenLeft(int $min, int $max)`
- `betweenRight(int $min, int $max)`
- `notBetweenRight(int $min, int $max)`

### Elements check (for arrays and strings)

[](#elements-check-for-arrays-and-strings)

- `contain($item)` Verify `$item` is present in arrays, or if both `$item` and subject are strings check the subject contain `$item`
- `notContain($item)`
- `startWith($item)` Verify first element of arrays, or if both `$item` and subject are strings check the subject string starts with `$item`
- `notStartWith($item)` Verify first element of arrays, or if both `$item` and subject are strings check the subject string starts with `$item`
- `endWith($item)`
- `notEndWith($item)`

### Elements check for arrays only

[](#elements-check-for-arrays-only)

- `anyOfValues(array $values)` Verify array subject for intersection with given values
- `notAnyOfValues(array $values)`
- `anyOf(...$values)` Like `anyOfValues` but with variadic arguments
- `notAnyOf(...$values)`

### Elements check for arrays and objects

[](#elements-check-for-arrays-and-objects)

- `hasValue($value)` Return a predicate that checks an array or a traversable object to find an element equal to value given
- `hasNotValue($value)`
- `hasValues(array $value)`
- `hasNotValues(array $values)`
- `hasAnyOfValues(array $values)`
- `hasNotAnyOfValues(array $values)`

### Object properties check (works with associative arrays as well)

[](#object-properties-check-works-with-associative-arrays-as-well)

- `hasKey(string $key)`
- `hasKeys(array $keys)`
- `hasNotKeys(array $keys)`
- `hasAnyOfKeys(array $keys)`
- `hasNotAnyOfKeys(array $keys)`
- `keyIs(string $key, $value)`
- `keyIsNot(string $key, $value)`
- `keyIsAnyOf(string $key, array values)`
- `keyIsNotAnyOf(string $key, array $values)`
- `keyIsType(string $key, string $type)`
- `keyInNotType(string $key, string $type)`
- `keyApply(string $key, callable $predicate)` Return a predicate that applies a predicate to a key of the subject

### Object methods check

[](#object-methods-check)

- `hasMethod(string $method)`
- `hasNotMethod(string $method)`
- `methodReturn(string $method, $value, array $methodArgs = [])`
- `methodNotReturn(string $method, $value, array $methodArgs = [])`
- `methodReturnAnyOf(string $method, array $values, array $methodArgs = [])`
- `methodNotReturnAnyOf(string $method, array $values, array $methodArgs = [])`
- `methodReturnType(string $method, string $type, array $methodArgs = [])`
- `methodNotReturnType(string $method, string $type, array $methodArgs = [])`
- `methodReturnEmpty(string $method, array $methodArgs = [])`
- `methodReturnNotEmpty(string $method, array $methodArgs = [])`
- `methodReturnApply(string $method, callable $predicate, array $methodArgs = [])` Return a predicate that applies a predicate return value of given method of the subject

### Bulk

[](#bulk)

- `bulk(callable $predicate)` Return a predicate that applies the same predicate to an array of values and returns true when the given predicate return true for all values
- `bulkPool(callable $predicate)` Like `bulk()` but returned predicate return `true` the given predicate returns `true` for any of the values

### Predicates composition

[](#predicates-composition)

- `negate(callable $predicate)` Return a predicate that return `true` when given predicate return `false` and viceversa
- `combine(...$predicates)` Return a predicate that returns `true` when all the given predicates returns true
- `pool(...$predicates)` Return a predicate that returns `true` when any of the given predicates returns true
- `combineCallbacks(array $predicates)` Like `combine()` but accepts an array of predicates
- `poolCallbacks(...$predicates)` Like `pool()` but accepts an array of predicates
- `combineMap(array $predicates)` Takes a map of predicates and return a predicates that applies to a map value, returns true when all the predicates return true
- `poolMap(array $predicates)` Like combineMap, but the returned predicates return `true` when any the predicates returns `true`

### Transforming a value before applying the predicate

[](#transforming-a-value-before-applying-the-predicate)

- `applyAfter(callable $transformation, callable $predicate)` Returns a predicate which returns the result of the predicate, after it has been applied the the input value, transformed by the `$transformation` function.

#### Example

[](#example)

```
use Pentothal as P;

$data = ['yes', 'no', 'Yes', 'No', 'YES', 'NO' ];

$yes = array_filter($data, P\applyAfter('strtolower', isSame('yes'))); // ['yes', 'Yes', 'YES']
```

Quite complex example
=====================

[](#quite-complex-example)

```
use Pentothal as P;

// some example data
$countableOne = new \ArrayObject(['foo' => 'bar']);
$countableTwo = new \ArrayObject(['a' => 'a', 'b' => 'b']);
$plainObj = new \stdClass();
$string1 = 'a';
$string3 = 'abc';
$number1 = 1;
$number3 = 3;

$list = [
  'a' => $countableOne,
  'b' => $countableTwo,
  'c' => $plainObj,
  'd' => $string1,
  'e' => $string3,
  'f' => $number1,
  'g' => $number3
];

$predicate = P\combine(
  P\pool(P\isNotObject(), P\isType('Countable')), // filter out: ['c' => $plainObj]
  P\pool(P\isNotString(), P\size(3)), // filter out: ['d' => $string1]
  P\pool(P\isString(), P\size(1)) // filter out: ['a' => $countableTwo, 'g' => $number3]
);

$negatePredicate = negate($predicate);

$in = array_filter($list, $predicate);
$out = array_filter($list, $negatePredicate);

var_dump($in); // array('a' => $countableOne, 'e' => $string3, 'f' => $number1];
var_dump($out); // array('b' => $countableTwo, 'c' => $plainObj, 'd' => $string1, 'g' => $number3];
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

5

Last Release

3766d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.4

1.0.1PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2208282?v=4)[Giuseppe Mazzapica](/maintainers/gmazzap)[@gmazzap](https://github.com/gmazzap)

---

Top Contributors

[![gmazzap](https://avatars.githubusercontent.com/u/2208282?v=4)](https://github.com/gmazzap "gmazzap (75 commits)")[![tomphp](https://avatars.githubusercontent.com/u/1959183?v=4)](https://github.com/tomphp "tomphp (1 commits)")

---

Tags

validationassertionsfunctionfilteringfunctional-programmingpredicates

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gmazzap-pentothal/health.svg)

```
[![Health](https://phpackages.com/badges/gmazzap-pentothal/health.svg)](https://phpackages.com/packages/gmazzap-pentothal)
```

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k489.6M670](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k148.7M414](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M379](/packages/respect-validation)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M106](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

64236.9M184](/packages/opis-json-schema)[giggsey/libphonenumber-for-php-lite

A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber

8412.9M47](/packages/giggsey-libphonenumber-for-php-lite)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
