jaspr/expression

Rich expression builder allows create complex closure expression tree to filter array of objects

1.1.3 2023-06-23 21:39 UTC

This package is auto-updated.

Last update: 2024-04-06 14:53:21 UTC


README

Is a tool for creating expression tree to filter collection of objects. The main task is to be able filter object in array by their properties. Additionally, I will add SQLResolver to create WHERE filter for your query.

Example

<?php
$obj1        = new \stdClass();
$obj1->field = 1;
$obj2        = new \stdClass();
$obj2->field = 2;
$arr         = [$obj1, $obj2];
$expression  = Ex::and(
    Ex::eq(Ex::field('field'), Ex::literal(1)),
    Ex::gt(Ex::field('field'), Ex::literal(0))
);
$resolver    = new ClosureResolver();
$filter      = $resolver->dispatch($expression);
$result      = array_filter($arr, $filter);

Result

array(1) {
  [0] =>
  class stdClass#410 (1) {
    public $field =>
    int(1)
  }
}