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
|--------------------------------------------------------------------------
@@ -30,6 +31,13 @@ abstract class ShopifyResource
30
31
*/
31
32
protected $ httpHeaders = array ();
32
33
34
+ /**
35
+ * HTTP response headers of last executed request
36
+ *
37
+ * @var array
38
+ */
39
+ public static $ lastHttpResponseHeaders = array ();
40
+
33
41
/**
34
42
* The base URL of the API Resource (excluding the '.json' extension).
35
43
*
@@ -113,6 +121,21 @@ abstract class ShopifyResource
113
121
*
114
122
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115
123
*/
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
+
116
139
public function __construct ($ id = null , $ parentResourceUrl = '' )
117
140
{
118
141
$ this ->id = $ id ;
@@ -302,6 +325,9 @@ public function generateUrl($urlParams = array(), $customAction = null)
302
325
*
303
326
* @uses HttpRequestJson::get() to send the HTTP request
304
327
*
328
+ * @throws ApiException if the response has an error specified
329
+ * @throws CurlException if response received with unexpected HTTP code.
330
+ *
305
331
* @return array
306
332
*/
307
333
public function get ($ urlParams = array (), $ url = null , $ dataKey = null )
@@ -321,6 +347,10 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
321
347
*
322
348
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
323
349
*
350
+ * @throws SdkException
351
+ * @throws ApiException if the response has an error specified
352
+ * @throws CurlException if response received with unexpected HTTP code.
353
+ *
324
354
* @return integer
325
355
*/
326
356
public function count ($ urlParams = array ())
@@ -340,6 +370,8 @@ public function count($urlParams = array())
340
370
* @param mixed $query
341
371
*
342
372
* @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.
343
375
*
344
376
* @return array
345
377
*/
@@ -365,6 +397,9 @@ public function search($query)
365
397
*
366
398
* @uses HttpRequestJson::post() to send the HTTP request
367
399
*
400
+ * @throws ApiException if the response has an error specified
401
+ * @throws CurlException if response received with unexpected HTTP code.
402
+ *
368
403
* @return array
369
404
*/
370
405
public function post ($ dataArray , $ url = null , $ wrapData = true )
@@ -387,6 +422,9 @@ public function post($dataArray, $url = null, $wrapData = true)
387
422
*
388
423
* @uses HttpRequestJson::put() to send the HTTP request
389
424
*
425
+ * @throws ApiException if the response has an error specified
426
+ * @throws CurlException if response received with unexpected HTTP code.
427
+ *
390
428
* @return array
391
429
*/
392
430
public function put ($ dataArray , $ url = null , $ wrapData = true )
@@ -409,6 +447,9 @@ public function put($dataArray, $url = null, $wrapData = true)
409
447
*
410
448
* @uses HttpRequestJson::delete() to send the HTTP request
411
449
*
450
+ * @throws ApiException if the response has an error specified
451
+ * @throws CurlException if response received with unexpected HTTP code.
452
+ *
412
453
* @return array an empty array will be returned if the request is successfully completed
413
454
*/
414
455
public function delete ($ urlParams = array (), $ url = null )
@@ -477,6 +518,8 @@ protected function castString($array)
477
518
*/
478
519
public function processResponse ($ responseArray , $ dataKey = null )
479
520
{
521
+ self ::$ lastHttpResponseHeaders = CurlRequest::$ lastHttpResponseHeaders ;
522
+
480
523
if ($ responseArray === null ) {
481
524
//Something went wrong, Checking HTTP Codes
482
525
$ httpOK = 200 ; //Request Successful, OK.
@@ -490,10 +533,13 @@ public function processResponse($responseArray, $dataKey = null)
490
533
}
491
534
}
492
535
536
+ $ lastResponseHeaders = CurlRequest::$ lastHttpResponseHeaders ;
537
+ $ this ->getLinks ($ lastResponseHeaders );
538
+
493
539
if (isset ($ responseArray ['errors ' ])) {
494
540
$ message = $ this ->castString ($ responseArray ['errors ' ]);
495
541
496
- throw new ApiException ($ message );
542
+ throw new ApiException ($ message, CurlRequest:: $ lastHttpCode );
497
543
}
498
544
499
545
if ($ dataKey && isset ($ responseArray [$ dataKey ])) {
@@ -502,4 +548,64 @@ public function processResponse($responseArray, $dataKey = null)
502
548
return $ responseArray ;
503
549
}
504
550
}
551
+
552
+ public function getLinks ($ responseHeaders ){
553
+ $ this ->nextLink = $ this ->getLink ($ responseHeaders ,'next ' );
554
+ $ this ->prevLink = $ this ->getLink ($ responseHeaders ,'previous ' );
555
+ }
556
+
557
+ public function getLink ($ responseHeaders , $ type ='next ' ){
558
+
559
+ if (array_key_exists ('x-shopify-api-version ' , $ responseHeaders )
560
+ && $ responseHeaders ['x-shopify-api-version ' ] < '2019-07 ' ){
561
+ return null ;
562
+ }
563
+
564
+ if (!empty ($ responseHeaders ['link ' ])) {
565
+ if (stristr ($ responseHeaders ['link ' ], '; rel=" ' .$ type .'" ' ) > -1 ) {
566
+ $ headerLinks = explode (', ' , $ responseHeaders ['link ' ]);
567
+ foreach ($ headerLinks as $ headerLink ) {
568
+ if (stristr ($ headerLink , '; rel=" ' .$ type .'" ' ) === -1 ) {
569
+ continue ;
570
+ }
571
+
572
+ $ pattern = '#<(.*?)>; rel=" ' .$ type .'"#m ' ;
573
+ preg_match ($ pattern , $ headerLink , $ linkResponseHeaders );
574
+ if ($ linkResponseHeaders ) {
575
+ return $ linkResponseHeaders [1 ];
576
+ }
577
+ }
578
+ }
579
+ }
580
+
581
+ return null ;
582
+ }
583
+
584
+ public function getPrevLink (){
585
+ return $ this ->prevLink ;
586
+ }
587
+
588
+ public function getNextLink (){
589
+ return $ this ->nextLink ;
590
+ }
591
+
592
+ public function getUrlParams ($ url ) {
593
+ if ($ url ) {
594
+ $ parts = parse_url ($ url );
595
+ return $ parts ['query ' ];
596
+ }
597
+ return '' ;
598
+ }
599
+
600
+ public function getNextPageParams (){
601
+ $ nextPageParams = [];
602
+ parse_str ($ this ->getUrlParams ($ this ->getNextLink ()), $ nextPageParams );
603
+ return $ nextPageParams ;
604
+ }
605
+
606
+ public function getPrevPageParams (){
607
+ $ nextPageParams = [];
608
+ parse_str ($ this ->getUrlParams ($ this ->getPrevLink ()), $ nextPageParams );
609
+ return $ nextPageParams ;
610
+ }
505
611
}
0 commit comments