Skip to content

Commit 3683986

Browse files
Added the ResultPaginator (incomplete)
This is the first pass at adding a paginator class for github API requests. It only works with fetchAll() at the moment, to demonstrate the usage. Currently: $paginator = new \Github\ResultPaginator( \Github\Client [client] ); $paginator->fetchAll( [api], [method], [parameters] ); Todo's include: Paginator is not complete for all possible headers Next, Last, Previous, First apiClient should be able to return a paginator (i.e. $client->getPaginator() ) CacheHttpClient does not work (does not return Link headers)
1 parent 07138eb commit 3683986

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

lib/Github/ResultPaginator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Github;
4+
5+
use Github\Api\ApiInterface;
6+
use Github\Exception\InvalidArgumentException;
7+
use Github\HttpClient\HttpClient;
8+
use Github\HttpClient\HttpClientInterface;
9+
10+
class ResultPaginator
11+
{
12+
protected $client;
13+
protected $apiClass;
14+
15+
protected $pagination = null;
16+
17+
public function __construct( Client $client ) {
18+
$this->client = $client;
19+
}
20+
21+
public function fetchAll( ApiInterface $api, $method, $parameters ) {
22+
$result = array();
23+
$result = call_user_func_array( array($api,$method), $parameters);
24+
$this->postFetch();
25+
26+
while ($this->hasNext()) {
27+
$result = array_merge($result, $this->fetchNext());
28+
}
29+
return $result;
30+
}
31+
32+
protected function postFetch() {
33+
$this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination();
34+
var_dump($this->pagination);
35+
}
36+
37+
public function fetchNext() {
38+
$result = $this->client->getHttpClient()->get($this->pagination['next']);
39+
$this->postFetch();
40+
return $result->getContent();
41+
}
42+
43+
public function hasNext() {
44+
if (!empty($this->pagination) and isset($this->pagination['next'])) {
45+
return true;
46+
}
47+
return false;
48+
}
49+
50+
public function hasPrevious() {
51+
if (!empty($this->pagination) and isset($this->pagination['previous'])) {
52+
return true;
53+
}
54+
return false;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)