Skip to content

Commit 487a056

Browse files
committed
Backwards-compatible cursor-based pagination
1 parent f0963bf commit 487a056

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

lib/CurlRequest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class CurlRequest
2828
*/
2929
public static $lastHttpCode;
3030

31+
/**
32+
* HTTP response headers of last executed request
33+
*
34+
* @var array
35+
*/
36+
public static $lastHttpResponseHeaders = array();
3137

3238
/**
3339
* Initialize the curl resource
@@ -170,7 +176,9 @@ protected static function processRequest($ch)
170176
// close curl resource to free up system resources
171177
curl_close($ch);
172178

179+
self::$lastHttpResponseHeaders = $response->getHeaders();
180+
173181
return $response->getBody();
174182
}
175-
183+
176184
}

lib/ShopifyResource.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ abstract class ShopifyResource
3030
*/
3131
protected $httpHeaders = array();
3232

33+
/**
34+
* HTTP response headers of last executed request
35+
*
36+
* @var array
37+
*/
38+
public static $lastHttpResponseHeaders = array();
39+
3340
/**
3441
* The base URL of the API Resource (excluding the '.json' extension).
3542
*
@@ -477,6 +484,8 @@ protected function castString($array)
477484
*/
478485
public function processResponse($responseArray, $dataKey = null)
479486
{
487+
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
488+
480489
if ($responseArray === null) {
481490
//Something went wrong, Checking HTTP Codes
482491
$httpOK = 200; //Request Successful, OK.
@@ -502,4 +511,107 @@ public function processResponse($responseArray, $dataKey = null)
502511
return $responseArray;
503512
}
504513
}
514+
515+
/**
516+
* Checks response headers for existence of next page info
517+
*
518+
* @return boolean
519+
*/
520+
static public function lastResourceContainsNextPageInfo()
521+
{
522+
$headers = self::$lastHttpResponseHeaders;
523+
524+
if (isset($headers["Link"])) {
525+
$matchData = array();
526+
527+
if (preg_match("/<([^>]*)>; rel=\"next\"/", $headers["Link"], $matchData)) {
528+
// found rel="next"
529+
return true;
530+
}
531+
}
532+
533+
return false;
534+
}
535+
536+
/**
537+
* Checks response headers for existence of previous page info
538+
*
539+
* @return boolean
540+
*/
541+
542+
static public function lastResourceContainsPrevPageInfo()
543+
{
544+
$headers = self::$lastHttpResponseHeaders;
545+
546+
if (isset($headers["Link"])) {
547+
$matchData = array();
548+
549+
if (preg_match("/<([^>]*)>; rel=\"previous\"/", $headers["Link"], $matchData)) {
550+
// found rel="prev"
551+
return true;
552+
}
553+
}
554+
555+
return false;
556+
}
557+
558+
/**
559+
* Gets next page info string for use in pagination
560+
*
561+
* @return string
562+
*/
563+
static public function getNextPageInfo()
564+
{
565+
$headers = self::$lastHttpResponseHeaders;
566+
567+
if (isset($headers["Link"])) {
568+
$matchData = array();
569+
570+
if (preg_match("/<([^>]*)>; rel=\"next\"/", $headers["Link"], $matchData)) {
571+
// found rel="next"
572+
$query = parse_url($matchData[1], PHP_URL_QUERY);
573+
574+
$pairs = explode( "&", $query );
575+
foreach( $pairs as $p ) {
576+
list( $key, $value) = explode( "=", $p );
577+
578+
if( $key == "page_info" ) {
579+
return $value;
580+
}
581+
}
582+
}
583+
}
584+
585+
return false;
586+
}
587+
588+
/**
589+
* Gets previous page info string for use in pagination
590+
*
591+
* @return string
592+
*/
593+
static public function getPrevPageInfo()
594+
{
595+
$headers = self::$lastHttpResponseHeaders;
596+
597+
if (isset($headers["Link"])) {
598+
$matchData = array();
599+
600+
if (preg_match("/<([^>]*)>; rel=\"previous\"/", $headers["Link"], $matchData)) {
601+
// found rel="prev"
602+
$query = parse_url($matchData[1], PHP_URL_QUERY);
603+
604+
$pairs = explode( "&", $query );
605+
foreach( $pairs as $p ) {
606+
list( $key, $value) = explode( "=", $p );
607+
608+
if( $key == "page_info" ) {
609+
return $value;
610+
}
611+
}
612+
}
613+
}
614+
615+
return false;
616+
}
505617
}

0 commit comments

Comments
 (0)