12
12
use PHPShopify \Exception \ApiException ;
13
13
use PHPShopify \Exception \SdkException ;
14
14
use PHPShopify \Exception \CurlException ;
15
+ use Psr \Http \Message \ResponseInterface ;
15
16
16
17
/*
17
18
|--------------------------------------------------------------------------
@@ -113,6 +114,21 @@ abstract class ShopifyResource
113
114
*
114
115
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115
116
*/
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
+
116
132
public function __construct ($ id = null , $ parentResourceUrl = '' )
117
133
{
118
134
$ this ->id = $ id ;
@@ -508,6 +524,9 @@ public function processResponse($responseArray, $dataKey = null)
508
524
}
509
525
}
510
526
527
+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
528
+ $ this ->getLinks ($ lastResponseHeaders );
529
+
511
530
if (isset ($ responseArray ['errors ' ])) {
512
531
$ message = $ this ->castString ($ responseArray ['errors ' ]);
513
532
@@ -520,4 +539,56 @@ public function processResponse($responseArray, $dataKey = null)
520
539
return $ responseArray ;
521
540
}
522
541
}
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
+ }
523
594
}
0 commit comments