Skip to content

Commit a57df05

Browse files
Redesigned pagination
1 parent 369c07c commit a57df05

File tree

9 files changed

+153
-156
lines changed

9 files changed

+153
-156
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGE LOG
88
* Switched to PSR-17 and PSR-18
99
* Encode URIs according to RFC 3986
1010
* Send request bodies as JSON to GitLab
11+
* Redesigned pagination
1112

1213

1314
## 9.18.0 (11/07/2020)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"psr/http-client-implementation": "^1.0",
3535
"psr/http-factory-implementation": "^1.0",
3636
"psr/http-message": "^1.0",
37-
"symfony/options-resolver": "^3.4 || ^4.0 || ^5.0"
37+
"symfony/options-resolver": "^3.4 || ^4.0 || ^5.0",
38+
"symfony/polyfill-php80": "^1.17"
3839
},
3940
"require-dev": {
4041
"bamarni/composer-bin-plugin": "^1.4.1",

src/Api/AbstractApi.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\StreamFactoryInterface;
1515
use Psr\Http\Message\StreamInterface;
1616
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
use ValueError;
1718

1819
/**
1920
* @author Joseph Bielawski <[email protected]>
@@ -30,28 +31,57 @@ abstract class AbstractApi implements ApiInterface
3031
private const URI_PREFIX = '/api/v4/';
3132

3233
/**
33-
* The HTTP methods client.
34+
* The client instance.
3435
*
35-
* @var HttpMethodsClientInterface
36+
* @var Client
3637
*/
37-
private $httpClient;
38+
private $client;
3839

3940
/**
40-
* The HTTP stream factory.
41+
* The per page parameter.
4142
*
42-
* @var StreamFactoryInterface
43+
* @var int|null
4344
*/
44-
private $streamFactory;
45+
private $perPage;
4546

4647
/**
47-
* @param Client $client
48+
* Create a new API instance.
49+
*
50+
* @param Client $client
51+
* @param int|null $perPage
4852
*
4953
* @return void
5054
*/
51-
public function __construct(Client $client)
55+
public function __construct(Client $client, ?int $perPage)
5256
{
53-
$this->httpClient = $client->getHttpClient();
54-
$this->streamFactory = $client->getStreamFactory();
57+
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
58+
throw new ValueError(sprintf('%s::__construct(): Argument #2 ($perPage) must be between 1 and 100, or null', self::class));
59+
}
60+
61+
$this->client = $client;
62+
$this->perPage = $perPage;
63+
}
64+
65+
/**
66+
* Create a new instance with the given page parameter.
67+
*
68+
* This must be an integer between 1 and 100.
69+
*
70+
* @param int|null $perPage
71+
*
72+
* @return static
73+
*/
74+
public function perPage(?int $perPage)
75+
{
76+
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
77+
throw new ValueError(sprintf('%s::perPage(): Argument #1 ($perPage) must be between 1 and 100, or null', self::class));
78+
}
79+
80+
$copy = clone $this;
81+
82+
$copy->perPage = $perPage;
83+
84+
return $copy;
5585
}
5686

5787
/**
@@ -67,7 +97,11 @@ public function __construct(Client $client)
6797
*/
6898
protected function getAsResponse($uri, array $params = [], array $headers = [])
6999
{
70-
return $this->httpClient->get(self::prepareUri($uri, $params), $headers);
100+
if (null !== $this->perPage && !isset($params['per_page'])) {
101+
$params['per_page'] = $this->perPage;
102+
}
103+
104+
return $this->client->getHttpClient()->get(self::prepareUri($uri, $params), $headers);
71105
}
72106

