Skip to content

add support for handling cursor pagination in API 2019-07+ #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPShopify\Exception\ApiException;
use PHPShopify\Exception\SdkException;
use PHPShopify\Exception\CurlException;
use Psr\Http\Message\ResponseInterface;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -113,6 +114,21 @@ abstract class ShopifyResource
*
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
*/

/**
* Response Header Link, used for pagination
* @see: https://help.shopify.com/en/api/guides/paginated-rest-results?utm_source=exacttarget&utm_medium=email&utm_campaign=api_deprecation_notice_1908
* @var string $nextLink
*/
private $nextLink = null;

/**
* Response Header Link, used for pagination
* @see: https://help.shopify.com/en/api/guides/paginated-rest-results?utm_source=exacttarget&utm_medium=email&utm_campaign=api_deprecation_notice_1908
* @var string $prevLink
*/
private $prevLink = null;

public function __construct($id = null, $parentResourceUrl = '')
{
$this->id = $id;
Expand Down Expand Up @@ -490,6 +506,9 @@ public function processResponse($responseArray, $dataKey = null)
}
}

$lastResponseHeaders = CurlRequest::$lastResponseHeaders;
$this->getLinks($lastResponseHeaders);

if (isset($responseArray['errors'])) {
$message = $this->castString($responseArray['errors']);

Expand All @@ -502,4 +521,56 @@ public function processResponse($responseArray, $dataKey = null)
return $responseArray;
}
}

public function getLinks($responseHeaders){
$this->nextLink = $this->getLink($responseHeaders,'next');
$this->prevLink = $this->getLink($responseHeaders,'prev');
}

public function getLink($responseHeaders, $type='next'){
$responseHeaders = json_decode($responseHeaders);

if(property_exists($responseHeaders,'x-shopify-api-version')
&& $responseHeaders->{'x-shopify-api-version'} < '2019-07'){
return null;
}

if(!empty($responseHeaders->link)) {
if (stristr($responseHeaders->link[0], '; rel="'.$type.'"') > -1) {
$headerLinks = explode(',', $responseHeaders->link[0]);
foreach ($headerLinks as $headerLink) {
if (stristr($headerLink, '; rel="'.$type.'"') === -1) {
continue;
}

$pattern = '#<(.*?)>; rel="'.$type.'"#m';
preg_match($pattern, $headerLink, $linkResponseHeaders);
if ($linkResponseHeaders) {
return $linkResponseHeaders[1];
}
}
}
}
return null;
}

public function getPrevLink(){
return $this->prevLink;
}

public function getNextLink(){
return $this->nextLink;
}

public function getNextPageParams(){
$nextPageParams = [];
parse_str($this->getNextLink(), $nextPageParams);
return $nextPageParams;
}

public function getPrevPageParams(){
$nextPageParams = [];
parse_str($this->getPrevLink(), $nextPageParams);
return $nextPageParams;
}
}