Skip to content
Symftony edited this page Dec 10, 2017 · 4 revisions

Xpression

Xpression is a simple PHP library to convert human friendly expression into specific query. Xpression syntax would to be smallest and simplest as possible in order to create more complex query. Xpression combine many patterns like DSL (Domain syntax language) and Specification.

Bridge

Many bridge adapter are provided

Demo

You can try this library on Demo Xpression

Installation

Installation as library

Installation as bundle

Documentation

This library provide:

  • Lexer that tokenise your query
  • Parser that use tokens and ExpressionBuilder to create the target Expression
  • many bridge adapter
  • a query string parser to correctly fix and parse the $_SERVER['QUERY_STRING'] into $_GET

Simple Expression

A simple expression is compose with two operand (integer, float, string, parameter) and an simple operator (=, ≠, >, ≥, <, ≤, in, not in, contains, not contains)

Composite Expression

A composite expression is compose with two or more expression(simple or composite) and an composite operator (&, !&, |, !|, ⊕)

Supported query operator

Operator Syntax Example ORM ODM ArrayCollection Closure
equal = param=value X X X X
not equal != param!=value param≠value X X X X
greater than > param>value X X X X
greater than equal >= param>=value param≥value X X X X
less than < param<value X X X X
less than equal <= param<=value param≤value X X X X
less than equal <= param<=value param≤value X X X X
in [ ] param[value1,value2] X X X X
contains {{ }} param{{value}} X X X
not contains !{{ }} param!{{value}} X X X
and & param>1&param<10 X X X X
not and !& param>1!&param<10 X X
or | param>1|param<10 X X X X
not or !| param>1!|param<10 X
xor ^| param>1^|param<10 param>1⊕param<10 X

Group syntax and composite operator precedence

The highest was compute before the lowest

  • and: 15
  • not and: 14
  • or: 10
  • xor: 9
  • not or: 8

if you want to force a certain order of computation use the group syntax with ( )

This expression search title 'Bar' with 'price' under '5' or title 'Foo'

title='Foo'|title='Bar'&price<5 is equal to title='Foo'|(title='Bar'&price<5)

The next one search the title 'Foo' or 'Bar' with 'price' less than '5'

(title='Foo'|title='Bar')&price<5

Exceptions

The Lexer throw UnknownTokenTypeException if input contain an unknown Xpression char.

The parser throw

  • UnsupportedTokenTypeException when input contain a known token but if the bridge not support it (report to support token table)
  • ForbiddenTokenException when input contain forbidden token
  • UnexpectedTokenException when current token was unexpected (2 token type with no logical relation between them, eg: field=≠3)