Skip to content

Commit 54c652c

Browse files
Re-worked pagination to not mutate the api classes
1 parent 58ec8f5 commit 54c652c

26 files changed

+211
-216
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"psr/cache": "^1.0",
2727
"psr/http-client-implementation": "^1.0",
2828
"psr/http-factory-implementation": "^1.0",
29-
"psr/http-message": "^1.0"
29+
"psr/http-message": "^1.0",
30+
"symfony/polyfill-php80": "^1.17"
3031
},
3132
"require-dev": {
3233
"symfony/cache": "^5.1.8",

lib/Github/Api/AbstractApi.php

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,100 @@
44

55
use Github\Client;
66
use Github\HttpClient\Message\ResponseMediator;
7+
use ValueError;
78

89
/**
9-
* Abstract class for Api classes.
10-
*
1110
* @author Joseph Bielawski <[email protected]>
11+
* @author Graham Campbell <[email protected]>
1212
*/
1313
abstract class AbstractApi implements ApiInterface
1414
{
1515
/**
16-
* The client.
16+
* The client instance.
1717
*
1818
* @var Client
1919
*/
20-
protected $client;
20+
private $client;
2121

2222
/**
23-
* The requested page (GitHub pagination).
23+
* The per page parameter.
2424
*
25-
* @var null|int
25+
* @var int|null
2626
*/
27-
private $page;
27+
private $perPage;
2828

2929
/**
30-
* Number of items per page (GitHub pagination).
30+
* Create a new API instance.
3131
*
32-
* @var null|int
33-
*/
34-
protected $perPage;
35-
36-
/**
37-
* @param Client $client
32+
* @param Client $client
33+
* @param int|null $perPage
34+
*
35+
* @return void
3836
*/
39-
public function __construct(Client $client)
37+
public function __construct(Client $client, int $perPage = null)
4038
{
41-
$this->client = $client;
42-
}
39+
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
40+
throw new ValueError(sprintf('%s::__construct(): Argument #2 ($perPage) must be between 1 and 100, or null', self::class));
41+
}
4342

44-
public function configure()
45-
{
43+
$this->client = $client;
44+
$this->perPage = $perPage;
4645
}
4746

4847
/**
49-
* @return null|int
48+
* Get the client instance.
49+
*
50+
* @return Client
5051
*/
51-
public function getPage()
52+
protected function getClient()
5253
{
53-
return $this->page;
54+
return $this->client;
5455
}
5556

5657
/**
57-
* @param null|int $page
58+
* Get the API version.
59+
*
60+
* @return string
5861
*/
59-
public function setPage($page)
62+
protected function getApiVersion()
6063
{
61-
$this->page = (null === $page ? $page : (int) $page);
62-
63-
return $this;
64+
return $this->client->getApiVersion();
6465
}
6566

6667
/**
67-
* @return null|int
68+
* Get the number of values to fetch per page.
69+
*
70+
* @return int|null
6871
*/
69-
public function getPerPage()
72+
protected function getPerPage()
7073
{
7174
return $this->perPage;
7275
}
7376

7477
/**
75-
* @param null|int $perPage
78+
* Create a new instance with the given page parameter.
79+
*
80+
* This must be an integer between 1 and 100.
81+
*
82+
* @param int|null $perPage
83+
*
84+
* @return static
7685
*/
77-
public function setPerPage($perPage)
86+
public function perPage(?int $perPage)
7887
{
79-
$this->perPage = (null === $perPage ? $perPage : (int) $perPage);
88+
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
89+
throw new ValueError(sprintf('%s::perPage(): Argument #1 ($perPage) must be between 1 and 100, or null', self::class));
90+
}
91+
92+
$copy = clone $this;
93+
94+
$copy->perPage = $perPage;
8095

81-
return $this;
96+
return $copy;
97+
}
98+
99+
public function configure()
100+
{
82101
}
83102

