44
55use Github \Client ;
66use 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 */
1313abstract 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 }
0 commit comments