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 20, 2015
2 parents 8227ed5 + b394af1 commit a155be1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ $paginator->setSliceCallback(function ($offset, $length) use ($items) {
});

// Paginate the item collection, passing the current page number (e.g. from the current request)
$pagination = $paginator->paginate((int)$_GET['page']);
$pagination = $paginator->paginate((int) $_GET['page']);

// Ok, from here on is where we'd be inside a template of view (e.g. pass $pagination to your view)

Expand Down Expand Up @@ -153,7 +153,7 @@ $paginator->setSliceCallback(function ($offset, $length) {
});

// Paginate the item collection, passing the current page number (e.g. from the current request)
$pagination = $paginator->paginate((int)$_GET['page']);
$pagination = $paginator->paginate((int) $_GET['page']);

// Ok, from here on is where we'd be inside a template of view (e.g. pass $pagination to your view)

Expand Down Expand Up @@ -190,6 +190,20 @@ $paginator = new Paginator(array(
));
```

Pagination as an Iterator
-------------------------

The Pagination object returned from the Paginator service implements \IteratorAggregate and \Countable so you can do things like this in your view:

```php
if (count($pagination) > 0)
{
foreach ($pagination as $item) {
echo $item . '<br />';
}
}
```

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

Expand Down
22 changes: 19 additions & 3 deletions lib/AshleyDawson/SimplePagination/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
* @package AshleyDawson\SimplePagination
* @author Ashley Dawson <[email protected]>
*/
class Pagination
class Pagination implements \IteratorAggregate, \Countable
{
/**
* @var mixed
*/
private $items;
private $items = array();

/**
* @var array
*/
private $pages;
private $pages = array();

/**
* @var int
Expand Down Expand Up @@ -333,4 +333,20 @@ public function setTotalNumberOfPages($totalNumberOfPages)
$this->totalNumberOfPages = $totalNumberOfPages;
return $this;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->items);
}

/**
* {@inheritdoc}
*/
public function count()
{
return count($this->items);
}
}
32 changes: 32 additions & 0 deletions tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,38 @@ public function testPaginateLowVolumeConstructorConfig()
$this->assertEquals(3, $pagination->getLastPageNumberInRange());
}

public function testPaginationIteratorAggregate()
{
$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' => 15,
'pagesInRange' => 5,
));

$pagination = $paginator->paginate(1);

$this->assertInstanceOf('\IteratorAggregate', $pagination);

$this->assertInstanceOf('\Countable', $pagination);

$this->assertCount(15, $pagination);

$iterations = 0;
foreach ($pagination as $i => $item) {
$this->assertEquals($i, $item);
$iterations ++;
}

$this->assertEquals(15, $iterations);
}

public function testPaginateLowVolume()
{
$items = range(0, 27);
Expand Down

0 comments on commit a155be1

Please sign in to comment.