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 ;
@@ -490,6 +506,9 @@ public function processResponse($responseArray, $dataKey = null)
490
506
}
491
507
}
492
508
509
+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
510
+ $ this ->getLinks ($ lastResponseHeaders );
511
+
493
512
if (isset ($ responseArray ['errors ' ])) {
494
513
$ message = $ this ->castString ($ responseArray ['errors ' ]);
495
514
@@ -502,4 +521,56 @@ public function processResponse($responseArray, $dataKey = null)
502
521
return $ responseArray ;
503
522
}
504
523
}
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
+ }
505
576
}
0 commit comments