xsga / bbphp-criteria-doctrine-adapter
A PHP criteria core library
Package info
github.com/xsga/BBPHP-Criteria-Doctrine-Adapter
Type:project
pkg:composer/xsga/bbphp-criteria-doctrine-adapter
Requires
- php: ^8.4
- doctrine/collections: ^2.3
- xsga/bbphp-criteria-core: ^1.0
Requires (Dev)
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.4
- phpmd/phpmd: ^2.15
- squizlabs/php_codesniffer: ^4.0
- vimeo/psalm: ^6.14
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-25 06:40:22 UTC
README
A PHP adapter that converts the validated criteria model into Doctrine Collections Criteria, making it easy to apply filters, ordering, and pagination from a domain-level abstraction without coupling repositories to Doctrine-specific code.
Description
BBPHP Criteria Doctrine Adapter is the persistence adapter for xsga/bbphp-criteria-core. It takes the immutable Criteria object created by the core library and transforms it into a Doctrine Common\Collections\Criteria instance ready to be used with EntityRepository::matching().
This package keeps the concerns cleanly separated:
- the core library validates and structures query criteria,
- this adapter translates those criteria into Doctrine-compatible expressions.
The result is a simple, reusable, and typed query layer that stays independent from business logic and direct SQL assembly.
Features
- Conversion from
Xsga\BBPHP\Criteria\Core\Domain\Model\Criteriato DoctrineCriteria - Support for
eq,ne,lk,gt,lt,ge,le, andinoperators - Correct handling of
INpredicates using pipe-delimited values such as1|2|3 - Sort mapping for one or multiple fields
limitandoffsetpropagation to Doctrine- Small, focused adapter with a single responsibility
- Compatible with the core criteria API and Doctrine Collections
Requirements
- PHP 8.4+
- Composer
doctrine/collections^2.3xsga/bbphp-criteria-core^1.0
Installation
composer require xsga/bbphp-criteria-doctrine-adapter
Quick example
use Psr\Log\NullLogger; use Xsga\BBPHP\Criteria\Adapter\Doctrine\DoctrineCriteriaConverter; use Xsga\BBPHP\Criteria\Core\Application\Services\CriteriaService; use Xsga\BBPHP\Criteria\Core\Domain\Services\GetFiltersService; use Xsga\BBPHP\Criteria\Core\Domain\Services\GetOrderService; use Xsga\BBPHP\Criteria\Core\Domain\Services\GetPaginationService; $logger = new NullLogger(); $criteriaService = new CriteriaService( new GetFiltersService($logger), new GetOrderService($logger), new GetPaginationService(), 50 ); $criteria = $criteriaService->getCriteria([ 'filter' => 'status:eq:active,createdAt:ge:2025-01-01:date', 'order_by' => 'createdAt:DESC', 'limit' => 10, 'offset' => 0, ]); $converter = new DoctrineCriteriaConverter(); $doctrineCriteria = $converter->get($criteria); $results = $repository->matching($doctrineCriteria);
How the adapter works
The flow is intentionally simple:
Core criteria model
↓
DoctrineCriteriaConverter::get($criteria)
↓
Doctrine\Common\Collections\Criteria
↓
EntityRepository::matching($criteria)
Filters
Each filter of the core domain is converted to a Doctrine Comparison expression:
new Comparison('status', '=', 'active');
For an in filter, the adapter splits pipe-separated values:
filter=id:in:1|2|3
which becomes:
new Comparison('id', 'in', ['1', '2', '3']);
Ordering
The order definitions are mapped to Doctrine's expected associative array:
[
'createdAt' => 'DESC',
'name' => 'ASC',
]
Pagination
The domain pagination is converted directly into Doctrine offset and limit values.
Supported input format
This package relies on the criteria notation defined by the core library.
Filters
filter=field:operator:value[:type]
Examples:
filter=status:eq:active
filter=createdAt:ge:2025-01-01:date
filter=id:in:1|2|3
filter=email:lk:john
Multiple filters are combined using AND:
filter=status:eq:active,createdAt:ge:2025-01-01:date
Ordering
order_by=field:type
Examples:
order_by=createdAt:DESC
order_by=name:ASC,createdAt:DESC
Pagination
limit=20&offset=0
Internal structure
Src/Xsga/BBPHP-Criteria-Doctrine-Adapter/
├── DoctrineCriteriaConverter.php
├── DoctrineSQLDto.php
└── ...
DoctrineCriteriaConverter
The main adapter that transforms a domain-level Criteria object into a Doctrine CollectionsCriteria instance.
Responsibilities:
- build a
CompositeExpressionfrom filters - map each domain filter to a Doctrine comparison
- handle
infilters as arrays from pipe-delimited values - transform order definitions to Doctrine order-by input
- set
offsetandlimit
Design principles
- Validation remains in the core criteria package
- Persistence adaptation happens in a dedicated adapter
- Immutable domain models
- Explicit operator validation
- Clean separation of concerns
Related packages
xsga/bbphp-criteria-core— typed criteria model and validationdoctrine/collections— filtering and sorting primitives used by this adapter
Documentation
License
This project is licensed under the MIT License.