sirprize / paginate
Normalize an index range from user input
2.0.0
2023-02-18 15:27 UTC
Requires
- php: ^7.2 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Normalize an index range from user input
Use Case 1 (start with an index range)
- Get the current range from user input (eg from a HTTP header -
Range: items=40-49
) - Define the slice (eg run a query with a
LIMIT
expression)
Usage
use Sirprize\Paginate\Input\IndexInput;
use Sirprize\Paginate\Range\IndexRange;
$i = new IndexInput(4400, 4499); // $offset, $last
$i->setDefaultNumItems(25)->setMaxItems(100); // set defaults and limits
$r = new IndexRange($i);
$q = sprintf("SELECT * FROM my_table LIMIT %d, %d", $r->getOffset(), $r->getNumItems());
Use Case 2 (start from current page)
- Get the current page and possibly the number of items per page from user input
- Define the slice (eg run a query with a
LIMIT
expression)
Usage
use Sirprize\Paginate\Input\PageInput;
use Sirprize\Paginate\Range\PageRange;
$i = new PageInput(45, 100); // $currentPage, $numItemsPerPage
$i->setDefaultNumItems(25)->setMaxItems(100); // set defaults and limits
$r = new PageRange($i);
$q = sprintf("SELECT * FROM my_table LIMIT %d, %d", $r->getOffset(), $r->getNumItems());
Paginator
- Perform an action to get the total number of items in a list (eg run a simple query with a
COUNT
expression) - Get the current page and possibly the number of items per page from user input
- Start paginator with user input and number of items
- Define the slice (eg run a complex query with a
LIMIT
expression)
Usage
use Sirprize\Paginate\Input\PageInput;
use Sirprize\Paginate\Range\PageRange;
use Sirprize\Paginate\Paginator;
$i = new PageInput(45, 100); // $currentPage, $numItemsPerPage
$i->setDefaultNumItems(25)->setMaxItems(100); // set defaults and limits
$r = new PageRange($i);
$r->setTotalItems(189678);
$p = new Paginator($r);
$p->setBaseUrl('/products')
$p->setPageParam('p');
The following information is available:
echo $p->isOutOfBounds(); // 0
echo $p->getTotalItems(); // 189678
echo $p->getNumItemsPerPage(); // 100
echo $p->getNumItemsOnCurrentPage(); // 100
echo $p->getFirstItemOnCurrentPage(); // 4401
echo $p->getLastItemOnCurrentPage(); // 4500
echo $p->getNumPages(); // 1897
echo $p->getCurrentPage(); // 45
echo $p->getPreviousPage(); // 44
echo $p->getNextPage(); // 46
echo $p->getFirstPage(); // 1
echo $p->getLastPage(); // 1897
echo $p->getOffset(); // 4400
echo $p->getLast(); // 4499
echo $p->getCurrentPageUrl(); // /products?p=45
echo $p->getNextPageUrl(); // /products?p=46
echo $p->getPreviousPageUrl(); // /products?p=44
echo $p->getFirstPageUrl(); // /products?p=1
echo $p->getLastPageUrl(); // /products?p=1897
echo $p->getBaseUrl(); // /products
echo $p->getPageParam(); // p
License
See LICENSE.