Skip to content

Commit 0c2fe3b

Browse files
committed
Next support
1 parent f058d8e commit 0c2fe3b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ $client->refreshToken();
2828
// $client is now ready to use
2929
$listEndPoint = new \PHPFUI\ConstantContact\V3\ContactLists($client);
3030
$lists = $listEndPoint->get();
31-
print_r($lists);
31+
do {
32+
print_r($lists);
33+
$lists = $listEndPoint->next();
34+
} while ($lists);
3235
```
3336

3437
## Constant Contact Setup

src/ConstantContact/Base.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ public function getResponseText() : string
2323
return $this->client->getBody();
2424
}
2525

26-
protected function doDelete(array $parameters) : bool
26+
/**
27+
* If the endpoint is paginated, you can call next() to retrieve the next set of data. If no next is provided, an empty array is returned.
28+
*
29+
* @return array filled with next part of the response from the endpoint, or empty if no next.
30+
*/
31+
public function next() : array
32+
{
33+
return $this->client->next();
34+
}
35+
36+
protected function doDelete(array $parameters) : array
2737
{
2838
$url = $this->getUrl($parameters);
2939

src/ConstantContact/Client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Client
2222

2323
private array $validScopes = ['account_read', 'account_update', 'contact_data', 'campaign_data', ];
2424

25+
private string $next = '';
26+
2527
/**
2628
* Construct a client.
2729
*
@@ -40,6 +42,19 @@ public function getBody() : string
4042
return $this->body;
4143
}
4244

45+
public function next() : array
46+
{
47+
if (! $this->next)
48+
{
49+
return [];
50+
}
51+
52+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
53+
$response = $guzzle->request('GET', 'https://api.cc.email' . $this->next);
54+
55+
return $this->process($response);
56+
}
57+
4358
public function setHost(string $host) : self
4459
{
4560
$this->host = $host;
@@ -256,6 +271,7 @@ private function process(\GuzzleHttp\Psr7\Response $response) : array
256271
$this->statusCode = $response->getStatusCode();
257272
$this->body = $response->getBody();
258273
$data = \json_decode($this->body, true);
274+
$this->next = $data['_links']['next']['href'] ?? '';
259275

260276
if (null !== $data)
261277
{

0 commit comments

Comments
 (0)