jaspr / expression
Rich expression builder allows create complex closure expression tree to filter array of objects
1.1.4
2024-12-07 22:08 UTC
Requires
- php: ^8.1
Requires (Dev)
- ext-pdo: *
- ext-pdo_sqlite: *
- doctrine/collections: ^1.6
- doctrine/orm: ^2.7
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.4
- rector/rector: 2.0.0-rc2
- squizlabs/php_codesniffer: ^3.5
Suggests
- doctrine/collections: If you want use DoctrineCriteriaResolver
- doctrine/orm: If you want use DoctrineQueryResolver
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) } }