-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.php
More file actions
140 lines (117 loc) · 4.18 KB
/
Page.php
File metadata and controls
140 lines (117 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace Rapttor\WhopSdk\Http;
use Rapttor\WhopSdk\Resources\AbstractResource;
/**
* A single page of cursor-paginated API results.
*
* Implements {@see \IteratorAggregate} so you can iterate through **all** items
* across every page transparently:
*
* ```php
* foreach ($client->payments->list(['company_id' => 'biz_xxx']) as $payment) {
* // Pages are fetched on demand as the iterator progresses
* }
* ```
*
* @template TItem of array<string, mixed>
* @implements \IteratorAggregate<int, TItem>
*/
final class Page implements \IteratorAggregate, \Countable
{
/**
* @param list<TItem> $data
* @param array<string, mixed> $requestParams
*/
public function __construct(
private readonly array $data,
private readonly ?string $nextCursor,
private readonly AbstractResource $resource,
private readonly array $requestParams,
) {}
// ── Data access ────────────────────────────────────────────────────────────
/**
* Items on the current page.
*
* @return list<TItem>
*/
public function getData(): array
{
return $this->data;
}
// ── Pagination ─────────────────────────────────────────────────────────────
public function hasNextPage(): bool
{
return $this->nextCursor !== null;
}
/**
* Cursor info for the next page, or null if this is the last page.
*
* @return array{after: string}|null
*/
public function nextPageInfo(): ?array
{
if ($this->nextCursor === null) {
return null;
}
return ['after' => $this->nextCursor];
}
/**
* Fetch and return the next page, or null if there are no more pages.
*
* @return static<TItem>|null
*/
public function getNextPage(): ?static
{
if ($this->nextCursor === null) {
return null;
}
/** @var static<TItem> */
return $this->resource->list(
array_merge($this->requestParams, ['after' => $this->nextCursor])
);
}
// ── IteratorAggregate ──────────────────────────────────────────────────────
/**
* Iterate all items across all pages, fetching new pages as needed.
*
* @return \Traversable<int, TItem>
*/
public function getIterator(): \Traversable
{
yield from $this->data;
$page = $this;
while ($page->hasNextPage()) {
$page = $page->getNextPage();
if ($page === null) {
break;
}
yield from $page->getData();
}
}
// ── Countable ──────────────────────────────────────────────────────────────
/** Returns the number of items on **this** page only. */
public function count(): int
{
return count($this->data);
}
// ── Factory ────────────────────────────────────────────────────────────────
/**
* Construct a Page from a raw API response array.
*
* @param array<string, mixed> $response
* @param array<string, mixed> $requestParams
* @return static<array<string, mixed>>
*/
public static function fromResponse(
array $response,
AbstractResource $resource,
array $requestParams,
): static {
/** @var list<array<string, mixed>> $data */
$data = $response['data'] ?? [];
$nextCursor = $response['page_info']['end_cursor'] ?? null;
/** @var static<array<string, mixed>> */
return new static($data, $nextCursor, $resource, $requestParams);
}
}