55use JsonException ;
66use Redmine \Api ;
77use Redmine \Client \Client ;
8+ use Redmine \Exception \SerializerException ;
9+ use Redmine \Serializer \JsonSerializer ;
10+ use Redmine \Serializer \PathSerializer ;
11+ use Redmine \Serializer \XmlSerializer ;
812use SimpleXMLElement ;
913
1014/**
@@ -51,7 +55,7 @@ public function lastCallFailed()
5155 */
5256 protected function get ($ path , $ decodeIfJson = true )
5357 {
54- $ this ->client ->requestGet ($ path );
58+ $ this ->client ->requestGet (strval ( $ path) );
5559
5660 $ body = $ this ->client ->getLastResponseBody ();
5761 $ contentType = $ this ->client ->getLastResponseContentType ();
@@ -63,9 +67,9 @@ protected function get($path, $decodeIfJson = true)
6367
6468 if (true === $ decodeIfJson && '' !== $ body && 0 === strpos ($ contentType , 'application/json ' )) {
6569 try {
66- return json_decode ($ body, true , 512 , \ JSON_THROW_ON_ERROR );
67- } catch (JsonException $ e ) {
68- return 'Error decoding body as JSON: ' .$ e ->getMessage ();
70+ return JsonSerializer:: createFromString ($ body)-> getNormalized ( );
71+ } catch (SerializerException $ e ) {
72+ return 'Error decoding body as JSON: ' .$ e ->getPrevious ()-> getMessage ();
6973 }
7074 }
7175
@@ -161,23 +165,54 @@ protected function sanitizeParams(array $defaults, array $params)
161165 * Retrieves all the elements of a given endpoint (even if the
162166 * total number of elements is greater than 100).
163167 *
168+ * @deprecated the `retrieveAll()` method is deprecated, use `retrieveData()` instead.
169+ *
164170 * @param string $endpoint API end point
165171 * @param array $params optional parameters to be passed to the api (offset, limit, ...)
166172 *
167- * @return array elements found
173+ * @return array|false elements found
168174 */
169175 protected function retrieveAll ($ endpoint , array $ params = [])
176+ {
177+ @trigger_error ('The ' .__METHOD__ .' method is deprecated, use `retrieveData()` instead. ' , E_USER_DEPRECATED );
178+
179+ try {
180+ $ data = $ this ->retrieveData (strval ($ endpoint ), $ params );
181+ } catch (SerializerException $ e ) {
182+ $ data = false ;
183+ }
184+
185+ return $ data ;
186+ }
187+
188+ /**
189+ * Retrieves all the elements of a given endpoint (even if the
190+ * total number of elements is greater than 100).
191+ *
192+ * @param string $endpoint API end point
193+ * @param array $params optional query parameters to be passed to the api (offset, limit, ...)
194+ *
195+ * @throws SerializerException if response body could not be converted into array
196+ *
197+ * @return array elements found
198+ */
199+ protected function retrieveData (string $ endpoint , array $ params = []): array
170200 {
171201 if (empty ($ params )) {
172- return $ this ->get ($ endpoint );
202+ $ this ->client ->requestGet ($ endpoint );
203+
204+ return $ this ->getLastResponseBodyAsArray ();
173205 }
174- $ defaults = [
175- 'limit ' => 25 ,
176- 'offset ' => 0 ,
177- ];
178- $ params = $ this ->sanitizeParams ($ defaults , $ params );
179206
180- $ ret = [];
207+ $ params = $ this ->sanitizeParams (
208+ [
209+ 'limit ' => 25 ,
210+ 'offset ' => 0 ,
211+ ],
212+ $ params
213+ );
214+
215+ $ returnData = [];
181216
182217 $ limit = $ params ['limit ' ];
183218 $ offset = $ params ['offset ' ];
@@ -193,10 +228,16 @@ protected function retrieveAll($endpoint, array $params = [])
193228 $ params ['limit ' ] = $ _limit ;
194229 $ params ['offset ' ] = $ offset ;
195230
196- $ newDataSet = (array ) $ this ->get ($ endpoint .'? ' .preg_replace ('/%5B[0-9]+%5D/simU ' , '%5B%5D ' , http_build_query ($ params )));
197- $ ret = array_merge_recursive ($ ret , $ newDataSet );
231+ $ this ->client ->requestGet (
232+ PathSerializer::create ($ endpoint , $ params )->getPath ()
233+ );
234+
235+ $ newDataSet = $ this ->getLastResponseBodyAsArray ();
236+
237+ $ returnData = array_merge_recursive ($ returnData , $ newDataSet );
198238
199239 $ offset += $ _limit ;
240+
200241 if (empty ($ newDataSet ) || !isset ($ newDataSet ['limit ' ]) || (
201242 isset ($ newDataSet ['offset ' ]) &&
202243 isset ($ newDataSet ['total_count ' ]) &&
@@ -207,7 +248,7 @@ protected function retrieveAll($endpoint, array $params = [])
207248 }
208249 }
209250
210- return $ ret ;
251+ return $ returnData ;
211252 }
212253
213254 /**
@@ -254,4 +295,31 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
254295
255296 return $ xml ;
256297 }
298+
299+ /**
300+ * returns the last response body as array
301+ *
302+ * @throws SerializerException if response body could not be converted into array
303+ */
304+ private function getLastResponseBodyAsArray (): array
305+ {
306+ $ body = $ this ->client ->getLastResponseBody ();
307+ $ contentType = $ this ->client ->getLastResponseContentType ();
308+ $ returnData = null ;
309+
310+ // parse XML
311+ if (0 === strpos ($ contentType , 'application/xml ' )) {
312+ $ returnData = XmlSerializer::createFromString ($ body )->getNormalized ();
313+ } else if (0 === strpos ($ contentType , 'application/json ' )) {
314+ $ returnData = JsonSerializer::createFromString ($ body )->getNormalized ();
315+ }
316+
317+ if (! is_array ($ returnData )) {
318+ throw new SerializerException (
319+ 'Could not convert response body into array: ' . $ body
320+ );
321+ }
322+
323+ return $ returnData ;
324+ }
257325}
0 commit comments