makedo / php-paginator
Data source independent paginator for php
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/makedo/php-paginator
Requires
- php: >=7.1
Requires (Dev)
- phpspec/phpspec: ^5.1
This package is auto-updated.
Last update: 2025-10-05 22:17:09 UTC
README
This is yet another paginator for php. Main idea of this package is to build data source independent paginator, which has an ability to work in several modes:
-
Count skip by id
$loader = function (int $limit, int $skip): iterable { //SELECT * from users WHERE id > $skip LIMIT $limit }; $perPage = 100; $lastIdOnPreviousPage = 34; $paginatorFactory = new SkipById($perPage); $page = $paginatorFactory ->createPaginator(new CallableLoader($loader), $lastIdOnPreviousPage) ->paginate() ;
In this example you have an ability use
idas skip value. As you can see, Loader function acceptsidin$skipvariable. It should beidof last item on previous page and it comes from client side. For 1st page it should be 0. For$page->hasNextpaginator will load$perPage + 1item, and then check if actual count of loaded items is more than$perPage.Optionally, you can set
$currentPagevalue, which will be set to$page->currentPage. -
Count skip by id with total count
$loader = function (int $limit, int $skip): iterable { //SELECT * from users WHERE id > $skip LIMIT }; $counter = function (): int { //SELECT count(id) from users }; $perPage = 100; $lastIdOnPreviousPage = 35; $paginatorFactory = new SkipByIdCountable($perPage); $page = $paginatorFactory ->createPaginator( new CallableLoader($loader), new CallableCounter($counter), $lastIdOnPreviousPage ) ->paginate() ;
In above example paginator counts skip by id and uses total count for
$page->total,$page->totalPagesvalues. For$page->hasNextpaginator will load$perPage + 1item, and then check if actual count of loaded items is more than$perPage. -
Count skip by id with total count and current page
$loader = function (int $limit, int $skip): iterable { //SELECT * from users WHERE id > $skip LIMIT }; $counter = function (): int { //SELECT count(id) from users }; $perPage = 100; $lastIdOnPreviousPage = 35; $currentPage = 4; $paginatorFactory = new SkipByIdCountable($perPage); $page = $paginatorFactory ->createPaginator( new CallableLoader($loader), new CallableCounter($counter), $lastIdOnPreviousPage, $currentPage ) ->paginate() ;
In above example paginator counts skip by id and uses total count for
$page->total,$page->totalPages,$page->hasNextvalues. In this case, we know$currentPagevalue, which in real case comes from client, so we can use total count and$currentPagefor counting$page->hasNextexcept using$perPage + 1strategy. -
Count skip by offset
$loader = function (int $limit, int $skip): iterable { //SELECT * from users LIMIT $limit OFFSET $skip }; $perPage = 100; $currentPage = 2; $paginatorFactory = new SkipByOffset($perPage); $page = $paginatorFactory ->createPaginator(new CallableLoader($loader), $currentPage) ->paginate() ;
In above example paginator counts skip as offset according to $perPage and $currentPage values. For counting
$page->hasNextpaginator will load$perPage + 1item, and then check if actual count of loaded items is more than$perPage.-
Count skip by offset and use total count
$loader = function (int $limit, int $skip): iterable { //SELECT * from users LIMIT $limit OFFSET $skip }; $counter = function (): int { //SELECT count(id) from users }; $perPage = 100; $currentPage = 2; $paginatorFactory = new SkipByOffsetCountable($perPage); $page = $paginatorFactory ->createPaginator(new CallableLoader($loader), new CallableCounter($counter), $currentPage) ->paginate() ;
In above example paginator counts skip by offset and uses total count for
$page->total,$page->totalPages,$page->hasNextvalues.
-