diff --git a/README.md b/README.md
index 9d92a0a..1970b0e 100644
--- a/README.md
+++ b/README.md
@@ -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)
@@ -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)
@@ -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 . '
';
+ }
+}
+```
+
Pagination Object
-------------------------------------------------
diff --git a/lib/AshleyDawson/SimplePagination/Pagination.php b/lib/AshleyDawson/SimplePagination/Pagination.php
index 955cf5c..be1353a 100644
--- a/lib/AshleyDawson/SimplePagination/Pagination.php
+++ b/lib/AshleyDawson/SimplePagination/Pagination.php
@@ -8,17 +8,17 @@
* @package AshleyDawson\SimplePagination
* @author Ashley Dawson
*/
-class Pagination
+class Pagination implements \IteratorAggregate, \Countable
{
/**
* @var mixed
*/
- private $items;
+ private $items = array();
/**
* @var array
*/
- private $pages;
+ private $pages = array();
/**
* @var int
@@ -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);
+ }
}
\ No newline at end of file
diff --git a/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php b/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php
index 52de703..eb561d0 100644
--- a/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php
+++ b/tests/AshleyDawson/SimplePagination/Tests/PaginatorTest.php
@@ -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);