From f0349f5ed1089445eec1222fe25b44bc877bb2cf Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 15 Aug 2017 15:26:04 +0100 Subject: [PATCH 1/3] Preserve the pagination data in the result set --- src/ResultSet.php | 26 +++++++++++++++++++++- tests/TestCase/ResultSetTest.php | 37 ++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/ResultSet.php b/src/ResultSet.php index af0f904..ba5ef09 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -31,18 +31,32 @@ class ResultSet implements ResultSetInterface */ protected $_results = []; + /** + * Total number of records + * + * @var int + */ protected $_total; + /** + * Array of pagination data + * + * @var array + */ + protected $_pagination; + /** * Construct the ResultSet * * @param array $resources The resources to attach + * @param array $pagination Array of pagination data * @param int|null $total The total amount of resources available */ - public function __construct(array $resources, $total = null) + public function __construct(array $resources, $total = null, array $pagination = []) { $this->_results = \SplFixedArray::fromArray($resources, false); $this->_total = $total; + $this->_pagination = $pagination; } /** @@ -128,4 +142,14 @@ public function total() { return $this->_total; } + + /** + * Return the pagination data array + * + * @return array + */ + public function pagination() + { + return $this->_pagination; + } } diff --git a/tests/TestCase/ResultSetTest.php b/tests/TestCase/ResultSetTest.php index de7b2f4..a7ed62f 100644 --- a/tests/TestCase/ResultSetTest.php +++ b/tests/TestCase/ResultSetTest.php @@ -21,20 +21,24 @@ public function setUp() { parent::setUp(); - $this->resultSet = new ResultSet([ - new Resource([ - 'id' => 1, - 'title' => 'Hello World' - ]), - new Resource([ - 'id' => 2, - 'title' => 'New ORM' - ]), - new Resource([ - 'id' => 3, - 'title' => 'Webservices' - ]) - ], 6); + $this->resultSet = new ResultSet( + [ + new Resource([ + 'id' => 1, + 'title' => 'Hello World' + ]), + new Resource([ + 'id' => 2, + 'title' => 'New ORM' + ]), + new Resource([ + 'id' => 3, + 'title' => 'Webservices' + ]) + ], + 6, + ['count' => 6, 'prevPage' => false, 'nextPage' => false] + ); } public function testCount() @@ -59,6 +63,11 @@ public function testUnserialize() $this->assertInstanceOf('\Muffin\Webservice\ResultSet', $unserialized); } + public function testPagination() + { + $this->assertEquals(['count' => 6, 'prevPage' => false, 'nextPage' => false], $this->resultSet->pagination()); + } + /** * @inheritDoc */ From 04986493a333fcadd5bfd43877dca92408789842 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 15 Aug 2017 16:46:05 +0100 Subject: [PATCH 2/3] Fixed the PHPCS errors --- src/ResultSet.php | 10 +++++----- tests/TestCase/ResultSetTest.php | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ResultSet.php b/src/ResultSet.php index ba5ef09..4795eed 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -33,14 +33,14 @@ class ResultSet implements ResultSetInterface /** * Total number of records - * + * * @var int */ protected $_total; /** * Array of pagination data - * + * * @var array */ protected $_pagination; @@ -49,8 +49,8 @@ class ResultSet implements ResultSetInterface * Construct the ResultSet * * @param array $resources The resources to attach - * @param array $pagination Array of pagination data - * @param int|null $total The total amount of resources available + * @param array $total Array of pagination data + * @param null $pagination The total amount of resources available */ public function __construct(array $resources, $total = null, array $pagination = []) { @@ -145,7 +145,7 @@ public function total() /** * Return the pagination data array - * + * * @return array */ public function pagination() diff --git a/tests/TestCase/ResultSetTest.php b/tests/TestCase/ResultSetTest.php index a7ed62f..2ec30b7 100644 --- a/tests/TestCase/ResultSetTest.php +++ b/tests/TestCase/ResultSetTest.php @@ -35,8 +35,8 @@ public function setUp() 'id' => 3, 'title' => 'Webservices' ]) - ], - 6, + ], + 6, ['count' => 6, 'prevPage' => false, 'nextPage' => false] ); } From db914e82e656676561ab6e7c9b881a5a72e00522 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 15 Aug 2017 16:48:53 +0100 Subject: [PATCH 3/3] Fixed an incorrect docBlock --- src/ResultSet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ResultSet.php b/src/ResultSet.php index 4795eed..1355b49 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -49,8 +49,8 @@ class ResultSet implements ResultSetInterface * Construct the ResultSet * * @param array $resources The resources to attach - * @param array $total Array of pagination data - * @param null $pagination The total amount of resources available + * @param int $total The total amount of resources available + * @param array $pagination Array of pagination data */ public function __construct(array $resources, $total = null, array $pagination = []) {