Skip to content

Commit 336f50a

Browse files
Renamed paginator to ResultPager, added resultPagerInterface
1 parent 09b147d commit 336f50a

File tree

3 files changed

+216
-68
lines changed

3 files changed

+216
-68
lines changed

lib/Github/ResultPager.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
/**
11+
* Pager class for supporting pagination in github classes
12+
*
13+
* @author Ramon de la Fuente <ramon@future500.nl>
14+
* @author Mitchel Verschoof <mitchel@future500.nl>
15+
*/
16+
class ResultPager implements ResultPagerInterface
17+
{
18+
/**
19+
* @var Github\Client client
20+
*/
21+
protected $client;
22+
23+
/**
24+
* @var array pagination
25+
* Comes from pagination headers in Github API results
26+
*/
27+
protected $pagination = null;
28+
29+
public function __construct( Client $client )
30+
{
31+
$this->client = $client;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function fetch( ApiInterface $api, $method )
38+
{
39+
$parameters = array_slice(func_get_args(),2);
40+
41+
$result = call_user_func_array(array($api, $method), $parameters);
42+
$this->postFetch();
43+
44+
return $result;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function fetchAll( ApiInterface $api, $method )
51+
{
52+
$parameters = array_slice(func_get_args(),2);
53+
54+
// Set parameters per_page to GitHub max to minimize number of requests
55+
$api->setPerPage(100);
56+
57+
$result = array();
58+
$result = call_user_func_array(array($api, $method), $parameters);
59+
$this->postFetch();
60+
61+
while ($this->hasNext()) {
62+
$result = array_merge($result, $this->fetchNext());
63+
}
64+
65+
return $result;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function postFetch()
72+
{
73+
$this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination();
74+
var_dump( $this->pagination );
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function hasNext()
81+
{
82+
return $this->has('next');
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function fetchNext()
89+
{
90+
return $this->get('next');
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function hasPrevious()
97+
{
98+
return $this->has('prev');
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function fetchPrevious()
105+
{
106+
return $this->get('prev');
107+
}
108+
109+
/**
110+
* {@inheritdoc}
111+
*/
112+
public function fetchFirst()
113+
{
114+
return $this->get('first');
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function fetchLast()
121+
{
122+
return $this->get('last');
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
protected function has($key)
129+
{
130+
if (!empty($this->pagination) and isset($this->pagination[$key])) {
131+
return true;
132+
}
133+
134+
return false;
135+
}
136+
137+
/**
138+
* {@inheritdoc}
139+
*/
140+
protected function get($key)
141+
{
142+
if ( $this->has($key) ) {
143+
$result = $this->client->getHttpClient()->get($this->pagination[$key]);
144+
$this->postFetch();
145+
146+
return $result->getContent();
147+
}
148+
}
149+
150+
}

lib/Github/ResultPagerInterface.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Github;
4+
5+
use Github\Api\ApiInterface;
6+
7+
/**
8+
* Pager interface
9+
*
10+
* @author Ramon de la Fuente <ramon@future500.nl>
11+
* @author Mitchel Verschoof <mitchel@future500.nl>
12+
*/
13+
interface ResultPagerInterface
14+
{
15+
/**
16+
* Fetch a single result (page) from an api call
17+
*/
18+
public function fetch( ApiInterface $api, $method );
19+
20+
/**
21+
* Fetch all results (pages) from an api call
22+
* Use with care - there is no maximum
23+
*/
24+
public function fetchAll( ApiInterface $api, $method );
25+
26+
/**
27+
* Method that performs the actual work to refresh the pagination property
28+
*/
29+
public function postFetch();
30+
31+
/**
32+
* Check to determine the availability of a next page
33+
* @return bool
34+
*/
35+
public function hasNext();
36+
37+
/**
38+
* Check to determine the availability of a previous page
39+
* @return bool
40+
*/
41+
public function hasPrevious();
42+
43+
/**
44+
* Fetch the next page
45+
* @return array
46+
*/
47+
public function fetchNext();
48+
49+
/**
50+
* Fetch the previous page
51+
* @return array
52+
*/
53+
public function fetchPrevious();
54+
55+
/**
56+
* Fetch the first page
57+
* @return array
58+
*/
59+
public function fetchFirst();
60+
61+
/**
62+
* Fetch the last page
63+
* @return array
64+
*/
65+
public function fetchLast();
66+
}

lib/Github/ResultPaginator.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)