Skip to content

Adds support for cursor based pagination #125

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions lib/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ protected static function processRequest($ch)
// close curl resource to free up system resources
curl_close($ch);

return $response->getBody();
//return $response->getBody();
return $response;
}

}
5 changes: 3 additions & 2 deletions lib/HttpRequestJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ public static function delete($url, $httpHeaders = array())
*
* @param string $response
*
* @return array
* @return string
*/
protected static function processResponse($response)
{

return json_decode($response, true);
//return json_decode($response, true);
return $response;
}

}
117 changes: 115 additions & 2 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ abstract class ShopifyResource
*/
protected $httpHeaders = array();

/**
* HTTP response headers
*
* @var array
*/
protected static $httpResponseHeaders = array();

/**
* The base URL of the API Resource (excluding the '.json' extension).
*
Expand Down Expand Up @@ -467,16 +474,17 @@ protected function castString($array)
/**
* Process the request response
*
* @param array $responseArray Request response in array format
* @param string $response Request response in array format
* @param string $dataKey Keyname to fetch data from response array
*
* @throws ApiException if the response has an error specified
* @throws CurlException if response received with unexpected HTTP code.
*
* @return array
*/
public function processResponse($responseArray, $dataKey = null)
public function processResponse($response, $dataKey = null)
{
$responseArray = json_decode($response->getBody(), true);
if ($responseArray === null) {
//Something went wrong, Checking HTTP Codes
$httpOK = 200; //Request Successful, OK.
Expand All @@ -496,10 +504,115 @@ public function processResponse($responseArray, $dataKey = null)
throw new ApiException($message);
}

self::$httpResponseHeaders = $response->getHeaders();

if ($dataKey && isset($responseArray[$dataKey])) {
return $responseArray[$dataKey];
} else {
return $responseArray;
}
}

/**
* Checks response headers for existence of next page info
*
* @return boolean
*/
static public function lastResourceContainsNextPageInfo()
{
$headers = self::$httpResponseHeaders;

if (isset($headers["Link"])) {
$matchData = array();

if (preg_match("/<([^>]*)>; rel=\"next\"/", $headers["Link"], $matchData)) {
// found rel="next"
return true;
}
}

return false;
}

/**
* Checks response headers for existence of previous page info
*
* @return boolean
*/

static public function lastResourceContainsPrevPageInfo()
{
$headers = self::$httpResponseHeaders;

if (isset($headers["Link"])) {
$matchData = array();

if (preg_match("/<([^>]*)>; rel=\"previous\"/", $headers["Link"], $matchData)) {
// found rel="prev"
return true;
}
}

return false;
}

/**
* Gets next page info string for use in pagination
*
* @return string
*/
static public function getNextPageInfo()
{
$headers = self::$httpResponseHeaders;

if (isset($headers["Link"])) {
$matchData = array();

if (preg_match("/<([^>]*)>; rel=\"next\"/", $headers["Link"], $matchData)) {
// found rel="next"
$query = parse_url($matchData[1], PHP_URL_QUERY);

$pairs = explode( "&", $query );
foreach( $pairs as $p ) {
list( $key, $value) = explode( "=", $p );

if( $key == "page_info" ) {
return $value;
}
}
}
}

return false;
}

/**
* Gets previous page info string for use in pagination
*
* @return string
*/
static public function getPrevPageInfo()
{
$headers = self::$httpResponseHeaders;

if (isset($headers["Link"])) {
$matchData = array();

if (preg_match("/<([^>]*)>; rel=\"previous\"/", $headers["Link"], $matchData)) {
// found rel="prev"
$query = parse_url($matchData[1], PHP_URL_QUERY);

$pairs = explode( "&", $query );
foreach( $pairs as $p ) {
list( $key, $value) = explode( "=", $p );

if( $key == "page_info" ) {
return $value;
}
}
}
}

return false;
}
}