Permalink
Browse files

importing Pagerfanta!

  • Loading branch information...
0 parents commit e1a058ddda42d79a0b1fee6ff840d511e14a808f @pablodip pablodip committed Apr 28, 2011
Showing with 2,323 additions and 0 deletions.
  1. +12 −0 .gitmodules
  2. +19 −0 LICENSE
  3. +276 −0 README.md
  4. +15 −0 phpunit.xml.dist
  5. +43 −0 src/Pagerfanta/Adapter/AdapterInterface.php
  6. +64 −0 src/Pagerfanta/Adapter/ArrayAdapter.php
  7. +66 −0 src/Pagerfanta/Adapter/DoctrineODMMongoDBAdapter.php
  8. +76 −0 src/Pagerfanta/Adapter/DoctrineORMAdapter.php
  9. +66 −0 src/Pagerfanta/Adapter/MandangoAdapter.php
  10. +21 −0 src/Pagerfanta/Exception/Exception.php
  11. +21 −0 src/Pagerfanta/Exception/InvalidArgumentException.php
  12. +21 −0 src/Pagerfanta/Exception/LessThan1CurrentPageException.php
  13. +21 −0 src/Pagerfanta/Exception/LessThan1MaxPerPageException.php
  14. +21 −0 src/Pagerfanta/Exception/LogicException.php
  15. +21 −0 src/Pagerfanta/Exception/NotIntegerCurrentPageException.php
  16. +21 −0 src/Pagerfanta/Exception/NotIntegerMaxPerPageException.php
  17. +21 −0 src/Pagerfanta/Exception/NotValidCurrentPageException.php
  18. +21 −0 src/Pagerfanta/Exception/NotValidMaxPerPageException.php
  19. +21 −0 src/Pagerfanta/Exception/OutOfRangeCurrentPageException.php
  20. +236 −0 src/Pagerfanta/Pagerfanta.php
  21. +145 −0 src/Pagerfanta/PagerfantaInterface.php
  22. +182 −0 src/Pagerfanta/View/DefaultView.php
  23. +59 −0 src/Pagerfanta/View/OptionableView.php
  24. +103 −0 src/Pagerfanta/View/ViewFactory.php
  25. +90 −0 src/Pagerfanta/View/ViewFactoryInterface.php
  26. +48 −0 src/Pagerfanta/View/ViewInterface.php
  27. +46 −0 tests/Pagerfanta/Tests/Adapter/ArrayAdapterTest.php
  28. +87 −0 tests/Pagerfanta/Tests/Adapter/DoctrineODMMongoDBAdapterTest.php
  29. +71 −0 tests/Pagerfanta/Tests/Adapter/MandangoAdapterTest.php
  30. +255 −0 tests/Pagerfanta/Tests/PagerfantaTest.php
  31. +34 −0 tests/Pagerfanta/Tests/View/DefaultViewTest.php
  32. +31 −0 tests/Pagerfanta/Tests/View/OptionableViewTest.php
  33. +71 −0 tests/Pagerfanta/Tests/View/ViewFactoryTest.php
  34. +14 −0 tests/bootstrap.php
  35. +1 −0 vendor/Symfony/Component/ClassLoader
  36. +1 −0 vendor/doctrine-mongodb
  37. +1 −0 vendor/doctrine-mongodb-odm
  38. +1 −0 vendor/mandango