84103
/**
@@ -92,12 +111,10 @@ public function setPerPage($perPage)
92111
*/
93112
protected function get($path, array $parameters = [], array $requestHeaders = [])
94113
{
95-
if (null !== $this->page && !isset($parameters['page'])) {
96-
$parameters['page'] = $this->page;
97-
}
98114
if (null !== $this->perPage && !isset($parameters['per_page'])) {
99115
$parameters['per_page'] = $this->perPage;
100116
}
117+
101118
if (array_key_exists('ref', $parameters) && null === $parameters['ref']) {
102119
unset($parameters['ref']);
103120
}

lib/Github/Api/ApiInterface.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
namespace Github\Api;
44

55
/**
6-
* Api interface.
7-
*
86
* @author Joseph Bielawski <[email protected]>
7+
* @author Graham Campbell <[email protected]>
98
*/
109
interface ApiInterface
1110
{
12-
public function getPerPage();
13-
14-
public function setPerPage($perPage);
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
}

lib/Github/Api/CurrentUser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public function update(array $params)
3535
*/
3636
public function emails()
3737
{
38-
return new Emails($this->client);
38+
return new Emails($this->getClient(), $this->getPerPage());
3939
}
4040

4141
/**
4242
* @return Followers
4343
*/
4444
public function follow()
4545
{
46-
return new Followers($this->client);
46+
return new Followers($this->getClient(), $this->getPerPage());
4747
}
4848

4949
public function followers($page = 1)
@@ -71,23 +71,23 @@ public function issues(array $params = [], $includeOrgIssues = true)
7171
*/
7272
public function keys()
7373
{
74-
return new PublicKeys($this->client);
74+
return new PublicKeys($this->getClient(), $this->getPerPage());
7575
}
7676

7777
/**
7878
* @return Notifications
7979
*/
8080
public function notifications()
8181
{
82-
return new Notifications($this->client);
82+
return new Notifications($this->getClient(), $this->getPerPage());
8383
}
8484

8585
/**
8686
* @return Memberships
8787
*/
8888
public function memberships()
8989
{
90-
return new Memberships($this->client);
90+
return new Memberships($this->getClient(), $this->getPerPage());
9191
}
9292

9393
/**
@@ -147,15 +147,15 @@ public function repositories($type = 'owner', $sort = 'full_name', $direction =
147147
*/
148148
public function watchers()
149149
{
150-
return new Watchers($this->client);
150+
return new Watchers($this->getClient(), $this->getPerPage());
151151
}
152152

153153
/**
154154
* @return Starring
155155
*/
156156
public function starring()
157157
{
158-
return new Starring($this->client);
158+
return new Starring($this->getClient(), $this->getPerPage());
159159
}
160160

161161
/**

lib/Github/Api/CurrentUser/Starring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Starring extends AbstractApi
2626
public function configure($bodyType = null)
2727
{
2828
if ('star' === $bodyType) {
29-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getApiVersion());
29+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->getApiVersion());
3030
}
3131

3232
return $this;

lib/Github/Api/Enterprise.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ class Enterprise extends AbstractApi
2222
*/
2323
public function stats()
2424
{
25-
return new Stats($this->client);
25+
return new Stats($this->getClient(), $this->getPerPage());
2626
}
2727

2828
/**
2929
* @return License
3030
*/
3131
public function license()
3232
{
33-
return new License($this->client);
33+
return new License($this->getClient(), $this->getPerPage());
3434
}
3535

3636
/**
3737
* @return ManagementConsole
3838
*/
3939
public function console()
4040
{
41-
return new ManagementConsole($this->client);
41+
return new ManagementConsole($this->getClient(), $this->getPerPage());
4242
}
4343

4444
/**
4545
* @return UserAdmin
4646
*/
4747
public function userAdmin()
4848
{
49-
return new UserAdmin($this->client);
49+
return new UserAdmin($this->getClient(), $this->getPerPage());
5050
}
5151
}

lib/Github/Api/Gist/Comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function configure($bodyType = null)
2929
$bodyType = 'raw';
3030
}
3131

32-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
32+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->getApiVersion(), $bodyType);
3333

3434
return $this;
3535
}

lib/Github/Api/Gists.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function configure($bodyType = null)
3232
$bodyType = 'raw';
3333
}
3434

35-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType);
35+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->getApiVersion(), $bodyType);
3636

3737
return $this;
3838
}
@@ -177,6 +177,6 @@ public function unstar($id)
177177
*/
178178
public function comments()
179179
{
180-
return new Comments($this->client);
180+
return new Comments($this->getClient(), $this->getPerPage());
181181
}
182182
}

lib/Github/Api/GitData.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,38 @@ class GitData extends AbstractApi
2222
*/
2323
public function blobs()
2424
{
25-
return new Blobs($this->client);
25+
return new Blobs($this->getClient(), $this->getPerPage());
2626
}
2727

2828
/**
2929
* @return Commits
3030
*/
3131
public function commits()
3232
{
33-
return new Commits($this->client);
33+
return new Commits($this->getClient(), $this->getPerPage());
3434
}
3535

3636
/**
3737
* @return References
3838
*/
3939
public function references()
4040
{
41-
return new References($this->client);
41+
return new References($this->getClient(), $this->getPerPage());
4242
}
4343

4444
/**
4545
* @return Tags
4646
*/
4747
public function tags()
4848
{
49-
return new Tags($this->client);
49+
return new Tags($this->getClient(), $this->getPerPage());
5050
}
5151

5252
/**
5353
* @return Trees
5454
*/
5555
public function trees()
5656
{
57-
return new Trees($this->client);
57+
return new Trees($this->getClient(), $this->getPerPage());
5858
}
5959
}

lib/Github/Api/GitData/Blobs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Blobs extends AbstractApi
2626
public function configure($bodyType = null)
2727
{
2828
if ('raw' === $bodyType) {
29-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getApiVersion());
29+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->getApiVersion());
3030
}
3131

3232
return $this;

0 commit comments

Comments
 (0)