PHPackages                             symftony/xpression - 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. symftony/xpression

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

symftony/xpression
==================

Xpression is a simple PHP implementation of Specification pattern

181.8k[1 PRs](https://github.com/Symftony/Xpression/pulls)

Since Apr 22Compare

[ Source](https://github.com/Symftony/Xpression)[ Packagist](https://packagist.org/packages/symftony/xpression)[ RSS](/packages/symftony-xpression/feed)WikiDiscussions Synced 1w ago

READMEChangelogDependenciesVersions (3)Used By (0)

Xpression
=========

[](#xpression)

![Codecov](https://camo.githubusercontent.com/a3d830e9c1f3dddee5607fdf317c60785af9f7027a6f92013aee96f6f45a0ab9/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f53796d66746f6e792f587072657373696f6e2e7376673f7374796c653d666c6174266d61784167653d3630)![Travis branch](https://camo.githubusercontent.com/926546a7c90f48de027cc4f9a7be16b994d65a926cfd43b08905e30bf8ee8547/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f53796d66746f6e792f587072657373696f6e2e7376673f7374796c653d666c6174266d61784167653d3630)![Scrutinizer](https://camo.githubusercontent.com/06bfca96bafcd6f1fa7adeb061942875c4dbed8d078ae7ee114c440b3d853e46/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f53796d66746f6e792f587072657373696f6e2e7376673f7374796c653d666c6174266d61784167653d3630)[![Latest Unstable Version](https://camo.githubusercontent.com/935d56a8c6d272cea6c5d4389baaaeed706ea2a321bab086e3c2cda38d47d29e/68747470733a2f2f706f7365722e707567782e6f72672f73796d66746f6e792f787072657373696f6e2f762f756e737461626c65)](https://packagist.org/packages/symftony/xpression)[![Latest Stable Version](https://camo.githubusercontent.com/bb2830197acec07c3f27bd85560fa9a08b729c5e4ce77a8ce1b14f5385b92df2/68747470733a2f2f706f7365722e707567782e6f72672f73796d66746f6e792f787072657373696f6e2f762f737461626c65)](https://packagist.org/packages/symftony/xpression)[![Total Downloads](https://camo.githubusercontent.com/86a67e10cd7f36ea14669908143506cead39196c15740b2a420ced14110e3a1c/68747470733a2f2f706f7365722e707567782e6f72672f73796d66746f6e792f787072657373696f6e2f646f776e6c6f616473)](https://packagist.org/packages/symftony/xpression)[![License](https://camo.githubusercontent.com/c982bc5ea57c31a49f5de5381a7e8afbcf4638224688bce4aec027ed6a1e8156/68747470733a2f2f706f7365722e707567782e6f72672f73796d66746f6e792f787072657373696f6e2f6c6963656e7365)](https://packagist.org/packages/symftony/xpression)[![SensioLabsInsight](https://camo.githubusercontent.com/5afb22da99396eba637c733ccbdaabd92be980e20773994d0820ce0f4599a5be/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33386434376366662d316162622d343038332d613533372d3537393464396139623238312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/38d47cff-1abb-4083-a537-5794d9a9b281)

**Xpression** is a simple PHP implementation of **Specification pattern**.

Demo
----

[](#demo)

You can try this library on [Demo Xpression](http://symftony-xpression.herokuapp.com/)

Installation
------------

[](#installation)

The recommended way to install **Xpression** is through [Composer](http://getcomposer.org).

```
# Install Composer
curl -sS https://getcomposer.org/installer | php
```

```
php composer require symftony/xpression
```

After installing, you need to require Composer's autoloader:

```
require 'vendor/autoload.php';
```

Documentation
-------------

[](#documentation)

This library provide:

- lexer that tokenise your query
- parser that use tokens and ExpressionBuilder to create the target Expression
- a query string parser to correctly fix and parse the *$\_SERVER\['QUERY\_STRING'\]* into *$\_GET*

### Supported query operator

[](#supported-query-operator)

OperatorSyntaxExampleORMODMArrayCollectionClosureequal`=``param=value`XXXXnot equal`!=` `≠``param!=value` `param≠value`XXXXgreater than`>``param>value`XXXXgreater than equal`>=` `≥``param>=value` `param≥value`XXXXless than` 'Golf', 'year' => 1990, 'price' => 11),
    array('id' => 2, 'constructor' => 'Volkswagen', 'model' => 'Rabbit', 'year' => 2009, 'price' => 7),
    array('id' => 3, 'constructor' => 'Volkswagen', 'model' => 'Rabbit', 'year' => 2006, 'price' => 12),
    array('id' => 4, 'constructor' => 'Cadillac', 'model' => 'Catera', 'year' => 1999, 'price' => 5),
    array('id' => 5, 'constructor' => 'Cadillac', 'model' => 'STS', 'year' => 2006, 'price' => 14),
    array('id' => 6, 'constructor' => 'Ford', 'model' => 'Mustang', 'year' => 1970, 'price' => 4),
    array('id' => 7, 'constructor' => 'Ford', 'model' => 'Laser', 'year' => 1989, 'price' => 2),
    array('id' => 8, 'constructor' => 'Ford', 'model' => 'Bronco II', 'year' => 1990, 'price' => 3),
    array('id' => 9, 'constructor' => 'Lexus', 'model' => 'LS', 'year' => 2007, 'price' => 18),
    array('id' => 10, 'constructor' => 'Lexus', 'model' => 'LS', 'year' => 2000, 'price' => 17),
    array('id' => 11, 'constructor' => 'Lexus', 'model' => 'LX', 'year' => 1999, 'price' => 4),
    array('id' => 12, 'constructor' => 'Hyundai', 'model' => 'Sonata', 'year' => 1996, 'price' => 13),
    array('id' => 13, 'constructor' => 'Hyundai', 'model' => 'XG350', 'year' => 2002, 'price' => 5),
    array('id' => 14, 'constructor' => 'Land Rover', 'model' => 'Discovery SeriesII', 'year' => 2000, 'price' => 17),
    array('id' => 15, 'constructor' => 'Land Rover', 'model' => 'Discovery', 'year' => 2002, 'price' => 20),
    array('id' => 16, 'constructor' => 'Oldsmobile Cutlass', 'model' => 'Supreme', 'year' => 1992, 'price' => 3),
    array('id' => 17, 'constructor' => 'Mitsubishi', 'model' => 'Eclipse', 'year' => 2001, 'price' => 8),
);

$parser = new Parser(new ClosureExpressionBuilder());
$expression = $parser->parse($_GET['query']);
$filteredProducts = array_filter($products, $expression);
```

### Filter an ArrayCollection

[](#filter-an-arraycollection)

/!\\ ArrayCollection doesn't support `not and` `not or` `xor` `contains` `not contains`

These not supported operator was not allowed to use and the parser will throw `UnsupportedTokenTypeException`

```
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ExpressionBuilder;
use Symftony\Xpression\Bridge\Doctrine\Common\ExpressionBuilderAdapter;
use Symftony\Xpression\Parser;

QueryStringParser::correctServerQueryString();

$products = new ArrayCollection(
    array(
        array('id' => 1, 'title' => 'banana', 'price' => 2, 'quantity' => 5, 'category' => 'food'),
        array('id' => 2, 'title' => 'banana', 'price' => 5, 'quantity' => 15, 'category' => 'food'),
        array('id' => 3, 'title' => 'apple', 'price' => 1, 'quantity' => 1, 'category' => 'food'),
        array('id' => 4, 'title' => 'TV', 'price' => 399, 'quantity' => 1, 'category' => 'multimedia'),
    )
);

$parser = new Parser(new ExpressionBuilderAdapter(new ExpressionBuilder()));
$expression = $parser->parse($_GET['query']);

$filteredProducts = $products->matching(new Criteria($expression));
```

### Create a Doctrine ORM Expression

[](#create-a-doctrine-orm-expression)

/!\\ ORM Expression doesn't support `not and` `not or` `xor`

These not supported operator was not allowed to use and the parser will throw `UnsupportedTokenTypeException`

```
use Doctrine\ORM\Query\Expr;
use Symftony\Xpression\Bridge\Doctrine\ORM\ExprAdapter;
use Symftony\Xpression\Parser;

QueryStringParser::correctServerQueryString();

$parser = new Parser(new ExprAdapter(new Expr()));
$expression = $parser->parse($_GET['query']);

//$yourQueryBuilder()->where($expression);
```

### Create a Doctrine Mongodb Expression

[](#create-a-doctrine-mongodb-expression)

/!\\ ORM Expression doesn't support `not or` `xor`

These not supported operator was not allowed to use and the parser will throw `UnsupportedTokenTypeException`

```
use Symftony\Xpression\Bridge\Doctrine\MongoDb\ExprBuilder;
use Symftony\Xpression\Parser;

QueryStringParser::correctServerQueryString();

$parser = new Parser(new ExprBuilder());
$expression = $parser->parse($_GET['query']);

//$yourQueryBuilder()->where($expression);
```

### You can disable token type

[](#you-can-disable-token-type)

If you want disable an operator you can disable it manually The parser will throw `ForbiddenTokenException`

By default all token type was allowed `Lexer::ALL`

but when you call `$parser->parse($query, $allowedToken)`

Example to disable the greater than equal `≥` and the not equal `≠`

```
use Symftony\Xpression\Exception\Parser\InvalidExpressionException;
use Symftony\Xpression\Expr\HtmlExpressionBuilder;
use Symftony\Xpression\Lexer;
use Symftony\Xpression\Parser;
use Symftony\Xpression\QueryStringParser;

QueryStringParser::correctServerQueryString();

$allowedTokenType = Lexer::T_ALL - Lexer::T_GREATER_THAN_EQUALS - Lexer::T_NOT_EQUALS;
$parser = new Parser(new HtmlExpressionBuilder());
$expression = $parser->parse($_GET['query']), $allowedTokenType);
```

###  Health Score

26

—

LowBetter than 40% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/26468034?v=4)[Symftony](/maintainers/Symftony)[@Symftony](https://github.com/Symftony)

---

Top Contributors

[![instabledesign](https://avatars.githubusercontent.com/u/1098878?v=4)](https://github.com/instabledesign "instabledesign (48 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![Symftony](https://avatars.githubusercontent.com/u/26468034?v=4)](https://github.com/Symftony "Symftony (1 commits)")

### Embed Badge

![Health badge](/badges/symftony-xpression/health.svg)

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

PHPackages © 2026

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