1212use PHPShopify \Exception \ApiException ;
1313use PHPShopify \Exception \SdkException ;
1414use PHPShopify \Exception \CurlException ;
15+ use Psr \Http \Message \ResponseInterface ;
1516
1617/*
1718|--------------------------------------------------------------------------
@@ -120,6 +121,21 @@ abstract class ShopifyResource
120121 *
121122 * @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
122123 */
124+
125+ /**
126+ * Response Header Link, used for pagination
127+ * @see: https://help.shopify.com/en/api/guides/paginated-rest-results?utm_source=exacttarget&utm_medium=email&utm_campaign=api_deprecation_notice_1908
128+ * @var string $nextLink
129+ */
130+ private $ nextLink = null ;
131+
132+ /**
133+ * Response Header Link, used for pagination
134+ * @see: https://help.shopify.com/en/api/guides/paginated-rest-results?utm_source=exacttarget&utm_medium=email&utm_campaign=api_deprecation_notice_1908
135+ * @var string $prevLink
136+ */
137+ private $ prevLink = null ;
138+
123139 public function __construct ($ id = null , $ parentResourceUrl = '' )
124140 {
125141 $ this ->id = $ id ;
@@ -309,6 +325,9 @@ public function generateUrl($urlParams = array(), $customAction = null)
309325 *
310326 * @uses HttpRequestJson::get() to send the HTTP request
311327 *
328+ * @throws ApiException if the response has an error specified
329+ * @throws CurlException if response received with unexpected HTTP code.
330+ *
312331 * @return array
313332 */
314333 public function get ($ urlParams = array (), $ url = null , $ dataKey = null )
@@ -328,6 +347,10 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
328347 *
329348 * @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
330349 *
350+ * @throws SdkException
351+ * @throws ApiException if the response has an error specified
352+ * @throws CurlException if response received with unexpected HTTP code.
353+ *
331354 * @return integer
332355 */
333356 public function count ($ urlParams = array ())
@@ -347,6 +370,8 @@ public function count($urlParams = array())
347370 * @param mixed $query
348371 *
349372 * @throws SdkException if search is not enabled for the resouce
373+ * @throws ApiException if the response has an error specified
374+ * @throws CurlException if response received with unexpected HTTP code.
350375 *
351376 * @return array
352377 */
@@ -372,6 +397,9 @@ public function search($query)
372397 *
373398 * @uses HttpRequestJson::post() to send the HTTP request
374399 *
400+ * @throws ApiException if the response has an error specified
401+ * @throws CurlException if response received with unexpected HTTP code.
402+ *
375403 * @return array
376404 */
377405 public function post ($ dataArray , $ url = null , $ wrapData = true )
@@ -394,6 +422,9 @@ public function post($dataArray, $url = null, $wrapData = true)
394422 *
395423 * @uses HttpRequestJson::put() to send the HTTP request
396424 *
425+ * @throws ApiException if the response has an error specified
426+ * @throws CurlException if response received with unexpected HTTP code.
427+ *
397428 * @return array
398429 */
399430 public function put ($ dataArray , $ url = null , $ wrapData = true )
@@ -416,6 +447,9 @@ public function put($dataArray, $url = null, $wrapData = true)
416447 *
417448 * @uses HttpRequestJson::delete() to send the HTTP request
418449 *
450+ * @throws ApiException if the response has an error specified
451+ * @throws CurlException if response received with unexpected HTTP code.
452+ *
419453 * @return array an empty array will be returned if the request is successfully completed
420454 */
421455 public function delete ($ urlParams = array (), $ url = null )
@@ -499,10 +533,13 @@ public function processResponse($responseArray, $dataKey = null)
499533 }
500534 }
501535
536+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
537+ $ this ->getLinks ($ lastResponseHeaders );
538+
502539 if (isset ($ responseArray ['errors ' ])) {
503540 $ message = $ this ->castString ($ responseArray ['errors ' ]);
504541
505- throw new ApiException ($ message );
542+ throw new ApiException ($ message, CurlRequest:: $ lastHttpCode );
506543 }
507544
508545 if ($ dataKey && isset ($ responseArray [$ dataKey ])) {
@@ -512,106 +549,56 @@ public function processResponse($responseArray, $dataKey = null)
512549 }
513550 }
514551
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 ;
552+ public function getLinks ($ responseHeaders ){
553+ $ this ->nextLink = $ this ->getLink ($ responseHeaders ,'next ' );
554+ $ this ->prevLink = $ this ->getLink ($ responseHeaders ,'prev ' );
534555 }
535556
536- /**
537- * Checks response headers for existence of previous page info
538- *
539- * @return boolean
540- */
557+ public function getLink ($ responseHeaders , $ type ='next ' ){
558+ $ responseHeaders = json_decode ($ responseHeaders );
541559
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- }
560+ if (property_exists ($ responseHeaders ,'x-shopify-api-version ' )
561+ && $ responseHeaders ->{'x-shopify-api-version ' } < '2019-07 ' ){
562+ return null ;
553563 }
554564
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 );
565+ if (!empty ($ responseHeaders ->link )) {
566+ if (stristr ($ responseHeaders ->link [0 ], '; rel=" ' .$ type .'" ' ) > -1 ) {
567+ $ headerLinks = explode (', ' , $ responseHeaders ->link [0 ]);
568+ foreach ($ headerLinks as $ headerLink ) {
569+ if (stristr ($ headerLink , '; rel=" ' .$ type .'" ' ) === -1 ) {
570+ continue ;
571+ }
577572
578- if ( $ key == "page_info " ) {
579- return $ value ;
573+ $ pattern = '#<(.*?)>; rel=" ' .$ type .'"#m ' ;
574+ preg_match ($ pattern , $ headerLink , $ linkResponseHeaders );
575+ if ($ linkResponseHeaders ) {
576+ return $ linkResponseHeaders [1 ];
580577 }
581578 }
582579 }
583580 }
584581
585- return false ;
582+ return null ;
586583 }
587584
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 );
585+ public function getPrevLink (){
586+ return $ this ->prevLink ;
587+ }
603588
604- $ pairs = explode ( " & " , $ query );
605- foreach ( $ pairs as $ p ) {
606- list ( $ key , $ value ) = explode ( " = " , $ p );
589+ public function getNextLink (){
590+ return $ this -> nextLink ;
591+ }
607592
608- if ( $ key == "page_info " ) {
609- return $ value ;
610- }
611- }
612- }
613- }
593+ public function getNextPageParams (){
594+ $ nextPageParams = [];
595+ parse_str ($ this ->getNextLink (), $ nextPageParams );
596+ return $ nextPageParams ;
597+ }
614598
615- return false ;
599+ public function getPrevPageParams (){
600+ $ nextPageParams = [];
601+ parse_str ($ this ->getPrevLink (), $ nextPageParams );
602+ return $ nextPageParams ;
616603 }
617604}
0 commit comments