-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathPaginator.php
158 lines (124 loc) · 2.53 KB
/
Paginator.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
<?php
namespace Milo\Github;
/**
* Iterates through the Github API responses by Link: header.
*
* @see https://developer.github.com/guides/traversing-with-pagination/
*
* @author Miloslav Hůla (https://github.com/milo)
*/
class Paginator extends Sanity implements \Iterator
{
/** @var Api */
private $api;
/** @var Http\Request */
private $firstRequest;
/** @var Http\Request|NULL */
private $request;
/** @var Http\Response|NULL */
private $response;
/** @var int */
private $limit;
/** @var int */
private $counter = 0;
public function __construct(Api $api, Http\Request $request)
{
$this->api = $api;
$this->firstRequest = clone $request;
}
/**
* Limits maximum steps of iteration.
*
* @param int|NULL
* @return self
*/
public function limit($limit)
{
$this->limit = $limit === NULL
? NULL
: (int) $limit;
return $this;
}
/**
* @return void
*/
public function rewind()
{
$this->request = $this->firstRequest;
$this->response = NULL;
$this->counter = 0;
}
/**
* @return bool
*/
public function valid()
{
return $this->request !== NULL && ($this->limit === NULL || $this->counter < $this->limit);
}
/**
* @return Http\Response
*/
public function current()
{
$this->load();
return $this->response;
}
/**
* @return int
*/
public function key()
{
return static::parsePage($this->request->getUrl());
}
/**
* @return void
*/
public function next()
{
$this->load();
if ($url = static::parseLink($this->response->getHeader('Link'), 'next')) {
$this->request = new Http\Request(
$this->request->getMethod(),
$url,
$this->request->getHeaders(),
$this->request->getContent()
);
} else {
$this->request = NULL;
}
$this->response = NULL;
$this->counter++;
}
private function load()
{
if ($this->response === NULL) {
$this->response = $this->api->request($this->request);
}
}
/**
* @param string
* @return int
*/
public static function parsePage($url)
{
list (, $parametersStr) = explode('?', $url, 2) + ['', ''];
parse_str($parametersStr, $parameters);
return isset($parameters['page'])
? max(1, (int) $parameters['page'])
: 1;
}
/**
* @see https://developer.github.com/guides/traversing-with-pagination/#navigating-through-the-pages
*
* @param string
* @param string
* @return string|NULL
*/
public static function parseLink($link, $rel)
{
if (!preg_match('(<([^>]+)>;\s*rel="' . preg_quote($rel) . '")', $link, $match)) {
return NULL;
}
return $match[1];
}
}