@@ -49,8 +49,7 @@ abstract class ShopifyAPI
49
49
*
50
50
* @var string
51
51
*/
52
- //TODO Change $apiUrl to $resourceUrl
53
- protected $ apiUrl ;
52
+ protected $ resourceUrl ;
54
53
55
54
/**
56
55
* Key of the API Resource which is used to fetch data from request responses
@@ -117,22 +116,10 @@ public function __construct($config, $id = null)
117
116
$ this ->id = $ id ;
118
117
119
118
$ this ->config = $ config ;
120
- //TODO Move the url manipulation part to ShopifyClient class
121
- //Remove https:// and trailing slash (if provided)
122
- $ shopUrl = preg_replace ('#^https?://|/$# ' , '' ,$ config ['ShopUrl ' ]);
123
-
124
- $ this ->shopUrl = $ shopUrl ;
125
119
126
120
$ parentResource = isset ($ config ['ParentResource ' ]) ? $ config ['ParentResource ' ] : '' ;
127
121
128
- if (isset ($ config ['ApiKey ' ])) {
129
- $ apiKey = $ config ['ApiKey ' ];
130
- $ password = $ config ['Password ' ];
131
-
132
- $ this ->apiUrl = "https:// $ apiKey: $ password@ " . $ shopUrl . '/admin/ ' . $ parentResource . $ this ->getResourcePath ();
133
- } else {
134
- $ this ->apiUrl = 'https:// ' . $ shopUrl . '/admin/ ' . $ parentResource . $ this ->getResourcePath ();
135
- }
122
+ $ this ->resourceUrl = $ config ['ApiUrl ' ] . $ parentResource . $ this ->getResourcePath () . ($ this ->id ? '/ ' . $ this ->id : '' );
136
123
}
137
124
138
125
/**
@@ -166,30 +153,33 @@ public function __get($childName)
166
153
*/
167
154
public function __call ($ name , $ arguments )
168
155
{
169
- //If the $name starts with an uppercase letter, it's considered as a child class and a custom action otherwise
156
+ //If the $name starts with an uppercase letter, it's considered as a child class
157
+ //Otherwise it's a custom action
170
158
if (ctype_upper ($ name [0 ])) {
171
- $ childClass = array_search ($ name , $ this ->childResource );
159
+ //Get the array key of the childResource in the childResource array
160
+ $ childKey = array_search ($ name , $ this ->childResource );
172
161
173
- if ($ childClass === false ) {
162
+ if ($ childKey === false ) {
174
163
throw new \SdkException ("Child Resource $ name is not available for " . $ this ->getResourceName ());
175
- } elseif (is_numeric ($ childClass )) {
176
- //If any associative key is given to the childname, then it will be considered as the class name, otherwise the childname will be the class name
177
- $ childClass = $ name ;
178
164
}
179
165
180
- $ apiClassName = __NAMESPACE__ . "\\" . $ childClass ;
166
+ //If any associative key is given to the childname, then it will be considered as the class name,
167
+ //otherwise the childname will be the class name
168
+ $ childClassName = !is_numeric ($ childKey ) ? $ childKey : $ name ;
169
+
170
+ $ childClass = __NAMESPACE__ . "\\" . $ childClassName ;
171
+
181
172
$ config = $ this ->config ;
182
173
183
174
//Set the parent resource path for the child class
184
175
$ config ['ParentResource ' ] = (isset ($ config ['ParentResource ' ]) ? $ config ['ParentResource ' ] : '' ) . $ this ->getResourcePath () . '/ ' . $ this ->id . '/ ' ;
185
176
186
- $ api = new $ apiClassName ($ config );
187
177
188
- if (! empty ( $ arguments )) {
189
- //if the first argument is provided with the call, consider it as a resource ID
190
- $ resourceID = $ arguments [ 0 ];
191
- $ api -> id = $ resourceID ;
192
- }
178
+ //If first argument is provided, it will be considered as the ID of the resource.
179
+ $ resourceID = ! empty ( $ arguments ) ? $ arguments [ 0 ] : null ;
180
+
181
+
182
+ $ api = new $ childClass ( $ config , $ resourceID );
193
183
194
184
return $ api ;
195
185
} else {
@@ -200,26 +190,25 @@ public function __call($name, $arguments)
200
190
'delete ' => 'customDeleteActions ' ,
201
191
);
202
192
193
+ //Get the array key for the action in the actions array
203
194
foreach ($ actionMaps as $ httpMethod => $ actionArrayKey ) {
204
195
$ actionKey = array_search ($ name , $ this ->$ actionArrayKey );
205
196
if ($ actionKey !== false ) break ;
206
197
}
207
198
208
199
if ($ actionKey === false ) {
209
200
throw new SdkException ("No action named $ name is defined for " . $ this ->getResourceName ());
210
- } elseif (is_numeric ($ actionKey )) {
211
- //If any associative key is given to the action name, then it will be considered as the method name, otherwise the action name will be the method name
212
- $ actionKey = $ name ;
213
201
}
214
202
215
- $ params = array ();
203
+ //If any associative key is given to the action, then it will be considered as the method name,
204
+ //otherwise the action name will be the method name
205
+ $ customAction = !is_numeric ($ actionKey ) ? $ actionKey : $ name ;
206
+
216
207
217
208
//Get the first argument if provided with the method call
218
- if (!empty ($ arguments )) {
219
- $ params = $ arguments [0 ];
220
- }
209
+ $ params = !empty ($ arguments ) ? $ arguments [0 ] : array ();
221
210
222
- $ url = $ this ->apiUrl . ( $ this -> id ? ' / ' . $ this -> id : '' ) . " / $ actionKey .json " ;
211
+ $ url = $ this ->generateUrl ( $ params , $ customAction ) ;
223
212
224
213
return $ this ->$ httpMethod ($ params , $ url );
225
214
}
@@ -256,14 +245,28 @@ protected function pluralizeKey() {
256
245
257
246
/**
258
247
* Get the resource path to be used to generate the api url
259
- * Normally its the same as the pluralized version of the resource key, when it's different, the specific resource class will override this function
248
+ * Normally its the same as the pluralized version of the resource key,
249
+ * when it's different, the specific resource class will override this function
260
250
*
261
251
* @return string
262
252
*/
263
253
protected function getResourcePath () {
264
254
return $ this ->pluralizeKey ();
265
255
}
266
256
257
+ /**
258
+ * Generate the custom url for api request based on the params and custom action (if any)
259
+ *
260
+ * @param array $params
261
+ * @param string $customAction
262
+ *
263
+ * @return string
264
+ */
265
+ public function generateUrl ($ params = array (), $ customAction = null )
266
+ {
267
+ return $ this ->resourceUrl . ($ customAction ? "/ $ customAction " : '' ) . '.json ' . (!empty ($ params ) ? '? ' . http_build_query ($ params ) : '' );
268
+ }
269
+
267
270
/**
268
271
* Generate a HTTP GET request and return results as an array
269
272
*
@@ -277,7 +280,7 @@ protected function getResourcePath() {
277
280
public function get ($ params = array (), $ url = null )
278
281
{
279
282
280
- if (! $ url ) $ url = $ this ->apiUrl . ( $ this -> id ? " / { $ this -> id }" : '' ) . ' .json ' . (! empty ( $ params) ? ' ? ' . http_build_query ( $ params ) : '' );
283
+ if (! $ url ) $ url = $ this ->generateUrl ( $ params );
281
284
282
285
$ this ->prepareRequest ();
283
286
@@ -302,7 +305,7 @@ public function get($params = array(), $url = null)
302
305
public function getCount ($ params = array (), $ url = null )
303
306
{
304
307
305
- if (! $ url ) $ url = $ this ->apiUrl . ' /count.json ' . (! empty ( $ params) ? ' ? ' . http_build_query ( $ params ) : ' ' );
308
+ if (! $ url ) $ url = $ this ->generateUrl ( $ params, ' count ' );
306
309
307
310
$ this ->prepareRequest ();
308
311
@@ -329,7 +332,7 @@ public function search($query)
329
332
330
333
if (! is_array ($ query )) $ query = array ('query ' => $ query );
331
334
332
- $ url = $ this ->apiUrl . ' / search.json? ' . http_build_query ( $ query );
335
+ $ url = $ this ->generateUrl ( $ query , ' search ' );
333
336
334
337
return $ this ->get (array (), $ url );
335
338
}
@@ -346,7 +349,7 @@ public function search($query)
346
349
*/
347
350
public function post ($ data , $ url = null )
348
351
{
349
- if (! $ url ) $ url = $ this ->apiUrl . ' .json ' ;
352
+ if (! $ url ) $ url = $ this ->generateUrl () ;
350
353
351
354
$ data = array ($ this ->getResourcePostKey () => $ data );
352
355
@@ -370,7 +373,7 @@ public function post($data, $url = null)
370
373
public function put ($ data , $ url = null )
371
374
{
372
375
373
- if (! $ url ) $ url = $ this ->apiUrl . ( $ this -> id ? " / { $ this -> id }" : '' ) . ' .json ' ;
376
+ if (! $ url ) $ url = $ this ->generateUrl () ;
374
377
375
378
$ data = array ($ this ->getResourcePostKey () => $ data );
376
379
@@ -393,7 +396,7 @@ public function put($data, $url = null)
393
396
*/
394
397
public function delete ($ params = array (), $ url = null )
395
398
{
396
- if (! $ url ) $ url = $ this ->apiUrl . ( $ this -> id ? " / { $ this -> id }" : '' ) . ' .json ' . (! empty ( $ params) ? ' ? ' . http_build_query ( $ params ) : '' );
399
+ if (! $ url ) $ url = $ this ->generateUrl ( $ params );
397
400
398
401
$ this ->prepareRequest ();
399
402
0 commit comments