@@ -0,0 +1,12 @@
+[submodule "vendor/mandango"]
+ path = vendor/mandango
+ url = http://github.com/mandango/mandango.git
+[submodule "vendor/doctrine-mongodb-odm"]
+ path = vendor/doctrine-mongodb-odm
+ url = http://github.com/doctrine/mongodb-odm.git
+[submodule "vendor/doctrine-mongodb"]
+ path = vendor/doctrine-mongodb
+ url = http://github.com/doctrine/mongodb.git
+[submodule "vendor/Symfony/Component/ClassLoader"]
+ path = vendor/Symfony/Component/ClassLoader
+ url = http://github.com/symfony/ClassLoader.git
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 Pablo Díez
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
276 README.md
@@ -0,0 +1,276 @@
+Pagerfanta
+=========
+
+Pagination for PHP 5.3
+
+Usage
+-----
+
+ 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()* method throws an exception if the max per page is not valid:
+
+ * *Pagerfanta\\Exception\\NotIntegerMaxPerPageException* (or integer in string)
+ * *Pagerfanta\\Exception\\LessThan1MaxPerPageException*
+
+Both extends from *Pagerfanta\\Exception\\NotValidMaxPerPageException*.
+
+The *->setCurrentPage()* method throws an exception if the page is not valid:
+
+ * *Pagerfanta\\Exception\\NotIntegerCurrentPageException* (or integer in string)
+ * *Pagerfanta\\Exception\\LessThan1CurrentPageException*
+ * *Pagerfanta\\Exception\\OutOfRangeCurrentPageException*
+
+All of them extends from *Pagerfanta\\Exception\\NotValidCurrentPageException*.
+
+Adapters
+--------
+
+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.
+
+An adapter must implement the *Pagerfanta\\Adapter\\AdapterInterface* interface, which has these two methods:
+
+ /**
+ * 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 The slice.
+ *
+ * @api
+ */
+ function getSlice($offset, $length);
+
+Pagerfanta comes with four adapters:
+
+### ArrayAdapter
+
+To paginate an array.
+
+ use Pagerfanta\Adapter\ArrayAdapter;
+
+ $adapter = new ArrayAdapter($array);
+
+### MandangoAdapter
+
+To paginate [Mandango](http://mandango.org) Queries.
+
+ use Pagerfanta\Adapter\MandangoAdapter;
+
+ $query = \Model\Article::getRepository()->createQuery();
+ $adapter = new MandangoAdapter($query);
+
+### DoctrineORMAdapter
+
+To paginate [DoctrineORM](http://www.doctrine-project.org/projects/orm) query builders.
+
+ use Pagerfanta\Adapter\DoctrineORMAdapter;
+
+ $queryBuilder = $entityManager->createQueryBuilder()
+ ->select('u')
+ ->from('Model\Article', 'u')
+ ;
+ $adapter = new DoctrineORMAdapter($queryBuilder);
+
+### DoctrineODMMongoDBAdapter
+
+To paginate [DoctrineODMMongoDB](http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/) query builders.
+
+ use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter;
+
+ $queryBuilder = $documentManager->createQueryBuilder('Model\Article');
+ $adapter = new DoctrineODMMongoDBAdapter($queryBuilder);
+
+Views
+-----
+
+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.
+
+The views implement the *Pagerfanta\\View\\ViewInterface* interface, which has two methods:
+
+ /**
+ * 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:
+
+ $routeGenerator = function($page) {
+ return '/path?page='.$page;
+ }
+
+Pagerfanta comes with two views, the default view and an special optionable view.
+
+### DefaultView
+
+This is the default view.
+
+ use Pagerfanta\View\DefaultView;
+
+ $view = new DefaultView();
+ $html = $view->render($pagerfanta, $routeGenerator, array(
+ 'proximity' => 3,
+ ));
+
+Options (default):
+
+ * proximity (3)
+ * previous_message (Previous)
+ * next_message (Next)
+ * css_disabled_class (disabled)
+ * css_dots_class (dots)
+ * css_current_class (current)
+
+![Pagerfanta DefaultView](http://img813.imageshack.us/img813/601/pagerfanta.png)
+
+ CSS:
+
+ .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;
+ }
+
+### OptionableView
+
+This view is to reuse options in different views.
+
+ 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!!'));
+
+Todo
+----
+
+ * Use count query in the DoctrinORMAdapter
+
+Author
+------
+
+Pablo Díez - <pablodip@gmail.com>
+
+License
+-------
+
+Pagerfanta is licensed under the MIT License. See the LICENSE file for full details.
+
+Sponsors
+--------
+
+[WhiteOctober](http://www.whiteoctober.co.uk/)
+
+Acknowledgements
+----------------
+
+Pagerfanta is inspired by [Zend Paginator](https://github.com/zendframework/zf2).
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit bootstrap="tests/bootstrap.php">
+ <testsuites>
+ <testsuite name="Pagerfanta Test Suite">
+ <directory>tests/Pagerfanta/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory suffix=".php">src/Pagerfanta/</directory>
+ </whitelist>
+ </filter>
+</phpunit>
@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * This file is part of the Pagerfanta package.
+ *
+ * (c) Pablo Díez <pablodip@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Pagerfanta\Adapter;
+
+/**
+ * AdapterInterface.
+ *
+ * @author Pablo Díez <pablodip@gmail.com>
+ *
+ * @api
+ */
+interface AdapterInterface
+{
+ /**
+ * 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 The slice.
+ *
+ * @api
+ */
+ function getSlice($offset, $length);
+}
Oops, something went wrong.

0 comments on commit e1a058d

Please sign in to comment.