From 0f81f04a50b75f9ffd09d5a4971ec189366d6286 Mon Sep 17 00:00:00 2001 From: Ashley Dawson Date: Mon, 18 May 2015 22:40:30 +0100 Subject: [PATCH] Added constructor configuration for paginator --- README.md | 22 ++++- .../SimplePagination/Paginator.php | 28 ++++++ .../SimplePagination/Tests/PaginatorTest.php | 96 +++++++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c500587..8304405 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ describe the operation of Simple Pagination. These are: * Pagination model The **Paginator** service performs the pagination algorithm, generating the page range and item collection slices. -When it's done it will return a **Pagination** model filled with the item collection slice and meta information. +When it's done it will return a **Pagination** object filled with the item collection slice and metadata. The two main operations the **Paginator** service will perform on your collection (or data set) are denoted by two callback methods passed to the **Paginator** service. The first one is the **Item total callback**. This callback is @@ -44,7 +44,7 @@ used to determine the total number of items in your collection (returned as an i The idea behind using these callbacks is so that Simple Pagination is kept, well, simple! The real power comes with the flexibility. You can use Simple Pagination with just about any collection you want. From simple arrays to database lists to [Doctrine](http://www.doctrine-project.org/) collections to [Solr](http://lucene.apache.org/solr/) result -sets - we're got you covered! It really doesn't matter what we paginate - as long as it's a collection of things and you +sets - we've got you covered! It really doesn't matter what we paginate - as long as it's a collection of things and you can count and slice it. Basic Usage @@ -171,6 +171,24 @@ foreach ($pagination->getPages() as $page) { It really doesn't matter what sort of collection you return from the Paginator::setSliceCallback() callback. It will always end up in Pagination::getItems(). +Constructor Configuration +------------------------- + +You can also configure the paginator with a configuration array passed to the constructor. For example: + +```php +$paginator = new Paginator(array( + 'itemTotalCallback' => function () { + // ... + }, + 'sliceCallback' => function ($offset, $length) { + // ... + }, + 'itemsPerPage' => 10, + 'pagesInRange' => 5 +)); +``` + Pagination Object ------------------------------------------------- diff --git a/lib/AshleyDawson/SimplePagination/Paginator.php b/lib/AshleyDawson/SimplePagination/Paginator.php index 9d901f5..1410c6d 100644 --- a/lib/AshleyDawson/SimplePagination/Paginator.php +++ b/lib/AshleyDawson/SimplePagination/Paginator.php @@ -33,6 +33,34 @@ class Paginator implements PaginatorInterface */ private $pagesInRange = 5; + /** + * Constructor - passing optional configuration + * + * + * $paginator = new Paginator(array( + * 'itemTotalCallback' => function () { + * // ... + * }, + * 'sliceCallback' => function ($offset, $length) { + * // ... + * }, + * 'itemsPerPage' => 10, + * 'pagesInRange' => 5 + * )); + * + * + * @param array|null $config + */ + public function __construct(array $config = null) + { + if (is_array($config)) { + $this->setItemTotalCallback($config['itemTotalCallback']); + $this->setSliceCallback($config['sliceCallback']); + $this->setItemsPerPage($config['itemsPerPage']); + $this->setPagesInRange($config['pagesInRange']); + } + } + /** * {@inheritdoc} */ diff --git a/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php b/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php index e9497a8..52de703 100644 --- a/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php +++ b/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php @@ -92,6 +92,102 @@ public function testPaginateFailZeroPageNumber() $this->paginator->paginate(0); } + public function testPaginateLowVolumeConstructorConfig() + { + $items = range(0, 27); + + $paginator = new Paginator(array( + 'itemTotalCallback' => function () use ($items) { + return count($items); + }, + 'sliceCallback' => function ($offset, $length) use ($items) { + return array_slice($items, $offset, $length); + }, + 'itemsPerPage' => 10, + 'pagesInRange' => 5, + )); + + $pagination = $paginator->paginate(1); + + $this->assertCount(10, $pagination->getItems()); + + $this->assertCount(3, $pagination->getPages()); + + $this->assertEquals(3, $pagination->getTotalNumberOfPages()); + + $this->assertEquals(1, $pagination->getCurrentPageNumber()); + + $this->assertEquals(1, $pagination->getFirstPageNumber()); + + $this->assertEquals(3, $pagination->getLastPageNumber()); + + $this->assertNull($pagination->getPreviousPageNumber()); + + $this->assertEquals(2, $pagination->getNextPageNumber()); + + $this->assertEquals(10, $pagination->getItemsPerPage()); + + $this->assertEquals(28, $pagination->getTotalNumberOfItems()); + + $this->assertEquals(1, $pagination->getFirstPageNumberInRange()); + + $this->assertEquals(3, $pagination->getLastPageNumberInRange()); + + // Increment page + $pagination = $paginator->paginate(2); + + $this->assertCount(10, $pagination->getItems()); + + $this->assertCount(3, $pagination->getPages()); + + $this->assertEquals(3, $pagination->getTotalNumberOfPages()); + + $this->assertEquals(2, $pagination->getCurrentPageNumber()); + + $this->assertEquals(1, $pagination->getFirstPageNumber()); + + $this->assertEquals(3, $pagination->getLastPageNumber()); + + $this->assertEquals(1, $pagination->getPreviousPageNumber()); + + $this->assertEquals(3, $pagination->getNextPageNumber()); + + $this->assertEquals(10, $pagination->getItemsPerPage()); + + $this->assertEquals(28, $pagination->getTotalNumberOfItems()); + + $this->assertEquals(1, $pagination->getFirstPageNumberInRange()); + + $this->assertEquals(3, $pagination->getLastPageNumberInRange()); + + // Increment page + $pagination = $paginator->paginate(3); + + $this->assertCount(8, $pagination->getItems()); + + $this->assertCount(3, $pagination->getPages()); + + $this->assertEquals(3, $pagination->getTotalNumberOfPages()); + + $this->assertEquals(3, $pagination->getCurrentPageNumber()); + + $this->assertEquals(1, $pagination->getFirstPageNumber()); + + $this->assertEquals(3, $pagination->getLastPageNumber()); + + $this->assertEquals(2, $pagination->getPreviousPageNumber()); + + $this->assertNull($pagination->getNextPageNumber()); + + $this->assertEquals(10, $pagination->getItemsPerPage()); + + $this->assertEquals(28, $pagination->getTotalNumberOfItems()); + + $this->assertEquals(1, $pagination->getFirstPageNumberInRange()); + + $this->assertEquals(3, $pagination->getLastPageNumberInRange()); + } + public function testPaginateLowVolume() { $items = range(0, 27);