PHPackages                             abivia/criteria - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. abivia/criteria

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

abivia/criteria
===============

A simple logical expression processor with regex capability.

1.3.1(1y ago)127MITPHPPHP ^8.2

Since Dec 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/abivia/criteria)[ Packagist](https://packagist.org/packages/abivia/criteria)[ RSS](/packages/abivia-criteria/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Abivia\\Criteria
================

[](#abiviacriteria)

This is a simple package for evaluating boolean logic in a data structure.

The entry point is `Criteria::evaluate(array $criteria, callable $accessor)`.

`$criteria` is an array of terms. If any of the terms evaluates as true, `evaluate` returns. This is an implicit "or" operation.

Terms
-----

[](#terms)

A term is an array with these elements:

- `arg` - the name of a value to be retrieved via the accessor callback.
- `op` - A comparison operator, see below.
- `value` or `var` - Required for binary operators (i.e. those other than null, !null), the value to be compared or the regular expression to be matched. The `value` can be an array. If an array value is provided for a binary operator (other than "in" or "includes"), each value is compared and the first `true` result causes the expression to be true. If `var` is provided, it must be a string. This is passed to the accessor callback to retrieve a value from the application.
- `name` - An optional name, used when reporting errors.
- `invert` - An optional boolean. When true, the result of the current (sub-)criteria is inverted.
- `and` - An optional list of sub-criteria. The list is evaluated as a series of logical "or" conjunctions, then a logical "and" is performed against the result of the argument/value operation.
- `xor` - Similar to the `and` element instead the result is exclusive or-ed with the result.

Element names are configurable to suit the application. See "property configuration" below.

Operators
---------

[](#operators)

The operator is one of ==, ===, !=, !==, &gt;, &gt;=, &lt;, &lt;=, contains, !contains, has, !has, in, !in, includes, !includes, null, !null, regex, !regex. If no operator is specified, then == is assumed.

- The "contains" operator is true if `var`/`value` is a substring of `arg`. This test is case-sensitive.
- The "has" operator performs an intersection operation. Both `arg` and `var`/`value` are treated as arrays. If one element in `arg` can be found in the `var`/`value` the result is true.
- The "in" operator is true if `arg` can be found in `var`/`value`, which normally is an array. If both `arg` and `var`/`value` are scalar, "in" is equivalent to ==. If `arg` is an array, then the expression is true if all elements in `arg` can be found in `var`/`value`.
- The "includes" operator is true if `var`/`value` can be found in `arg`, which normally is an array. If both `arg` and `var`/`value` are scalar, "in" is equivalent to ==. If both `arg` and `var`/`value` are arrays, then the expression is true if all elements in `var`/`value`can be found in `arg`.
- The "null" operator is unary, any `var`/`value` is ignored.
- The "regex" operator expects a PHP regular expression.
- A ! in front of "contains", "in", "includes", and "regex" will invert the result of the test.

Examples
--------

[](#examples)

Criteria are specified in JSON for readability.

```
[
  {
    "arg": "language",
    "op": "==",
    "value": "en"
  }
]
```

Will evaluate true if the value of `acessor('language')` is "en".

```
[
  {
    "arg": "firstName",
    "var": "lastName"
  }
]
```

This assumes the operator is == and Will evaluate true if `acessor('firstName')` is equal to `acessor('lastName')`.

```
[
  {
    "arg": "firstName",
    "op": "==",
    "var": "lastName"
  },
  {
    "arg": "firstName",
    "op": "==",
    "var": ""
  }
]
```

Will evaluate true if `acessor('firstName')` is equal to `acessor('lastName')`or if `acessor('firstName')` is an empty string.

```
[
  {
    "arg": "province",
    "op": "==",
    "value": "ON",
    "and": [
      {
        "arg": "areaCode",
        "op": "in",
        "value": [
          "226",
          "249",
          "289",
          "343",
          "365",
          "416",
          "437",
          "519",
          "548",
          "613",
          "647",
          "705",
          "807",
          "905"
        ]
      }
    ]
  }
]
```

Will evaluate true if `acessor('province')` is "ON" and `accessor('areaCode')` matches one of the values in the list.

Operator Access Control
-----------------------

[](#operator-access-control)

If your application is processing user-generated data and you want to restrict access to some operators, this is possible with the `operatorState` option.

```
$criteria = new \Abivia\Criteria\Criteria(
   ['operatorState' => ['regex' => false, '!regex' => false]]
);
```

This will disable use of the regular expression operators.

Property Configuration
----------------------

[](#property-configuration)

Property names used in the criteria can be changed at instantiation by passing an array of replacements. Only the passed in properties will be modified.

```
$criteria = new \Abivia\Criteria\Criteria(
   ['overrides' => ['arg' => 'left', 'op' => 'operator', 'value' => 'right']]
);
```

Would change a valid input to:

```
[
  {
    "left": "firstName",
    "operator": "==",
    "right": "lastName",
    "invert": true
  }
]
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance47

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.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 ~37 days

Total

4

Last Release

397d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/42cd72dce530e94e60853407cdab69ead232423272b482218ae686c740ce0e99?d=identicon)[abivia](/maintainers/abivia)

---

Top Contributors

[![instancezero](https://avatars.githubusercontent.com/u/2599327?v=4)](https://github.com/instancezero "instancezero (11 commits)")[![abivia](https://avatars.githubusercontent.com/u/94868022?v=4)](https://github.com/abivia "abivia (1 commits)")

---

Tags

expressionregexlogic

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/abivia-criteria/health.svg)

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

###  Alternatives

[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[composer/pcre

PCRE wrapping library that offers type-safe preg\_\* replacements.

693313.8M34](/packages/composer-pcre)[lorisleiva/cron-translator

Makes CRON expressions human-readable

3148.5M31](/packages/lorisleiva-cron-translator)[gherkins/regexpbuilderphp

PHP port of thebinarysearchtree/regexpbuilderjs

1.4k163.0k1](/packages/gherkins-regexpbuilderphp)[unclecheese/display-logic

Allows assignment of conditions for display and hide of specific form fields based on client side behavior.

771.0M101](/packages/unclecheese-display-logic)[cytopia/awesome-ci

Lot's of tools for git, file and static source code analysis.

3386.4k](/packages/cytopia-awesome-ci)

PHPackages © 2026

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