73107
/**
@@ -106,7 +140,7 @@ protected function post($uri, array $params = [], array $headers = [], array $fi
106140
}
107141
}
108142

109-
$response = $this->httpClient->post(self::prepareUri($uri), $headers, $body);
143+
$response = $this->client->getHttpClient()->post(self::prepareUri($uri), $headers, $body);
110144

111145
return ResponseMediator::getContent($response);
112146
}
@@ -133,7 +167,7 @@ protected function put($uri, array $params = [], array $headers = [], array $fil
133167
}
134168
}
135169

136-
$response = $this->httpClient->put(self::prepareUri($uri), $headers, $body ?? '');
170+
$response = $this->client->getHttpClient()->put(self::prepareUri($uri), $headers, $body ?? '');
137171

138172
return ResponseMediator::getContent($response);
139173
}
@@ -153,7 +187,7 @@ protected function delete($uri, array $params = [], array $headers = [])
153187
$headers = self::addJsonContentType($headers);
154188
}
155189

156-
$response = $this->httpClient->delete(self::prepareUri($uri), $headers, $body ?? '');
190+
$response = $this->client->getHttpClient()->delete(self::prepareUri($uri), $headers, $body ?? '');
157191

158192
return ResponseMediator::getContent($response);
159193
}
@@ -241,7 +275,7 @@ private static function prepareUri(string $uri, array $query = [])
241275
*/
242276
private function createMultipartStreamBuilder(array $params = [], array $files = [])
243277
{
244-
$builder = new MultipartStreamBuilder($this->streamFactory);
278+
$builder = new MultipartStreamBuilder($this->client->getStreamFactory());
245279

246280
foreach ($params as $name => $value) {
247281
$builder->addResource($name, $value);

src/Api/ApiInterface.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66

77
use Gitlab\Client;
88

9-
/**
10-
* Api interface.
11-
*/
129
interface ApiInterface
1310
{
14-
public function __construct(Client $client);
11+
/**
12+
* Create a new instance with the given per page parameter.
13+
*
14+
* This must be an integer between 1 and 100.
15+
*
16+
* @param int|null $perPage
17+
*
18+
* @return static
19+
*/
20+
public function perPage(?int $perPage);
1521
}

src/Client.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,199 +107,199 @@ public static function createWithHttpClient(ClientInterface $httpClient)
107107
*/
108108
public function deployKeys()
109109
{
110-
return new Api\DeployKeys($this);
110+
return new Api\DeployKeys($this, null);
111111
}
112112

113113
/**
114114
* @return Api\Deployments
115115
*/
116116
public function deployments()
117117
{
118-
return new Api\Deployments($this);
118+
return new Api\Deployments($this, null);
119119
}
120120

121121
/**
122122
* @return Api\Environments
123123
*/
124124
public function environments()
125125
{
126-
return new Api\Environments($this);
126+
return new Api\Environments($this, null);
127127
}
128128

129129
/**
130130
* @return Api\Groups
131131
*/
132132
public function groups()
133133
{
134-
return new Api\Groups($this);
134+
return new Api\Groups($this, null);
135135
}
136136

137137
/**
138138
* @return Api\GroupsBoards
139139
*/
140140
public function groupsBoards()
141141
{
142-
return new Api\GroupsBoards($this);
142+
return new Api\GroupsBoards($this, null);
143143
}
144144

145145
/**
146146
* @return Api\GroupsMilestones
147147
*/
148148
public function groupsMilestones()
149149
{
150-
return new Api\GroupsMilestones($this);
150+
return new Api\GroupsMilestones($this, null);
151151
}
152152

153153
/**
154154
* @return Api\IssueBoards
155155
*/
156156
public function issueBoards()
157157
{
158-
return new Api\IssueBoards($this);
158+
return new Api\IssueBoards($this, null);
159159
}
160160

161161
/**
162162
* @return Api\IssueLinks
163163
*/
164164
public function issueLinks()
165165
{
166-
return new Api\IssueLinks($this);
166+
return new Api\IssueLinks($this, null);
167167
}
168168

169169
/**
170170
* @return Api\Issues
171171
*/
172172
public function issues()
173173
{
174-
return new Api\Issues($this);
174+
return new Api\Issues($this, null);
175175
}
176176

177177
/**
178178
* @return Api\IssuesStatistics
179179
*/
180180
public function issuesStatistics()
181181
{
182-
return new Api\IssuesStatistics($this);
182+
return new Api\IssuesStatistics($this, null);
183183
}
184184

185185
/**
186186
* @return Api\Jobs
187187
*/
188188
public function jobs()
189189
{
190-
return new Api\Jobs($this);
190+
return new Api\Jobs($this, null);
191191
}
192192

193193
/**
194194
* @return Api\Keys
195195
*/
196196
public function keys()
197197
{
198-
return new Api\Keys($this);
198+
return new Api\Keys($this, null);
199199
}
200200

201201
/**
202202
* @return Api\MergeRequests
203203
*/
204204
public function mergeRequests()
205205
{
206-
return new Api\MergeRequests($this);
206+
return new Api\MergeRequests($this, null);
207207
}
208208

209209
/**
210210
* @return Api\Milestones
211211
*/
212212
public function milestones()
213213
{
214-
return new Api\Milestones($this);
214+
return new Api\Milestones($this, null);
215215
}
216216

217217
/**
218218
* @return Api\ProjectNamespaces
219219
*/
220220
public function namespaces()
221221
{
222-
return new Api\ProjectNamespaces($this);
222+
return new Api\ProjectNamespaces($this, null);
223223
}
224224

225225
/**
226226
* @return Api\Projects
227227
*/
228228
public function projects()
229229
{
230-
return new Api\Projects($this);
230+
return new Api\Projects($this, null);
231231
}
232232

233233
/**
234234
* @return Api\Repositories
235235
*/
236236
public function repositories()
237237
{
238-
return new Api\Repositories($this);
238+
return new Api\Repositories($this, null);
239239
}
240240

241241
/**
242242
* @return Api\RepositoryFiles
243243
*/
244244
public function repositoryFiles()
245245
{
246-
return new Api\RepositoryFiles($this);
246+
return new Api\RepositoryFiles($this, null);
247247
}
248248

249249
/**
250250
* @return Api\Schedules
251251
*/
252252
public function schedules()
253253
{
254-
return new Api\Schedules($this);
254+
return new Api\Schedules($this, null);
255255
}
256256

257257
/**
258258
* @return Api\Snippets
259259
*/
260260
public function snippets()
261261
{
262-
return new Api\Snippets($this);
262+
return new Api\Snippets($this, null);
263263
}
264264

265265
/**
266266
* @return Api\SystemHooks
267267
*/
268268
public function systemHooks()
269269
{
270-
return new Api\SystemHooks($this);
270+
return new Api\SystemHooks($this, null);
271271
}
272272

273273
/**
274274
* @return Api\Users
275275
*/
276276
public function users()
277277
{
278-
return new Api\Users($this);
278+
return new Api\Users($this, null);
279279
}
280280

281281
/**
282282
* @return Api\Tags
283283
*/
284284
public function tags()
285285
{
286-
return new Api\Tags($this);
286+
return new Api\Tags($this, null);
287287
}
288288

289289
/**
290290
* @return Api\Version
291291
*/
292292
public function version()
293293
{
294-
return new Api\Version($this);
294+
return new Api\Version($this, null);
295295
}
296296

297297
/**
298298
* @return Api\Wiki
299299
*/
300300
public function wiki()
301301
{
302-
return new Api\Wiki($this);
302+
return new Api\Wiki($this, null);
303303
}
304304

305305
/**

0 commit comments

Comments
 (0)