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
<?php
namespace Commercetools\Commons\Helper;
use Commercetools\Core\Client;
use Commercetools\Core\Request\QueryAllRequestInterface;
class QueryHelper
{
const DEFAULT_PAGE_SIZE = 500;
public function getAll(Client $client, QueryAllRequestInterface $request)
{
$lastId = null;
$data = ['results' => []];
do {
$request->sort('id')->limit(static::DEFAULT_PAGE_SIZE)->withTotal(false);
if ($lastId != null) {
$request->where('id > "' . $lastId . '"');
}
$response = $client->execute($request);
if ($response->isError() || is_null($response->toObject())) {
break;
}
$results = $response->toArray()['results'];
$data['results'] = array_merge($data['results'], $results);
$lastId = end($results)['id'];
} while (count($results) >= static::DEFAULT_PAGE_SIZE);
$result = $request->map($data, $client->getConfig()->getContext());
return $result;
}
}