-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathResultPager.php
200 lines (172 loc) · 4.09 KB
/
ResultPager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Github;
use Github\Api\ApiInterface;
use Github\Api\Search;
use Github\HttpClient\Message\ResponseMediator;
/**
* Pager class for supporting pagination in github classes.
*
* @author Ramon de la Fuente <[email protected]>
* @author Mitchel Verschoof <[email protected]>
*/
class ResultPager implements ResultPagerInterface
{
/**
* The GitHub Client to use for pagination.
*
* @var \Github\Client
*/
protected $client;
/**
* Comes from pagination headers in Github API results.
*
* @var array
*/
protected $pagination;
/**
* The Github client to use for pagination.
*
* This must be the same instance that you got the Api instance from.
*
* Example code:
*
* $client = new \Github\Client();
* $api = $client->api('someApi');
* $pager = new \Github\ResultPager($client);
*
* @param \Github\Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* {@inheritdoc}
*/
public function getPagination()
{
return $this->pagination;
}
/**
* {@inheritdoc}
*/
public function fetch(ApiInterface $api, $method, array $parameters = [])
{
$result = $this->callApi($api, $method, $parameters);
$this->postFetch();
return $result;
}
/**
* {@inheritdoc}
*/
public function fetchAll(ApiInterface $api, $method, array $parameters = [])
{
$isSearch = $api instanceof Search;
// get the perPage from the api
$perPage = $api->getPerPage();
// set parameters per_page to GitHub max to minimize number of requests
$api->setPerPage(100);
try {
$result = $this->callApi($api, $method, $parameters);
$this->postFetch();
if ($isSearch) {
$result = isset($result['items']) ? $result['items'] : $result;
}
while ($this->hasNext()) {
$next = $this->fetchNext();
if ($isSearch) {
$result = array_merge($result, $next['items']);
} else {
$result = array_merge($result, $next);
}
}
} finally {
// restore the perPage
$api->setPerPage($perPage);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function postFetch()
{
$this->pagination = ResponseMediator::getPagination($this->client->getLastResponse());
}
/**
* {@inheritdoc}
*/
public function hasNext()
{
return $this->has('next');
}
/**
* {@inheritdoc}
*/
public function fetchNext()
{
return $this->get('next');
}
/**
* {@inheritdoc}
*/
public function hasPrevious()
{
return $this->has('prev');
}
/**
* {@inheritdoc}
*/
public function fetchPrevious()
{
return $this->get('prev');
}
/**
* {@inheritdoc}
*/
public function fetchFirst()
{
return $this->get('first');
}
/**
* {@inheritdoc}
*/
public function fetchLast()
{
return $this->get('last');
}
/**
* @param string $key
*
* @return bool
*/
protected function has($key)
{
return !empty($this->pagination) && isset($this->pagination[$key]);
}
/**
* @param string $key
*
* @return array
*/
protected function get($key)
{
if ($this->has($key)) {
$result = $this->client->getHttpClient()->get($this->pagination[$key]);
$this->postFetch();
return ResponseMediator::getContent($result);
}
return [];
}
/**
* @param ApiInterface $api
* @param string $method
* @param array $parameters
*
* @return mixed
*/
protected function callApi(ApiInterface $api, $method, array $parameters)
{
return call_user_func_array([$api, $method], $parameters);
}
}