-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPaginationClient.php
89 lines (69 loc) · 2.48 KB
/
PaginationClient.php
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
<?php
declare(strict_types=1);
namespace Sysix\LexOffice;
use Psr\Http\Message\ResponseInterface;
abstract class PaginationClient extends BaseClient
{
public int $size = 100;
public string $sortColumn;
public string $sortDirection = 'DESC';
protected function generatePageUrl(int $page): string
{
return $this->resource . '?' . $this->buildQueryParams([
'page' => $page
]);
}
/**
* @param array<string, bool|int|string|null> $params
*/
protected function buildQueryParams(array $params): string
{
$params['size'] = $this->size;
// contact endpoint can't be sorted but is a Pagination client
if (isset($this->sortColumn)) {
$params['sort'] = $this->sortColumn . ',' . $this->sortDirection;
}
return http_build_query($params);
}
public function getPage(int $page): ResponseInterface
{
return $this->api
->newRequest('GET', $this->generatePageUrl($page))
->getResponse();
}
/**
* @deprecated 1.0 Not recommend anymore because of Rate Limiting, WILL be removed in 2.0
*/
public function getAll(): ResponseInterface
{
trigger_error(self::class . '::' . __METHOD__ . ' should not be called anymore, in future versions this method WILL not exist', E_USER_DEPRECATED);
$response = $this->getPage(0);
$result = Utils::getJsonFromResponse($response);
if (
$result === null || !is_object($result) ||
!property_exists($result, 'totalPages') || $result->totalPages == 1 ||
!property_exists($result, 'content')
) {
return $response;
}
// update content to get all contacts
for ($i = 1; $i < $result->totalPages; $i++) {
$responsePage = $this->getPage($i);
if ($responsePage->getStatusCode() !== 200) {
return $responsePage;
}
$resultPage = Utils::getJsonFromResponse($responsePage);
if (
$resultPage === null ||
!is_object($resultPage) ||
!property_exists($resultPage, 'content') ||
!is_array($resultPage->content) ||
!is_array($result->content)
) {
return $responsePage;
}
array_push($result->content, ...$resultPage->content);
}
return $response->withBody(Utils::createStream($result));
}
}