complex-heart / criteria
Small implementation of a filter criteria pattern in PHP for Complex Heart SDK. Compose several filters using fluent interface.
v4.0.1
2024-08-14 02:56 UTC
Requires
- php: ^8.2
- ext-json: *
- complex-heart/domain-model: ^4.0.0
Requires (Dev)
- mockery/mockery: ^1.6.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-faker: ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.0
- phpstan/phpstan-mockery: ^1.1
README
Small implementation of a criteria pattern in PHP for Complex Heart SDK. Compose several filters using fluent interface.
Installation
Just install the package from Packagist using composer:
composer require complex-heart/criteria
Usage
Import the class and use the fluent interface:
namespace ComplexHeart\Domain\Criteria; // Match the users with status active and more than 7k followers and from Spain and France $g1 = FilterGroup::create() ->addFilterEqual('status', 1) ->addFilterGreaterThan('followers', 7000) ->addFilterIn('country', ['es', 'fr']); $criteria = Criteria::default() ->withFilterGroup($g1) ->withOrderBy('surname') ->withOrderType('asc') ->withPageLimit(25) ->withPageOffset(50); $users = $repository->match($criteria); // alternative, same as above $criteria = Criteria::default() ->withFilterGroup(FilterGroup::create() ->addFilterEqual('status', 1) ->addFilterGreaterThan('followers', 7000) ->addFilterIn('country', ['es', 'fr'])) ->withOrderBy('surname') ->withOrderType('asc') ->withPageLimit(25) ->withPageOffset(50); // In SQL, we may have something like: // WHERE status = 1 AND followers >= 700 AND country in ('es', 'fr') $users = $repository->match($criteria);
A FilterGroup
is a set of filters or conditions that must match all together (AND
). To match one group or another
(OR
), just add more FilterGroup
.
// Match articles with given term in title, or in tagline, or in content. $criteria = Criteria::default() ->withFilterGroup(FilterGroup::create()->addFilterContains('title', $term)) ->withFilterGroup(FilterGroup::create()->addFilterContains('tagline', $term)) ->withFilterGroup(FilterGroup::create()->addFilterContains('content', $term)) ->withOrderBy('created_at') ->withOrderType(Order::TYPE_ASC) ->withPageNumber(3);