dev-master
9999999-devPagination for PHP 5.3
MIT
The Requires
- php >=5.3.0
by Pablo Díez
page paging
Pagination for PHP 5.3
Pagination for PHP 5.3, (*2)
<?php use Pagerfanta\Pagerfanta; use Pagerfanta\Adapter\ArrayAdapter; $adapter = new ArrayAdapter($array); $pagerfanta = new Pagerfanta($adapter); $pagerfanta->setMaxPerPage($maxPerPage); // 10 by default $maxPerPage = $pagerfanta->getMaxPerPage(); $pagerfanta->setCurrentPage($currentPage); // 1 by default $currentPage = $pagerfanta->getCurrentPage(); $nbResults = $pagerfanta->getNbResults(); $currentPageResults = $pagerfanta->getCurrentPageResults(); $pagerfanta->getNbPages(); $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page $pagerfanta->hasPreviousPage(); $pagerfanta->getPreviousPage(); $pagerfanta->hasNextPage(); $pagerfanta->getNextPage();
The ->setMaxPerPage()
and ->setCurrentPage()
methods implement a fluent interface:, (*3)
<?php $pagerfanta ->setMaxPerPage($maxPerPage) ->setCurrentPage($currentPage) ;
The ->setMaxPerPage()
method throws an exception if the max per page is not valid:, (*4)
Pagerfanta\Exception\NotIntegerMaxPerPageException
(including integer in string)Pagerfanta\Exception\LessThan1MaxPerPageException
Both extends from Pagerfanta\Exception\NotValidMaxPerPageException
., (*5)
The ->setCurrentPage()
method throws an exception if the page is not valid:, (*6)
Pagerfanta\Exception\NotIntegerCurrentPageException
(including integer in string)Pagerfanta\Exception\LessThan1CurrentPageException
Pagerfanta\Exception\OutOfRangeCurrentPageException
All of them extends from Pagerfanta\Exception\NotValidCurrentPageException
., (*7)
The adapter's concept is very simple. An adapter just returns the number of results and an slice for a offset and length. This way you can adapt a pagerfanta to paginate any kind results simply creating an adapter., (*8)
An adapter must implement the Pagerfanta\Adapter\AdapterInterface
interface, which has these two methods:, (*9)
<?php /** * Returns the number of results. * * @return integer The number of results. * * @api */ function getNbResults(); /** * Returns an slice of the results. * * @param integer $offset The offset. * @param integer $length The length. * * @return array|\Iterator|\IteratorAggregate The slice. * * @api */ function getSlice($offset, $length);
Pagerfanta comes with eight adapters:, (*10)
To paginate an array., (*11)
<?php use Pagerfanta\Adapter\ArrayAdapter; $adapter = new ArrayAdapter($array);
To paginate Mongo Cursors., (*12)
<?php use Pagerfanta\Adapter\MongoAdapter; $cursor = $collection->find(); $adapter = new MongoAdapter($cursor);
To paginate Mandango Queries., (*13)
<?php use Pagerfanta\Adapter\MandangoAdapter; $query = \Model\Article::getRepository()->createQuery(); $adapter = new MandangoAdapter($query);
To paginate DoctrineORM query objects., (*14)
<?php use Pagerfanta\Adapter\DoctrineORMAdapter; $queryBuilder = $entityManager->createQueryBuilder() ->select('u') ->from('Model\Article', 'u') ; $adapter = new DoctrineORMAdapter($queryBuilder);
To paginate DoctrineODMMongoDB query builders., (*15)
<?php use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; $queryBuilder = $documentManager->createQueryBuilder('Model\Article'); $adapter = new DoctrineODMMongoDBAdapter($queryBuilder);
To paginate a Doctrine\Common\Collection\Collections
interface you can use the DoctrineCollectionAdapter
. It proxies to the count() and slice() methods on the Collections interface for pagination. This makes sense if you are using Doctrine ORMs Extra Lazy association features:, (*16)
<?php use Pagerfanta\Adapter\DoctrineCollectionAdapter; $user = $em->find("Pagerfanta\Tests\Adapter\DoctrineORM\User", 1); $adapter = new DoctrineCollectionAdapter($user->getGroups());
To paginate a propel query:, (*17)
<?php use Pagerfanta\Adapter\PropelAdapter; $adapter = new PropelAdapter($query);
To paginate a solarium query:, (*18)
<?php use Pagerfanta\Adapter\SolariumAdapter; $query = $solarium->createSelect(); $query->setQuery('search term'); $adapter = new SolariumAdapter($solarium, $query);
The views are to render pagerfantas, this way you can reuse your pagerfantas' html in several projects, share them and use another ones from another developers., (*19)
The views implement the Pagerfanta\View\ViewInterface
interface, which has two methods:, (*20)
<?php /** * Renders a pagerfanta. * * The route generator is any callable to generate the routes receiving the page number * as first and unique argument. * * @param PagerfantaInterface $pagerfanta A pagerfanta. * @param mixed $routeGenerator A callable to generate the routes. * @param array $options An array of options (optional). * * @api */ function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()); /** * Returns the canonical name. * * @return string The canonical name. * * @api */ function getName();
RouteGenerator example:, (*21)
<?php $routeGenerator = function($page) { return '/path?page='.$page; }
Pagerfanta comes with three views, the default one, one for Twitter Bootstrap and an special optionable view., (*22)
This is the default view., (*23)
<?php use Pagerfanta\View\DefaultView; $view = new DefaultView(); $html = $view->render($pagerfanta, $routeGenerator, array( 'proximity' => 3, ));
Options (default):, (*24)
, (*25)
CSS:, (*26)
.pagerfanta { } .pagerfanta a, .pagerfanta span { display: inline-block; border: 1px solid blue; color: blue; margin-right: .2em; padding: .25em .35em; } .pagerfanta a { text-decoration: none; } .pagerfanta a:hover { background: #ccf; } .pagerfanta .dots { border-width: 0; } .pagerfanta .current { background: #ccf; font-weight: bold; } .pagerfanta .disabled { border-color: #ccf; color: #ccf; } COLORS: .pagerfanta a, .pagerfanta span { border-color: blue; color: blue; } .pagerfanta a:hover { background: #ccf; } .pagerfanta .current { background: #ccf; } .pagerfanta .disabled { border-color: #ccf; color: #cf; }
This view generates a pagination for Twitter Bootstrap., (*27)
<?php use Pagerfanta\View\TwitterBootstrapView; $view = new TwitterBootstrapView(); $html = $view->render($pagerfanta, $routeGenerator, array( 'proximity' => 3, ));
Options (default):, (*28)
This view is to reuse options in different views., (*29)
<?php use Pagerfanta\DefaultView; use Pagerfanta\OptionableView; $defaultView = new DefaultView(); // view and default options $myView1 = new OptionableView($defaultView, array('proximity' => 3)); $myView2 = new OptionableView($defaultView, array('previous_message' => 'Anterior', 'next_message' => 'Siguiente')); // using in a normal way $pagerfantaHtml = $myView2->render($pagerfanta, $routeGenerator); // overwriting default options $pagerfantaHtml = $myView2->render($pagerfanta, $routeGenerator, array('next_message' => 'Siguiente!!'));
Pablo Díez - pablodip@gmail.com, (*30)
Pagerfanta is licensed under the MIT License. See the LICENSE file for full details., (*31)
WhiteOctober, (*32)
Pagerfanta is inspired by Zend Paginator., (*33)
Pagination for PHP 5.3
MIT
page paging