Skip to content

Commit 4d192aa

Browse files
committed
Add support for Pagination using cursor
1 parent 44dc830 commit 4d192aa

24 files changed

+363
-116
lines changed

Api/Endpoints/Comments.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Comments
@@ -46,7 +46,7 @@ public function listProject(string $projectId, array $queryParams = []): Lokalis
4646
*/
4747
public function fetchAllProject(string $projectId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"projects/$projectId/comments",
5252
[],
@@ -89,7 +89,7 @@ public function listKey(string $projectId, int $keyId, array $queryParams = []):
8989
*/
9090
public function fetchAllKey(string $projectId, int $keyId): LokaliseApiResponse
9191
{
92-
return $this->requestAll(
92+
return $this->requestAllUsingPaging(
9393
'GET',
9494
"projects/$projectId/keys/$keyId/comments",
9595
[],

Api/Endpoints/Contributors.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Contributors
@@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4646
*/
4747
public function fetchAll(string $projectId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"projects/$projectId/contributors",
5252
[],

Api/Endpoints/CustomTranslationStatuses.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class CustomTranslationStatuses
@@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4747
*/
4848
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
4949
{
50-
return $this->requestAll(
50+
return $this->requestAllUsingPaging(
5151
'GET',
5252
"projects/$projectId/custom-translation-statuses",
5353
$queryParams,

Api/Endpoints/Endpoint.php

+71-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ protected function request(string $requestType, string $uri, array $queryParams
110110
*
111111
* @throws LokaliseApiException
112112
* @throws LokaliseResponseException
113+
* @deprecated Use requestAllUsingCursor or requestAllUsingPaging instead
113114
*/
114115
protected function requestAll(
115116
string $requestType,
@@ -118,12 +119,35 @@ protected function requestAll(
118119
array $body = [],
119120
string $bodyResponseKey = ''
120121
): LokaliseApiResponse {
122+
return $this->requestAllUsingPaging($requestType, $uri, $queryParams, $body, $bodyResponseKey);
123+
}
124+
125+
/**
126+
* @param string $requestType
127+
* @param string $uri
128+
* @param array $queryParams
129+
* @param array $body
130+
* @param string $bodyResponseKey
131+
*
132+
* @return LokaliseApiResponse
133+
*
134+
* @throws LokaliseApiException
135+
* @throws LokaliseResponseException
136+
*/
137+
protected function requestAllUsingPaging(
138+
string $requestType,
139+
string $uri,
140+
array $queryParams = [],
141+
array $body = [],
142+
string $bodyResponseKey = ''
143+
): LokaliseApiResponse
144+
{
121145
$page = 1;
122146
$queryParams = array_merge($queryParams, ['limit' => static::FETCH_ALL_LIMIT, 'page' => $page]);
123147

124148
$bodyData = [];
125149
$result = $this->request($requestType, $uri, $queryParams, $body);
126-
if (is_array($result->body[$bodyResponseKey])) {
150+
if (isset($result->body[$bodyResponseKey]) && is_array($result->body[$bodyResponseKey])) {
127151
$bodyData = $result->body[$bodyResponseKey];
128152
}
129153
while ($result->getPageCount() > $page) {
@@ -134,14 +158,59 @@ protected function requestAll(
134158

135159
$result = $this->request($requestType, $uri, $queryParams, $body);
136160
if (is_array($result->body[$bodyResponseKey])) {
137-
$bodyData = array_merge($result->body[$bodyResponseKey], $bodyData);
161+
$bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]);
138162
$result->body[$bodyResponseKey] = $bodyData;
139163
}
140164
}
141165

142166
return $result;
143167
}
144168

169+
170+
/**
171+
* @param string $requestType
172+
* @param string $uri
173+
* @param array $queryParams
174+
* @param array $body
175+
* @param string $bodyResponseKey
176+
*
177+
* @return LokaliseApiResponse
178+
*
179+
* @throws LokaliseApiException
180+
* @throws LokaliseResponseException
181+
*/
182+
protected function requestAllUsingCursor(
183+
string $requestType,
184+
string $uri,
185+
array $queryParams = [],
186+
array $body = [],
187+
string $bodyResponseKey = ''
188+
): LokaliseApiResponse
189+
{
190+
$bodyData = [];
191+
$cursor = '';
192+
$queryParams['limit'] = static::FETCH_ALL_LIMIT;
193+
$queryParams['pagination'] = 'cursor';
194+
while (true) {
195+
$queryParams['cursor'] = $cursor;
196+
197+
$result = $this->request($requestType, $uri, $queryParams, $body);
198+
199+
if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) {
200+
$bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]);
201+
$result->body[$bodyResponseKey] = $bodyData;
202+
}
203+
204+
if (!$result->hasNextCursor()) {
205+
break;
206+
}
207+
208+
$cursor = $result->getNextCursor();
209+
}
210+
211+
return $result;
212+
}
213+
145214
/**
146215
* @param array $queryParams
147216
*

Api/Endpoints/Files.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Files
@@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4646
*/
4747
public function fetchAll(string $projectId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"projects/$projectId/files",
5252
[],

Api/Endpoints/Keys.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Keys
@@ -48,7 +48,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4848
*/
4949
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
5050
{
51-
return $this->requestAll(
51+
return $this->requestAllUsingCursor(
5252
'GET',
5353
"projects/$projectId/keys",
5454
$queryParams,

Api/Endpoints/Languages.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Languages
@@ -43,7 +43,7 @@ public function listSystem(array $queryParams = []): LokaliseApiResponse
4343
*/
4444
public function fetchAllSystem(): LokaliseApiResponse
4545
{
46-
return $this->requestAll(
46+
return $this->requestAllUsingPaging(
4747
'GET',
4848
"system/languages",
4949
[],
@@ -84,7 +84,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
8484
*/
8585
public function fetchAll(string $projectId): LokaliseApiResponse
8686
{
87-
return $this->requestAll(
87+
return $this->requestAllUsingPaging(
8888
'GET',
8989
"projects/$projectId/languages",
9090
[],

Api/Endpoints/Orders.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Orders
@@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse
4646
*/
4747
public function fetchAll(int $teamId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"teams/{$teamId}/orders",
5252
[],

Api/Endpoints/PaymentCards.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Cards
@@ -43,7 +43,7 @@ public function list(array $queryParams = []): LokaliseApiResponse
4343
*/
4444
public function fetchAll(): LokaliseApiResponse
4545
{
46-
return $this->requestAll(
46+
return $this->requestAllUsingPaging(
4747
'GET',
4848
"payment_cards",
4949
[],

Api/Endpoints/Projects.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Projects
@@ -45,7 +45,7 @@ public function list(array $queryParams = []): LokaliseApiResponse
4545
*/
4646
public function fetchAll(array $queryParams = []): LokaliseApiResponse
4747
{
48-
return $this->requestAll(
48+
return $this->requestAllUsingPaging(
4949
'GET',
5050
"projects",
5151
$queryParams,

Api/Endpoints/Screenshots.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Screenshots
@@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4646
*/
4747
public function fetchAll(string $projectId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"projects/$projectId/screenshots",
5252
[],

Api/Endpoints/Snapshots.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Snapshots
@@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4646
*/
4747
public function fetchAll(string $projectId): LokaliseApiResponse
4848
{
49-
return $this->requestAll(
49+
return $this->requestAllUsingPaging(
5050
'GET',
5151
"projects/$projectId/snapshots",
5252
[],

Api/Endpoints/Tasks.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lokalise\Endpoints;
44

5-
use \Lokalise\LokaliseApiResponse;
6-
use \Lokalise\Exceptions\LokaliseApiException;
7-
use \Lokalise\Exceptions\LokaliseResponseException;
5+
use Lokalise\Exceptions\LokaliseApiException;
6+
use Lokalise\Exceptions\LokaliseResponseException;
7+
use Lokalise\LokaliseApiResponse;
88

99
/**
1010
* Class Tasks
@@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
4747
*/
4848
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
4949
{
50-
return $this->requestAll(
50+
return $this->requestAllUsingPaging(
5151
'GET',
5252
"projects/$projectId/tasks",
5353
$queryParams,

0 commit comments

Comments
 (0)