@@ -170,11 +170,33 @@ public function getVersion()
170170 }
171171
172172 /**
173- * Load XML from string. Can throw exception
174- * @param string $response String from a CURL response
175- * @return SimpleXMLElement status_code, response
176- * @throws PrestaShopWebserviceException
177- */
173+ * Load XML from string. Can throw exception
174+ * @param string $response String from a CURL response
175+ * @param string $output String two options: XML or JSON
176+ * @return function parseXML or parseJSON
177+ */
178+ protected function parseResponse ($ response , $ output = 'XML ' )
179+ {
180+ if ($ response !== '' )
181+ {
182+ if ($ output === 'XML ' ) {
183+ return self ::parseXML ($ response );
184+ } else if ($ output === 'JSON ' ) {
185+ return self ::parseJSON ($ response );
186+ } else {
187+ throw new PrestaShopWebserviceException ('Not select a correct output. ' );
188+ }
189+ }
190+ else
191+ throw new PrestaShopWebserviceException ('HTTP response is empty ' );
192+ }
193+
194+ /**
195+ * Load XML from string. Can throw exception
196+ * @param string $response String from a CURL response
197+ * @return SimpleXMLElement status_code, response
198+ * @throws PrestaShopWebserviceException
199+ */
178200 protected function parseXML ($ response )
179201 {
180202 if ($ response != '' )
@@ -194,6 +216,25 @@ protected function parseXML($response)
194216 throw new PrestaShopWebserviceException ('HTTP response is empty ' );
195217 }
196218
219+ /**
220+ * Load XML from string. Can throw exception
221+ * @param string $response String from a CURL response
222+ * @return JSONElement status_code, response
223+ */
224+ protected function parseJSON ($ response )
225+ {
226+ if ($ response != '' )
227+ {
228+ if (json_decode ($ response , true )) {
229+ return json_decode ($ response , true );
230+ } else {
231+ throw new PrestaShopWebserviceException ('Error when parse json to array ' );
232+ }
233+ }
234+ else
235+ throw new PrestaShopWebserviceException ('HTTP response is empty ' );
236+ }
237+
197238 /**
198239 * Add (POST) a resource
199240 * <p>Unique parameter must take : <br><br>
@@ -265,21 +306,26 @@ public function get($options)
265306 if (isset ($ options ['id ' ]))
266307 $ url .= '/ ' .$ options ['id ' ];
267308
268- $ params = array ('filter ' , 'display ' , 'sort ' , 'limit ' , 'id_shop ' , 'id_group_shop ' ,'date ' );
309+ $ params = array ('filter ' , 'display ' , 'sort ' , 'limit ' , 'id_shop ' , 'id_group_shop ' ,'date ' , ' output_format ' );
269310 foreach ($ params as $ p )
270311 foreach ($ options as $ k => $ o )
271312 if (strpos ($ k , $ p ) !== false )
272313 $ url_params [$ k ] = $ options [$ k ];
273314 if (count ($ url_params ) > 0 )
274315 $ url .= '? ' .http_build_query ($ url_params );
316+
317+ $ outputFormat =
318+ (isset ($ options ['output_format ' ]) === true )
319+ ? $ options ['output_format ' ]
320+ : 'XML ' ;
275321 }
276322 else
277323 throw new PrestaShopWebserviceException ('Bad parameters given ' );
278324
279325 $ request = self ::executeRequest ($ url , array (CURLOPT_CUSTOMREQUEST => 'GET ' ));
280326
281327 self ::checkStatusCode ($ request ['status_code ' ]);// check the response validity
282- return self ::parseXML ($ request ['response ' ]);
328+ return self ::parseResponse ($ request ['response ' ], $ outputFormat );
283329 }
284330
285331 /**
@@ -300,7 +346,7 @@ public function head($options)
300346 if (isset ($ options ['id ' ]))
301347 $ url .= '/ ' .$ options ['id ' ];
302348
303- $ params = array ('filter ' , 'display ' , 'sort ' , 'limit ' );
349+ $ params = array ('filter ' , 'display ' , 'sort ' , 'limit ' , ' output_format ' );
304350 foreach ($ params as $ p )
305351 foreach ($ options as $ k => $ o )
306352 if (strpos ($ k , $ p ) !== false )
@@ -339,13 +385,18 @@ public function edit($options)
339385 $ url .= '&id_shop= ' .$ options ['id_shop ' ];
340386 if (isset ($ options ['id_group_shop ' ]))
341387 $ url .= '&id_group_shop= ' .$ options ['id_group_shop ' ];
388+
389+ $ outputFormat =
390+ (isset ($ options ['output_format ' ]) === true )
391+ ? $ options ['output_format ' ]
392+ : 'XML ' ;
342393 }
343394 else
344395 throw new PrestaShopWebserviceException ('Bad parameters given ' );
345396
346397 $ request = self ::executeRequest ($ url , array (CURLOPT_CUSTOMREQUEST => 'PUT ' , CURLOPT_POSTFIELDS => $ xml ));
347398 self ::checkStatusCode ($ request ['status_code ' ]);// check the response validity
348- return self ::parseXML ($ request ['response ' ]);
399+ return self ::parseResponse ($ request ['response ' ], $ outputFormat );
349400 }
350401
351402 /**
@@ -391,4 +442,4 @@ public function delete($options)
391442 }
392443
393444
394- }
445+ }
0 commit comments