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
|--------------------------------------------------------------------------
@@ -120,6 +121,21 @@ abstract class ShopifyResource
120
121
*
121
122
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
122
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
+
123
139
public function __construct ($ id = null , $ parentResourceUrl = '' )
124
140
{
125
141
$ this ->id = $ id ;
@@ -309,6 +325,9 @@ public function generateUrl($urlParams = array(), $customAction = null)
309
325
*
310
326
* @uses HttpRequestJson::get() to send the HTTP request
311
327
*
328
+ * @throws ApiException if the response has an error specified
329
+ * @throws CurlException if response received with unexpected HTTP code.
330
+ *
312
331
* @return array
313
332
*/
314
333
public function get ($ urlParams = array (), $ url = null , $ dataKey = null )
@@ -328,6 +347,10 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
328
347
*
329
348
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
330
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
+ *
331
354
* @return integer
332
355
*/
333
356
public function count ($ urlParams = array ())
@@ -347,6 +370,8 @@ public function count($urlParams = array())
347
370
* @param mixed $query
348
371
*
349
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.
350
375
*
351
376
* @return array
352
377
*/
@@ -372,6 +397,9 @@ public function search($query)
372
397
*
373
398
* @uses HttpRequestJson::post() to send the HTTP request
374
399
*
400
+ * @throws ApiException if the response has an error specified
401
+ * @throws CurlException if response received with unexpected HTTP code.
402
+ *
375
403
* @return array
376
404
*/
377
405
public function post ($ dataArray , $ url = null , $ wrapData = true )
@@ -394,6 +422,9 @@ public function post($dataArray, $url = null, $wrapData = true)
394
422
*
395
423
* @uses HttpRequestJson::put() to send the HTTP request
396
424
*
425
+ * @throws ApiException if the response has an error specified
426
+ * @throws CurlException if response received with unexpected HTTP code.
427
+ *
397
428
* @return array
398
429
*/
399
430
public function put ($ dataArray , $ url = null , $ wrapData = true )
@@ -416,6 +447,9 @@ public function put($dataArray, $url = null, $wrapData = true)
416
447
*
417
448
* @uses HttpRequestJson::delete() to send the HTTP request
418
449
*
450
+ * @throws ApiException if the response has an error specified
451
+ * @throws CurlException if response received with unexpected HTTP code.
452
+ *
419
453
* @return array an empty array will be returned if the request is successfully completed
420
454
*/
421
455
public function delete ($ urlParams = array (), $ url = null )
@@ -499,10 +533,13 @@ public function processResponse($responseArray, $dataKey = null)
499
533
}
500
534
}
501
535
536
+ $ lastResponseHeaders = CurlRequest::$ lastResponseHeaders ;
537
+ $ this ->getLinks ($ lastResponseHeaders );
538
+
502
539
if (isset ($ responseArray ['errors ' ])) {
503
540
$ message = $ this ->castString ($ responseArray ['errors ' ]);
504
541
505
- throw new ApiException ($ message );
542
+ throw new ApiException ($ message, CurlRequest:: $ lastHttpCode );
506
543
}
507
544
508
545
if ($ dataKey && isset ($ responseArray [$ dataKey ])) {
@@ -512,106 +549,56 @@ public function processResponse($responseArray, $dataKey = null)
512
549
}
513
550
}
514
551
515
- /**
516
- * Checks response headers for existence of next page info
517
- *
518
- * @return boolean
519
- */
520
- static public function lastResourceContainsNextPageInfo ()
521
- {
522
- $ headers = self ::$ lastHttpResponseHeaders ;
523
-
524
- if (isset ($ headers ["Link " ])) {
525
- $ matchData = array ();
526
-
527
- if (preg_match ("/<([^>]*)>; rel= \"next \"/ " , $ headers ["Link " ], $ matchData )) {
528
- // found rel="next"
529
- return true ;
530
- }
531
- }
532
-
533
- return false ;
552
+ public function getLinks ($ responseHeaders ){
553
+ $ this ->nextLink = $ this ->getLink ($ responseHeaders ,'next ' );
554
+ $ this ->prevLink = $ this ->getLink ($ responseHeaders ,'prev ' );
534
555
}
535
556
536
- /**
537
- * Checks response headers for existence of previous page info
538
- *
539
- * @return boolean
540
- */
557
+ public function getLink ($ responseHeaders , $ type ='next ' ){
558
+ $ responseHeaders = json_decode ($ responseHeaders );
541
559
542
- static public function lastResourceContainsPrevPageInfo ()
543
- {
544
- $ headers = self ::$ lastHttpResponseHeaders ;
545
-
546
- if (isset ($ headers ["Link " ])) {
547
- $ matchData = array ();
548
-
549
- if (preg_match ("/<([^>]*)>; rel= \"previous \"/ " , $ headers ["Link " ], $ matchData )) {
550
- // found rel="prev"
551
- return true ;
552
- }
560
+ if (property_exists ($ responseHeaders ,'x-shopify-api-version ' )
561
+ && $ responseHeaders ->{'x-shopify-api-version ' } < '2019-07 ' ){
562
+ return null ;
553
563
}
554
564
555
- return false ;
556
- }
557
-
558
- /**
559
- * Gets next page info string for use in pagination
560
- *
561
- * @return string
562
- */
563
- static public function getNextPageInfo ()
564
- {
565
- $ headers = self ::$ lastHttpResponseHeaders ;
566
-
567
- if (isset ($ headers ["Link " ])) {
568
- $ matchData = array ();
569
-
570
- if (preg_match ("/<([^>]*)>; rel= \"next \"/ " , $ headers ["Link " ], $ matchData )) {
571
- // found rel="next"
572
- $ query = parse_url ($ matchData [1 ], PHP_URL_QUERY );
573
-
574
- $ pairs = explode ( "& " , $ query );
575
- foreach ( $ pairs as $ p ) {
576
- list ( $ key , $ value ) = explode ( "= " , $ p );
565
+ if (!empty ($ responseHeaders ->link )) {
566
+ if (stristr ($ responseHeaders ->link [0 ], '; rel=" ' .$ type .'" ' ) > -1 ) {
567
+ $ headerLinks = explode (', ' , $ responseHeaders ->link [0 ]);
568
+ foreach ($ headerLinks as $ headerLink ) {
569
+ if (stristr ($ headerLink , '; rel=" ' .$ type .'" ' ) === -1 ) {
570
+ continue ;
571
+ }
577
572
578
- if ( $ key == "page_info " ) {
579
- return $ value ;
573
+ $ pattern = '#<(.*?)>; rel=" ' .$ type .'"#m ' ;
574
+ preg_match ($ pattern , $ headerLink , $ linkResponseHeaders );
575
+ if ($ linkResponseHeaders ) {
576
+ return $ linkResponseHeaders [1 ];
580
577
}
581
578
}
582
579
}
583
580
}
584
581
585
- return false ;
582
+ return null ;
586
583
}
587
584
588
- /**
589
- * Gets previous page info string for use in pagination
590
- *
591
- * @return string
592
- */
593
- static public function getPrevPageInfo ()
594
- {
595
- $ headers = self ::$ lastHttpResponseHeaders ;
596
-
597
- if (isset ($ headers ["Link " ])) {
598
- $ matchData = array ();
599
-
600
- if (preg_match ("/<([^>]*)>; rel= \"previous \"/ " , $ headers ["Link " ], $ matchData )) {
601
- // found rel="prev"
602
- $ query = parse_url ($ matchData [1 ], PHP_URL_QUERY );
585
+ public function getPrevLink (){
586
+ return $ this ->prevLink ;
587
+ }
603
588
604
- $ pairs = explode ( " & " , $ query );
605
- foreach ( $ pairs as $ p ) {
606
- list ( $ key , $ value ) = explode ( " = " , $ p );
589
+ public function getNextLink (){
590
+ return $ this -> nextLink ;
591
+ }
607
592
608
- if ( $ key == "page_info " ) {
609
- return $ value ;
610
- }
611
- }
612
- }
613
- }
593
+ public function getNextPageParams (){
594
+ $ nextPageParams = [];
595
+ parse_str ($ this ->getNextLink (), $ nextPageParams );
596
+ return $ nextPageParams ;
597
+ }
614
598
615
- return false ;
599
+ public function getPrevPageParams (){
600
+ $ nextPageParams = [];
601
+ parse_str ($ this ->getPrevLink (), $ nextPageParams );
602
+ return $ nextPageParams ;
616
603
}
617
604
}
0 commit comments