componenta / paginator
Paginator contract and immutable paginator implementation
v1.0.0
2026-06-16 21:48 UTC
Requires
- php: ^8.4
- componenta/arrayable: ^1.0
Requires (Dev)
- pestphp/pest: ^4.0
- phpunit/phpunit: ^12.0
README
Offset-based paginator value object with array conversion.
Use it for read models and list endpoints that return a slice of results together with navigation metadata.
Installation
composer require componenta/paginator
Related Packages
| Package | Why it matters here |
|---|---|
componenta/arrayable |
PaginatorInterface extends Arrayable. |
componenta/http-responder |
Responders can serialize pagination results as JSON. |
componenta/cycle |
DataFetcher can return paginated read-side results. |
componenta/cqrs |
Query handlers commonly return Paginator from read scenarios. |
Usage
use Componenta\Stdlib\Paginator; $page = new Paginator( results: $posts, limit: 20, offset: 40, totalCount: 95, ); $page->currentPage; // 3 $page->totalPages; // 5 $page->hasNextPage; // true $page->nextOffset; // 60
Paginator implements PaginatorInterface, which extends Componenta\Arrayable\Arrayable.
Array Shape
toArray() returns:
resultstotalCountlimitoffsetcurrentPagetotalPageshasNexthasPrevrange
range is null for an empty page. Otherwise it contains one-based result positions for the current slice.
Unknown Totals
totalCount may be null for infinite-scroll reads. In that mode:
totalPagesisnullhasNextPageuseshasNextPageHintwhen provided- without a hint,
hasNextPagefalls back tocount(results) >= limit
Fetchers can request one extra row, trim it, and pass an explicit hint for precise next-page detection without running a total-count query.