Filter array in PHP based on boolean expressions
I am trying to implement a filter function in PHP. My 2D input array looks like this:
name(string) age(integer) reg_date(date)
----------------------------------------------
John 22 12/03/2015
Anne 23 14/04/2015
Marry 24 01/02/2015
Tim 55 13/03/2015
What I want is a filter that can take a boolean expression and apply it to the original source like the following (custom project):
{name:'age' type:'integer', operator:'==', value:'23'} OR {name:'age' type:'integer', operator:'==', value:'24'} AND {name:'reg_date' type:'date', operator:'>', value:'12/03/2015', format:'d/m/Y'}
Expressed in words: give me all records where the variable age is 23 or 24 and the variable reg_date is later than 12/03/2015
For each data type (type), there must be a filter that must be able to handle operations that depend on the data type (for example, a date filter must use a format field to parse a date in the correct format). You cannot add new data types.
The correct output for the expression above would be
name(string) age(integer) reg_date(date)
----------------------------------------------
Anne 23 14/04/2015
My question is, should I write my own lexer to parse the boolean expression (which can get quite tricky) and apply a filter Ive written myself or the filter functions I need already exist? I tried to parse my array in an XML structure and use XPath, unfortunately XPath 1.0 which PHP uses cannot handle date formats. I took a look at filter_var in the PHP Filter class, but it looks like boolean expressions cannot be used there.
source to share