PHPackages                             gomoob/php-value-filter-dsl - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. gomoob/php-value-filter-dsl

ActiveLibrary[HTTP &amp; Networking](/categories/http)

gomoob/php-value-filter-dsl
===========================

Powerful filter DSL PHP library for REST Web Services query / URL parameters or other filtering needs.

1.2.1(8y ago)719.2k1[2 issues](https://github.com/gomoob/php-value-filter-dsl/issues)BSD-3-ClausePHPPHP ^5.6 || ^7.0

Since Aug 1Pushed 8y ago2 watchersCompare

[ Source](https://github.com/gomoob/php-value-filter-dsl)[ Packagist](https://packagist.org/packages/gomoob/php-value-filter-dsl)[ RSS](/packages/gomoob-php-value-filter-dsl/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (12)Versions (9)Used By (0)

php-value-filter-dsl
====================

[](#php-value-filter-dsl)

> Powerful filter DSL PHP library for REST Web Services query / URL parameters or other filtering needs.

[![Total Downloads](https://camo.githubusercontent.com/8b06765d2f233554b773e9248d3234eed266e3c451dd175826ab312599653d10/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gomoob/php-value-filter-dsl)[![Latest Stable Version](https://camo.githubusercontent.com/a5732957259593a1a973b91f094dec38b5fffe9f52a2cb361684248d83b9f6b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gomoob/php-value-filter-dsl)[![Build Status](https://camo.githubusercontent.com/690bb73d702c658613415af2f656d8079677ceb5ef29a93b8bb52f199f2d851b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/gomoob/php-value-filter-dsl)[![Coverage](https://camo.githubusercontent.com/edae24e48383e7e023b9280965331a9917b6ecdb18bce7073db3820bd41247be/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/gomoob/php-value-filter-dsl?branch=master)[![Code Climate](https://camo.githubusercontent.com/7e838a1a47bd2404abec195653b7ccff0e63266d3d29b6a928c685de855a81ac/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/gomoob/php-value-filter-dsl)[![License](https://camo.githubusercontent.com/3fc15163234e59b0a4b8b22772c9efcbe50ccf3efb3e7adccd6a20f29044f320/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f676f6d6f6f622f7068702d76616c75652d66696c7465722d64736c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gomoob/php-value-filter-dsl)

Sample with not in
------------------

[](#sample-with-not-in)

Suppose you have a Web Service accessible using `https://api.myserver.com/users` and want to create a filter to find users not having a first name equals to `Jack`, `Joe`, `Willian` or `Averell`.

To do this you'll have to request your relationnal database with a `not in` SQL request. The `php-value-filter-dsl`library allows you to parse a custom filter expression from an URL query parameter and convert it into an equivalent SQL expression you can use in your SQL query builder.

Our filter expression language is a custom one designed by Gomoob to respond to lots of REST Web Services API filtering needs, this filter expression is thorougly described in our documentation.

Filtering to exclude the 4 first names described previously would be done using the following GET HTTP request.

```
https://api.myserver.com/users?first_name=!in('Jack','Joe','Willian','Averell')

```

The PHP source code used to parse the filter expression (i.e the `first_name` URL query parameter value) is the following.

```
// Suppose we are inside a controller (for example inside a PSR-7 Middleware) and we got the value of the 'first_name'
// URL query parameter from "https://api.myserver.com/users?first_name=!in('Jack','Joe','Willian','Averell')"
$urlParameterName = 'first_name';
$urlParameterValue = "!in('Jack','Joe','Willian','Averell')";

// Parsing the filter expression
$filterConverter = new SqlFilterConverter();
$sqlFilter = $this->filterConverter->transform($urlParameterName, $urlParameterValue);

// Use the parsed result to build our SQL query
$preparedStatement = $pdo->prepare('select * from users where ' . $sqlFilter->getExpression());

// Bind our prepared statement parameters
$i = 1;
foreach($sqlFilter->getParams() as $param) {
    $preparedStatement->bindParam($i++, $param);
}

// Executes our query
$preparedStatement->execute();
```

The previous sample will execute the SQL query `select * from users where first_name not in('?','?','?','?')` with the prepared statement parameters `Jack`, `Joe`, `Willian`, `Averell`.

Very simple and useful, isn't it ?

Please note that for now we only provide convertion of filter expressions in SQL. Later we'll extend the library to provide additional converters to transform the filters into other formats.

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

[](#documentation)

### Standard operators

[](#standard-operators)

The expression language provides the following operators.

OperatorASCII valueNameValue type(s)`=``%3D`EqualsInteger, Float, String`=``%3E%3D`Greater than or equal toInteger, Float`in`InInteger list, Double list, String list`~``%7E`LikeString`!``%21`Not*see description above*`+``%2B`And*see description above*`-``%2D`Or*see description above*### Not operator

[](#not-operator)

The `!` operator is special, it can be used directly before a value string or in combination with the `=` or `in`operators.

For exemple `!5` or `!=5` to express "not equals to 5" or `!in('Paris','London')` to express "not equals to Paris or London".

### AND and OR operators

[](#and-and-or-operators)

The `+` and `-` operator allow to create AND and OR SQL requests.

Here are sample expressions with logical operators.

- `property=>5.4+= ? AND property < ?` with 2 parameters `[5.4,12]` ;
- `property=~'*ball*'-~'*tennis*'` is translated to `property like ? OR property like ?` with 2 parameters `\['%ball%','%tennis%'\].

### Like operator

[](#like-operator)

The `~` operator allows to create like SQL requests, but it is always converted to expressions equals to `my_property like ?` with a value equals to `%word%` which is not always wanted.

To express more complex like expressions you can use the `*` string operator in the value associated to the `=` or `~`operators.

For example `property=~'*Nantes*France*'` or `property='Nantes*France*'` will be translated to `property like ?` with a parameter equals to `%Nantes%France%`.

### Values

[](#values)

The following values can be used.

- `null`
- `true`, converted to the string "true" if the associated property is a string, to 1 if the property is an integer and to 1.0 if the property is a double
- `false`, converted to the string "false" if the associated property is a string, to 0 if the property is an integer and to 0.0 if the property is a double
- integer
- floting number
- string (must be quoted with simple quotes ')
- string with an ISO 8601 format for the dates

### Date and time parsing

[](#date-and-time-parsing)

By default when the `SqlFilterConverter` encounters a string inside an expression it simply takes it as a "standard" string.

But you've probably business entities having date attributes and want to request those entities using data and time filters. To do this you can set a date and time parser on the `SqlFilterConverter` to indicate him to parse date and time string and transform them to date and time string which are compliant with the database in use.

For example configuring the `SqlFilterConverter` to parse ISO 8601 strings and convert them to MySQL date and time format is done with the following.

```
$sqFilterConverter = new SqlFilterConverter();
$sqlFilterConverter->setDateTimeParser(new FormatDateTimeParser());
```

By default the `FormatDateTimeParser` class uses ISO 8601 date and time parsing, but you can change its behavior with the `FormatDateTimeParser->setFormat(string $format)` method. In generall you'll want to use one of the format provided with the PHP `DateTime` class, that's to say one of `DateTime::ATOM`, `DateTime::COOKIE`, `DateTime::ISO8601`, `DateTime::RFC822`, `DateTime::RFC850`, `DateTime::RFC1036`, `DateTime::RFC1123`, `DateTime::RFC2822`, `DateTime::RFC3339`, `DateTime::RSS` or `DateTime::W3C`.

The parser parses date and time strings and convert them to PHP `DateTime` object, then internally the `SqlFilterConverter` converts the `DateTime` object to a string which is compatible with Mysql.

For example the following transform will create a `property transform('property', "
