18
18
*/
19
19
abstract class PaginatedList implements Iterator, ArrayAccess, Countable
20
20
{
21
+ /**
22
+ * The limit for how many results are allowed to be requested.
23
+ * @type int
24
+ */
25
+ public const LIMIT = 10_000 ;
26
+
21
27
protected int $ iterator = 0 ;
22
28
23
29
protected function __construct (
@@ -30,6 +36,15 @@ protected function __construct(
30
36
{
31
37
}
32
38
39
+ /**
40
+ * Get the pagination part of the response
41
+ * @return Pagination
42
+ */
43
+ public function getPagination (): Pagination
44
+ {
45
+ return $ this ->pagination ;
46
+ }
47
+
33
48
/**
34
49
* @return T[]
35
50
*/
@@ -41,18 +56,19 @@ public function getResults(): array
41
56
/**
42
57
* Get a new page of results starting at the given offset
43
58
* @param int $offset
59
+ * @param int $pageSize
44
60
* @return $this
45
61
* @throws ApiException
46
62
*/
47
- public abstract function getOffset (int $ offset ): static ;
63
+ public abstract function getOffset (int $ offset, int $ pageSize ): static ;
48
64
49
65
/**
50
66
* returns true if there is a next page with results on it
51
67
* @return bool
52
68
*/
53
69
public function hasNextPage (): bool
54
70
{
55
- return $ this ->pagination ->getTotalCount () > $ this ->getNextOffset ();
71
+ return min ( $ this ->pagination ->getTotalCount (), static :: LIMIT ) > $ this ->getNextOffset ();
56
72
}
57
73
58
74
/**
@@ -67,16 +83,8 @@ public function getNextPage(): ?static
67
83
return null ;
68
84
}
69
85
70
- return $ this ->getOffset ($ this ->getNextOffset ());
71
- }
72
-
73
- /**
74
- * get the offset of the next page
75
- * @return int
76
- */
77
- protected function getNextOffset (): int
78
- {
79
- return $ this ->pagination ->getIndex () + $ this ->pagination ->getPageSize ();
86
+ $ offset = $ this ->getNextOffset ();
87
+ return $ this ->getOffset ($ offset , $ this ->getNextPageSize ($ offset ));
80
88
}
81
89
82
90
/**
@@ -110,7 +118,7 @@ public function getPreviousPage(): ?static
110
118
return null ;
111
119
}
112
120
113
- return $ this ->getOffset ($ this ->getPreviousOffset ());
121
+ return $ this ->getOffset ($ this ->getPreviousOffset (), $ this -> pagination -> getPageSize () );
114
122
}
115
123
116
124
/**
@@ -220,4 +228,22 @@ public function count(): int
220
228
{
221
229
return count ($ this ->results );
222
230
}
231
+
232
+ /**
233
+ * @param int $offset
234
+ * @return int
235
+ */
236
+ protected function getNextPageSize (int $ offset ): int
237
+ {
238
+ return min ($ this ->pagination ->getPageSize (), static ::LIMIT - $ offset );
239
+ }
240
+
241
+ /**
242
+ * get the offset of the next page
243
+ * @return int
244
+ */
245
+ protected function getNextOffset (): int
246
+ {
247
+ return $ this ->pagination ->getIndex () + $ this ->pagination ->getPageSize ();
248
+ }
223
249
}
0 commit comments