Skip to content

Commit ce09c70

Browse files
committed
Added way to parse pagination headers from GitHub.
1 parent 839e518 commit ce09c70

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

lib/Github/HttpClient/HttpClient.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class HttpClient implements HttpClientInterface
5656
*/
5757
protected $browser;
5858

59+
/**
60+
* @var array
61+
*/
62+
private $lastResponse;
63+
5964
/**
6065
* Instantiated a new http client
6166
*
@@ -101,6 +106,18 @@ public function setOption($name, $value)
101106
return $this;
102107
}
103108

109+
/**
110+
* @return null|array
111+
*/
112+
public function getPagination()
113+
{
114+
if (null === $this->lastResponse) {
115+
return null;
116+
}
117+
118+
return $this->lastResponse['pagination'];
119+
}
120+
104121
/**
105122
* {@inheritDoc}
106123
*/
@@ -162,9 +179,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
162179
));
163180

164181
// get encoded response
165-
$response = $this->doRequest($url, $parameters, $httpMethod, $options);
182+
$this->lastResponse = $this->doRequest($url, $parameters, $httpMethod, $options);
166183

167-
return $this->decodeResponse($response['response']);
184+
return $this->decodeResponse($this->lastResponse['response']);
168185
}
169186

170187
/**
@@ -186,11 +203,36 @@ protected function doRequest($url, array $parameters = array(), $httpMethod = 'G
186203
return array(
187204
'response' => $response->getContent(),
188205
'headers' => $response->getHeaders(),
206+
'pagination' => $this->decodePagination($response),
189207
'errorNumber' => '',
190208
'errorMessage' => ''
191209
);
192210
}
193211

212+
/**
213+
* @param MessageInterface $response
214+
*
215+
* @return array|null
216+
*/
217+
protected function decodePagination(MessageInterface $response)
218+
{
219+
$header = $response->getHeader('Link');
220+
if (empty($header)) {
221+
return null;
222+
}
223+
224+
$pagination = array();
225+
foreach (explode("\n", $header) as $link) {
226+
preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match);
227+
228+
if (3 === count($match)) {
229+
$pagination[$match[2]] = $match[1];
230+
}
231+
}
232+
233+
return $pagination;
234+
}
235+
194236
/**
195237
* {@inheritDoc}
196238
*/

0 commit comments

Comments
 (0)