forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultPager.php
More file actions
163 lines (139 loc) · 3.28 KB
/
ResultPager.php
File metadata and controls
163 lines (139 loc) · 3.28 KB
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
<?php
namespace Github;
use Github\Api\ApiInterface;
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
{
/**
* @var \Github\Client client
*/
protected $client;
/**
* @var array pagination
* Comes from pagination headers in Github API results
*/
protected $pagination;
/**
* The Github client to use for pagination. This must be the same
* instance that you got the Api instance from, i.e.:
*
* $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 = array())
{
$result = call_user_func_array(array($api, $method), $parameters);
$this->postFetch();
return $result;
}
/**
* {@inheritdoc}
*/
public function fetchAll(ApiInterface $api, $method, array $parameters = array())
{
// get the perPage from the api
$perPage = $api->getPerPage();
// Set parameters per_page to GitHub max to minimize number of requests
$api->setPerPage(100);
$result = array();
$result = call_user_func_array(array($api, $method), $parameters);
$this->postFetch();
while ($this->hasNext()) {
$result = array_merge($result, $this->fetchNext());
}
// restore the perPage
$api->setPerPage($perPage);
return $result;
}
/**
* {@inheritdoc}
*/
public function postFetch()
{
$this->pagination = ResponseMediator::getPagination($this->client->getHttpClient()->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');
}
/**
* {@inheritdoc}
*/
protected function has($key)
{
return !empty($this->pagination) && isset($this->pagination[$key]);
}
/**
* {@inheritdoc}
*/
protected function get($key)
{
if ($this->has($key)) {
$result = $this->client->getHttpClient()->get($this->pagination[$key]);
$this->postFetch();
return ResponseMediator::getContent($result);
}
}
}