1212use PHPShopify \Exception \ApiException ;
1313use PHPShopify \Exception \SdkException ;
1414use PHPShopify \Exception \CurlException ;
15+ use Psr \Http \Message \ResponseInterface ;
1516
1617/*
1718|--------------------------------------------------------------------------
@@ -113,6 +114,21 @@ abstract class ShopifyResource
113114 *
114115 * @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115116 */
117+
118+ /**
119+ * Response Header Link, used for pagination
120+ * @see: https://help.shopify.com/en/api/guides/paginated-rest-results?utm_source=exacttarget&utm_medium=email&utm_campaign=api_deprecation_notice_1908
121+ * @var string $nextLink
122+ */
123+ private $ nextLink = null ;
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 $prevLink
129+ */
130+ private $ prevLink = null ;
131+
116132 public function __construct ($ id = null , $ parentResourceUrl = '' )
117133 {
118134 $ this ->id = $ id ;
@@ -508,6 +524,9 @@ public function processResponse($responseArray, $dataKey = null)
508524 }
509525 }
510526
527+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
528+ $ this ->getLinks ($ lastResponseHeaders );
529+
511530 if (isset ($ responseArray ['errors ' ])) {
512531 $ message = $ this ->castString ($ responseArray ['errors ' ]);
513532
@@ -520,4 +539,56 @@ public function processResponse($responseArray, $dataKey = null)
520539 return $ responseArray ;
521540 }
522541 }
542+
543+ public function getLinks ($ responseHeaders ){
544+ $ this ->nextLink = $ this ->getLink ($ responseHeaders ,'next ' );
545+ $ this ->prevLink = $ this ->getLink ($ responseHeaders ,'prev ' );
546+ }
547+
548+ public function getLink ($ responseHeaders , $ type ='next ' ){
549+ $ responseHeaders = json_decode ($ responseHeaders );
550+
551+ if (property_exists ($ responseHeaders ,'x-shopify-api-version ' )
552+ && $ responseHeaders ->{'x-shopify-api-version ' } < '2019-07 ' ){
553+ return null ;
554+ }
555+
556+ if (!empty ($ responseHeaders ->link )) {
557+ if (stristr ($ responseHeaders ->link [0 ], '; rel=" ' .$ type .'" ' ) > -1 ) {
558+ $ headerLinks = explode (', ' , $ responseHeaders ->link [0 ]);
559+ foreach ($ headerLinks as $ headerLink ) {
560+ if (stristr ($ headerLink , '; rel=" ' .$ type .'" ' ) === -1 ) {
561+ continue ;
562+ }
563+
564+ $ pattern = '#<(.*?)>; rel=" ' .$ type .'"#m ' ;
565+ preg_match ($ pattern , $ headerLink , $ linkResponseHeaders );
566+ if ($ linkResponseHeaders ) {
567+ return $ linkResponseHeaders [1 ];
568+ }
569+ }
570+ }
571+ }
572+ return null ;
573+ }
574+
575+ public function getPrevLink (){
576+ return $ this ->prevLink ;
577+ }
578+
579+ public function getNextLink (){
580+ return $ this ->nextLink ;
581+ }
582+
583+ public function getNextPageParams (){
584+ $ nextPageParams = [];
585+ parse_str ($ this ->getNextLink (), $ nextPageParams );
586+ return $ nextPageParams ;
587+ }
588+
589+ public function getPrevPageParams (){
590+ $ nextPageParams = [];
591+ parse_str ($ this ->getPrevLink (), $ nextPageParams );
592+ return $ nextPageParams ;
593+ }
523594}
0 commit comments