Skip to content

Commit 587fa24

Browse files
authored
Merge pull request #5 from phpclassic/master
Updating to most recent version of upstream
2 parents f0963bf + c232b69 commit 587fa24

File tree

9 files changed

+190
-10
lines changed

9 files changed

+190
-10
lines changed

lib/Collection.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PHPShopify;
4+
5+
/**
6+
* --------------------------------------------------------------------------
7+
* Collection -> Child Resources
8+
* --------------------------------------------------------------------------
9+
*
10+
* @property-read Product $Product
11+
*
12+
* @method Product Product(integer $id = null)
13+
*
14+
* @see https://shopify.dev/docs/admin-api/rest/reference/products/collection
15+
*
16+
*/
17+
class Collection extends ShopifyResource
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public $readOnly = false;
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
protected $resourceKey = 'collection';
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
protected $childResource = array(
33+
'Product',
34+
);
35+
}

lib/CurlRequest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class CurlRequest
2828
*/
2929
public static $lastHttpCode;
3030

31+
/**
32+
* HTTP response headers of last executed request
33+
*
34+
* @var array
35+
*/
36+
public static $lastHttpResponseHeaders = array();
3137

3238
/**
3339
* Initialize the curl resource
@@ -170,7 +176,9 @@ protected static function processRequest($ch)
170176
// close curl resource to free up system resources
171177
curl_close($ch);
172178

179+
self::$lastHttpResponseHeaders = $response->getHeaders();
180+
173181
return $response->getBody();
174182
}
175-
183+
176184
}

lib/CurlResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ private function parse($response)
2727
foreach (\explode("\r\n", $headers) as $header) {
2828
$pair = \explode(': ', $header, 2);
2929
if (isset($pair[1])) {
30-
$this->headers[$pair[0]] = $pair[1];
30+
$headerKey = strtolower($pair[0]);
31+
$this->headers[$headerKey] = $pair[1];
3132
}
3233
}
3334
} else {

lib/GraphQL.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace PHPShopify;
1212

1313

14+
use PHPShopify\Exception\ApiException;
15+
use PHPShopify\Exception\CurlException;
1416
use PHPShopify\Exception\SdkException;
1517

1618
class GraphQL extends ShopifyResource
@@ -33,6 +35,8 @@ protected function getResourcePath()
3335
* @param array|null $variables
3436
*
3537
* @uses HttpRequestGraphQL::post() to send the HTTP request
38+
* @throws ApiException if the response has an error specified
39+
* @throws CurlException if response received with unexpected HTTP code.
3640
*
3741
* @return array
3842
*/
@@ -47,6 +51,7 @@ public function post($graphQL, $url = null, $wrapData = false, $variables = null
4751

4852
/**
4953
* @inheritdoc
54+
* @throws SdkException
5055
*/
5156
public function get($urlParams = array(), $url = null, $dataKey = null)
5257
{
@@ -55,6 +60,7 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
5560

5661
/**
5762
* @inheritdoc
63+
* @throws SdkException
5864
*/
5965
public function put($dataArray, $url = null, $wrapData = true)
6066
{
@@ -63,6 +69,7 @@ public function put($dataArray, $url = null, $wrapData = true)
6369

6470
/**
6571
* @inheritdoc
72+
* @throws SdkException
6673
*/
6774
public function delete($urlParams = array(), $url = null)
6875
{

lib/HttpRequestJson.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected static function prepareRequest($httpHeaders = array(), $dataArray = ar
6262
* @param string $url
6363
* @param array $httpHeaders
6464
*
65-
* @return string
65+
* @return array
6666
*/
6767
public static function get($url, $httpHeaders = array())
6868
{
@@ -80,7 +80,7 @@ public static function get($url, $httpHeaders = array())
8080
* @param array $dataArray
8181
* @param array $httpHeaders
8282
*
83-
* @return string
83+
* @return array
8484
*/
8585
public static function post($url, $dataArray, $httpHeaders = array())
8686
{
@@ -98,7 +98,7 @@ public static function post($url, $dataArray, $httpHeaders = array())
9898
* @param array $dataArray
9999
* @param array $httpHeaders
100100
*
101-
* @return string
101+
* @return array
102102
*/
103103
public static function put($url, $dataArray, $httpHeaders = array())
104104
{
@@ -115,7 +115,7 @@ public static function put($url, $dataArray, $httpHeaders = array())
115115
* @param string $url
116116
* @param array $httpHeaders
117117
*
118-
* @return string
118+
* @return array
119119
*/
120120
public static function delete($url, $httpHeaders = array())
121121
{

lib/ShopifyResource.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPShopify\Exception\ApiException;
1313
use PHPShopify\Exception\SdkException;
1414
use PHPShopify\Exception\CurlException;
15+
use Psr\Http\Message\ResponseInterface;
1516

1617
/*
1718
|--------------------------------------------------------------------------
@@ -30,6 +31,13 @@ abstract class ShopifyResource
3031
*/
3132
protected $httpHeaders = array();
3233

34+
/**
35+
* HTTP response headers of last executed request
36+
*
37+
* @var array
38+
*/
39+
public static $lastHttpResponseHeaders = array();
40+
3341
/**
3442
* The base URL of the API Resource (excluding the '.json' extension).
3543
*
@@ -113,6 +121,21 @@ abstract class ShopifyResource
113121
*
114122
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115123
*/
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+
116139
public function __construct($id = null, $parentResourceUrl = '')
117140
{
118141
$this->id = $id;
@@ -302,6 +325,9 @@ public function generateUrl($urlParams = array(), $customAction = null)
302325
*
303326
* @uses HttpRequestJson::get() to send the HTTP request
304327
*
328+
* @throws ApiException if the response has an error specified
329+
* @throws CurlException if response received with unexpected HTTP code.
330+
*
305331
* @return array
306332
*/
307333
public function get($urlParams = array(), $url = null, $dataKey = null)
@@ -321,6 +347,10 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
321347
*
322348
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
323349
*
350+
* @throws SdkException
351+
* @throws ApiException if the response has an error specified
352+
* @throws CurlException if response received with unexpected HTTP code.
353+
*
324354
* @return integer
325355
*/
326356
public function count($urlParams = array())
@@ -340,6 +370,8 @@ public function count($urlParams = array())
340370
* @param mixed $query
341371
*
342372
* @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.
343375
*
344376
* @return array
345377
*/
@@ -365,6 +397,9 @@ public function search($query)
365397
*
366398
* @uses HttpRequestJson::post() to send the HTTP request
367399
*
400+
* @throws ApiException if the response has an error specified
401+
* @throws CurlException if response received with unexpected HTTP code.
402+
*
368403
* @return array
369404
*/
370405
public function post($dataArray, $url = null, $wrapData = true)
@@ -387,6 +422,9 @@ public function post($dataArray, $url = null, $wrapData = true)
387422
*
388423
* @uses HttpRequestJson::put() to send the HTTP request
389424
*
425+
* @throws ApiException if the response has an error specified
426+
* @throws CurlException if response received with unexpected HTTP code.
427+
*
390428
* @return array
391429
*/
392430
public function put($dataArray, $url = null, $wrapData = true)
@@ -409,6 +447,9 @@ public function put($dataArray, $url = null, $wrapData = true)
409447
*
410448
* @uses HttpRequestJson::delete() to send the HTTP request
411449
*
450+
* @throws ApiException if the response has an error specified
451+
* @throws CurlException if response received with unexpected HTTP code.
452+
*
412453
* @return array an empty array will be returned if the request is successfully completed
413454
*/
414455
public function delete($urlParams = array(), $url = null)
@@ -477,6 +518,8 @@ protected function castString($array)
477518
*/
478519
public function processResponse($responseArray, $dataKey = null)
479520
{
521+
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
522+
480523
if ($responseArray === null) {
481524
//Something went wrong, Checking HTTP Codes
482525
$httpOK = 200; //Request Successful, OK.
@@ -490,10 +533,13 @@ public function processResponse($responseArray, $dataKey = null)
490533
}
491534
}
492535

536+
$lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
537+
$this->getLinks($lastResponseHeaders);
538+
493539
if (isset($responseArray['errors'])) {
494540
$message = $this->castString($responseArray['errors']);
495541

496-
throw new ApiException($message);
542+
throw new ApiException($message, CurlRequest::$lastHttpCode);
497543
}
498544

499545
if ($dataKey && isset($responseArray[$dataKey])) {
@@ -502,4 +548,64 @@ public function processResponse($responseArray, $dataKey = null)
502548
return $responseArray;
503549
}
504550
}
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+
}
505611
}

lib/ShopifySDK.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @property-read Blog $Blog
7272
* @property-read CarrierService $CarrierService
7373
* @property-read Collect $Collect
74+
* @property-read Collection $Collection
7475
* @property-read Comment $Comment
7576
* @property-read Country $Country
7677
* @property-read Currency $Currency
@@ -110,6 +111,7 @@
110111
* @method Blog Blog(integer $id = null)
111112
* @method CarrierService CarrierService(integer $id = null)
112113
* @method Collect Collect(integer $id = null)
114+
* @method Collection Collection(integer $id = null)
113115
* @method Comment Comment(integer $id = null)
114116
* @method Country Country(integer $id = null)
115117
* @method Currency Currency(integer $id = null)
@@ -158,6 +160,7 @@ class ShopifySDK
158160
'Blog',
159161
'CarrierService',
160162
'Collect',
163+
'Collection',
161164
'Comment',
162165
'Country',
163166
'Currency',
@@ -205,13 +208,17 @@ class ShopifySDK
205208
*/
206209
public static $timeAllowedForEachApiCall = .5;
207210

211+
/**
212+
* @var string Default Shopify API version
213+
*/
214+
public static $defaultApiVersion = '2020-01';
215+
208216
/**
209217
* Shop / API configurations
210218
*
211219
* @var array
212220
*/
213221
public static $config = array(
214-
'ApiVersion' => '2019-10'
215222
);
216223

217224
/**
@@ -310,7 +317,7 @@ public static function config($config)
310317
* Reset config to it's initial values
311318
*/
312319
self::$config = array(
313-
'ApiVersion' => '2019-04'
320+
'ApiVersion' => self::$defaultApiVersion
314321
);
315322

316323
foreach ($config as $key => $value) {

0 commit comments

Comments
 (0)