mirror of
https://github.com/salesagility/SuiteCRM.git
synced 2025-02-11 08:48:55 +00:00
![Stephen Ostrow](/assets/img/avatar_default.png)
fixes #7095 The new API states the base_uri should `https://crm.example.org/Api/`. This means all links returned from the API need to be relative to this `base_uri`. According to [Guzzle explanation of RFC 3986](http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request) the `base_uri` with a path should end with `/` and the relative link should start without a `/`. Full Example usage of this would ```php <?php $client = new \GuzzleHttp\Client([ 'base_uri' => 'https://crm.example.org/Api/', ]); $url = 'V8/module/Notes?page[size]=20'; do { $response = $client->request('GET', $url); $body = json_decode((string)$response->getBody()); $data = $body->data; // Do something with data $url = property_exists($body, 'links') && property_exists($body->links, 'next') && !is_null($body->links->next) ? $body->links->next : null; } while ($url); ```
60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
namespace Api\V8\JsonApi\Helper;
|
|
|
|
use Api\V8\JsonApi\Response\MetaResponse;
|
|
use Api\V8\JsonApi\Response\PaginationResponse;
|
|
use Slim\Http\Request;
|
|
|
|
class PaginationObjectHelper
|
|
{
|
|
/**
|
|
* @param integer $totalPages
|
|
* @param integer $numOfRecords
|
|
*
|
|
* @return MetaResponse
|
|
*/
|
|
public function getPaginationMeta($totalPages, $numOfRecords)
|
|
{
|
|
return new MetaResponse(
|
|
['total-pages' => $totalPages, 'records-on-this-page' => $numOfRecords]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param integer $totalPages
|
|
* @param integer $number
|
|
*
|
|
* @return PaginationResponse
|
|
*/
|
|
public function getPaginationLinks(Request $request, $totalPages, $number)
|
|
{
|
|
$pagination = new PaginationResponse();
|
|
|
|
if ($number > 1) {
|
|
$pagination->setFirst($this->createPaginationLink($request, 1));
|
|
$pagination->setPrev($this->createPaginationLink($request, $number - 1));
|
|
}
|
|
|
|
if ($number + 1 <= $totalPages) {
|
|
$pagination->setNext($this->createPaginationLink($request, $number + 1));
|
|
$pagination->setLast($this->createPaginationLink($request, $totalPages));
|
|
}
|
|
|
|
return $pagination;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param integer $number
|
|
*
|
|
* @return string
|
|
*/
|
|
private function createPaginationLink(Request $request, $number)
|
|
{
|
|
$queryParams = $request->getQueryParams();
|
|
$queryParams['page']['number'] = $number;
|
|
|
|
return sprintf('%s?%s', $request->getUri()->getPath(), urldecode(http_build_query($queryParams)));
|
|
}
|
|
}
|