88use Salient \Contract \Curler \CurlerPagerInterface ;
99use Salient \Contract \Http \HttpResponseInterface ;
1010use Salient \Curler \CurlerPage ;
11+ use Salient \Utility \Test ;
1112use Closure ;
13+ use LogicException ;
1214
1315/**
1416 * Increments a value in the query string of each request until no results are
@@ -22,11 +24,8 @@ final class QueryPager implements CurlerPagerInterface
2224
2325 /** @var array-key|null */
2426 private $ PageKey ;
27+ /** @var int<1,max>|null */
2528 private ?int $ PageSize ;
26- /** @var mixed[] */
27- private array $ CurrentQuery ;
28- /** @var array-key|null */
29- private $ CurrentPageKey ;
3029
3130 /**
3231 * Creates a new QueryPager object
@@ -40,10 +39,9 @@ final class QueryPager implements CurlerPagerInterface
4039 * - `Arr::get($data, $entitySelector)` if `$entitySelector` is a string or
4140 * integer, or
4241 * - `$data` if `$entitySelector` is `null`
43- * @param int|null $pageSize Another page is requested if:
42+ * @param int<1,max> |null $pageSize Another page is requested if:
4443 * - `$pageSize` is `null` and at least one result is returned, or
45- * - `$pageSize` is greater than `0` and exactly `$pageSize` results are
46- * returned
44+ * - exactly `$pageSize` results are returned
4745 */
4846 public function __construct (
4947 $ pageKey = null ,
@@ -63,18 +61,6 @@ public function getFirstRequest(
6361 CurlerInterface $ curler ,
6462 ?array $ query = null
6563 ): RequestInterface {
66- $ this ->CurrentQuery = $ query ?? [];
67- $ this ->CurrentPageKey = null ;
68-
69- // If `$this->PageKey` does not appear in the query, add it to
70- // `$this->CurrentQuery` without changing the first request
71- if (
72- $ this ->PageKey !== null
73- && !array_key_exists ($ this ->PageKey , $ this ->CurrentQuery )
74- ) {
75- $ this ->CurrentQuery [$ this ->PageKey ] = 1 ;
76- }
77-
7864 return $ request ;
7965 }
8066
@@ -93,26 +79,20 @@ public function getPage(
9379
9480 if ($ data && (
9581 $ this ->PageSize === null
96- || $ this ->PageSize < 1
9782 || count ($ data ) === $ this ->PageSize
9883 )) {
99- if (
100- $ this ->PageKey === null
101- && !$ previousPage
102- && $ this ->CurrentQuery
103- && is_int (reset ($ this ->CurrentQuery ))
104- ) {
105- $ this ->CurrentPageKey = key ($ this ->CurrentQuery );
84+ $ key = $ this ->PageKey ;
85+ if ($ key === null && $ query && Test::isInteger (reset ($ query ))) {
86+ $ key = key ($ query );
10687 }
107- $ key = $ this ->PageKey ?? $ this ->CurrentPageKey ;
108- if ($ key !== null ) {
109- $ this ->CurrentQuery [$ key ]++;
110- $ uri = $ request ->getUri ();
111- $ uri = $ curler ->replaceQuery ($ uri , $ this ->CurrentQuery );
112- $ nextRequest = $ request ->withUri ($ uri );
88+ if ($ key === null ) {
89+ throw new LogicException ('No page key and no integer value at query offset 0 ' );
11390 }
91+ $ query [$ key ] ??= 1 ;
92+ $ query [$ key ]++;
93+ $ nextRequest = $ curler ->replaceQuery ($ request , $ query );
11494 }
11595
116- return new CurlerPage ($ data , $ nextRequest ?? null );
96+ return new CurlerPage ($ data , $ nextRequest ?? null , $ query );
11797 }
11898}
0 commit comments