Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the pagination data in the result set #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 int|null $total 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)
public function __construct(array $resources, $total = null, array $pagination = [])
{
$this->_results = \SplFixedArray::fromArray($resources, false);
$this->_total = $total;
$this->_pagination = $pagination;
}

/**
Expand Down Expand Up @@ -128,4 +142,14 @@ public function total()
{
return $this->_total;
}

/**
* Return the pagination data array
*
* @return array
*/
public function pagination()
{
return $this->_pagination;
}
}
37 changes: 23 additions & 14 deletions tests/TestCase/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
*/
Expand Down