Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:AshleyDawson/SimplePagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Dawson committed May 18, 2015
2 parents c0a926a + 0f81f04 commit 5877fa6
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -172,6 +172,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
));
```

<a name="pagination-object"></a>Pagination Object
-------------------------------------------------

Expand Down
28 changes: 28 additions & 0 deletions lib/AshleyDawson/SimplePagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ class Paginator implements PaginatorInterface
*/
private $pagesInRange = 5;

/**
* Constructor - passing optional configuration
*
* <code>
* $paginator = new Paginator(array(
* 'itemTotalCallback' => function () {
* // ...
* },
* 'sliceCallback' => function ($offset, $length) {
* // ...
* },
* 'itemsPerPage' => 10,
* 'pagesInRange' => 5
* ));
* </code>
*
* @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}
*/
Expand Down
96 changes: 96 additions & 0 deletions tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5877fa6

Please sign in to comment.