Skip to content

Commit

Permalink
Merge pull request #42 from feyobekius/master
Browse files Browse the repository at this point in the history
add possibility to fetch more than 60 results.
  • Loading branch information
stephangroen committed Feb 2, 2016
2 parents cdda5cb + 6028662 commit b2abf49
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class Connection
*/
protected $middleWares = [];


/**
* @var
*/
public $nextUrl = null;

/**
* @return Client
*/
Expand Down Expand Up @@ -173,7 +179,7 @@ private function createRequest($method = 'GET', $endpoint, $body = null, array $
*/
public function get($url, array $params = [])
{
$url = $this->formatUrl($url, $url !== 'current/Me');
$url = $this->formatUrl($url, $url !== 'current/Me', $url == $this->nextUrl);

try {
$request = $this->createRequest('GET', $url, null, $params);
Expand Down Expand Up @@ -343,6 +349,12 @@ private function parseResponse(Response $response)
if (count($json['d']['results']) == 1) {
return $json['d']['results'][0];
}
if (array_key_exists('__next', $json['d'])) {
$this->nextUrl = $json['d']['__next'];
}
else {
$this->nextUrl = null;
}

return $json['d']['results'];
}
Expand Down Expand Up @@ -462,8 +474,12 @@ private function tokenHasExpired()
return $this->tokenExpires <= time();
}

private function formatUrl($endPoint, $includeDivision = true)
private function formatUrl($endPoint, $includeDivision = true, $formatNextUrl = false)
{
if ($formatNextUrl) {
return $endPoint;
}

if ($includeDivision) {
return implode('/', [
$this->getApiUrl(),
Expand Down Expand Up @@ -581,5 +597,3 @@ public function setTokenUrl($tokenUrl)
$this->tokenUrl = $tokenUrl;
}
}


5 changes: 5 additions & 0 deletions src/Picqer/Financials/Exact/Query/Findable.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function collectionFromResult($result)
$result = [ $result ];
}

while ($this->connection()->nextUrl !== null)
{
$nextResult = $this->connection()->get($this->connection()->nextUrl);
$result = array_merge($result, $nextResult);
}
$collection = [ ];
foreach ($result as $r) {
$collection[] = new self($this->connection(), $r);
Expand Down

0 comments on commit b2abf49

Please sign in to comment.