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 ;
@@ -490,6 +506,9 @@ public function processResponse($responseArray, $dataKey = null)
490506 }
491507 }
492508
509+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
510+ $ this ->getLinks ($ lastResponseHeaders );
511+
493512 if (isset ($ responseArray ['errors ' ])) {
494513 $ message = $ this ->castString ($ responseArray ['errors ' ]);
495514
@@ -502,4 +521,56 @@ public function processResponse($responseArray, $dataKey = null)
502521 return $ responseArray ;
503522 }
504523 }
524+
525+ public function getLinks ($ responseHeaders ){
526+ $ this ->nextLink = $ this ->getLink ($ responseHeaders ,'next ' );
527+ $ this ->prevLink = $ this ->getLink ($ responseHeaders ,'prev ' );
528+ }
529+
530+ public function getLink ($ responseHeaders , $ type ='next ' ){
531+ $ responseHeaders = json_decode ($ responseHeaders );
532+
533+ if (property_exists ($ responseHeaders ,'x-shopify-api-version ' )
534+ && $ responseHeaders ->{'x-shopify-api-version ' } < '2019-07 ' ){
535+ return null ;
536+ }
537+
538+ if (!empty ($ responseHeaders ->link )) {
539+ if (stristr ($ responseHeaders ->link [0 ], '; rel=" ' .$ type .'" ' ) > -1 ) {
540+ $ headerLinks = explode (', ' , $ responseHeaders ->link [0 ]);
541+ foreach ($ headerLinks as $ headerLink ) {
542+ if (stristr ($ headerLink , '; rel=" ' .$ type .'" ' ) === -1 ) {
543+ continue ;
544+ }
545+
546+ $ pattern = '#<(.*?)>; rel=" ' .$ type .'"#m ' ;
547+ preg_match ($ pattern , $ headerLink , $ linkResponseHeaders );
548+ if ($ linkResponseHeaders ) {
549+ return $ linkResponseHeaders [1 ];
550+ }
551+ }
552+ }
553+ }
554+ return null ;
555+ }
556+
557+ public function getPrevLink (){
558+ return $ this ->prevLink ;
559+ }
560+
561+ public function getNextLink (){
562+ return $ this ->nextLink ;
563+ }
564+
565+ public function getNextPageParams (){
566+ $ nextPageParams = [];
567+ parse_str ($ this ->getNextLink (), $ nextPageParams );
568+ return $ nextPageParams ;
569+ }
570+
571+ public function getPrevPageParams (){
572+ $ nextPageParams = [];
573+ parse_str ($ this ->getPrevLink (), $ nextPageParams );
574+ return $ nextPageParams ;
575+ }
505576}
0 commit comments