@@ -22,6 +22,10 @@ abstract class AbstractEndpoint
2222 /** @var string */
2323 protected $ resourcePath ;
2424 /** @var string */
25+ protected $ singleResourceKey ;
26+ /** @var string */
27+ protected $ listResourceKey ;
28+ /** @var string */
2529 protected $ parentId ;
2630
2731 /**
@@ -43,15 +47,6 @@ protected function buildQueryString(array $filters)
4347 return '' ;
4448 }
4549
46- foreach ($ filters as $ key => $ value ) {
47- if ($ value === true ) {
48- $ filters [$ key ] = 'true ' ;
49- }
50- if ($ value === false ) {
51- $ filters [$ key ] = 'false ' ;
52- }
53- }
54-
5550 return '? ' . http_build_query ($ filters , '' , '& ' );
5651 }
5752
@@ -68,7 +63,7 @@ protected function restCreate(array $body)
6863 $ this ->parseRequestBody ($ body )
6964 );
7065
71- return ResourceFactory::createFromApiResult ($ result , $ this ->getResourceObject ());
66+ return ResourceFactory::createFromApiResult ($ result , $ this ->getResourceObject (), $ this -> getSingleResourceKey () );
7267 }
7368
7469 /**
@@ -77,7 +72,7 @@ protected function restCreate(array $body)
7772 * @return AbstractResource
7873 * @throws ApiException
7974 */
80- protected function restRead ($ id , array $ filters )
75+ protected function restRead ($ id , array $ filters ): AbstractResource
8176 {
8277 if (empty ($ id )) {
8378 throw new ApiException ('Invalid resource id. ' );
@@ -90,7 +85,7 @@ protected function restRead($id, array $filters)
9085 "{$ this ->getResourcePath ()}/ {$ id }" . $ this ->buildQueryString ($ filters )
9186 );
9287
93- return ResourceFactory::createFromApiResult ($ result , $ this ->getResourceObject ());
88+ return ResourceFactory::createFromApiResult ($ result , $ this ->getResourceObject (), $ this -> getSingleResourceKey () );
9489 }
9590
9691 /**
@@ -99,7 +94,7 @@ protected function restRead($id, array $filters)
9994 * @return AbstractResource|null
10095 * @throws ApiException
10196 */
102- protected function restDelete ($ id , array $ body = [])
97+ protected function restDelete ($ id , array $ body = []): AbstractResource
10398 {
10499 if (empty ($ id )) {
105100 throw new ApiException ('Invalid resource id. ' );
@@ -122,10 +117,10 @@ protected function restDelete($id, array $body = [])
122117
123118 /**
124119 * @param array $filters
125- * @return array| AbstractCollection
120+ * @return AbstractCollection
126121 * @throws ApiException
127122 */
128- protected function restList (array $ filters = [])
123+ protected function restList (array $ filters = []): AbstractCollection
129124 {
130125 $ apiPath = $ this ->getResourcePath ();
131126
@@ -135,33 +130,29 @@ protected function restList(array $filters = [])
135130
136131 $ result = $ this ->client ->performHttpCall (self ::REST_LIST , $ apiPath );
137132
138- $ collection = null ;
133+ $ previous = null ;
134+ $ next = null ;
139135
140- if (is_object ($ result )) {
141- $ previous = null ;
142- $ next = null ;
136+ if (isset ($ result-> previous )) {
137+ $ previous = $ result -> previous ;
138+ }
143139
144- if (isset ($ result ->previous )) {
145- $ previous = $ result ->previous ;
146- }
140+ if (isset ($ result ->next )) {
141+ $ next = $ result ->next ;
142+ }
147143
148- if (isset ($ result ->next )) {
149- $ next = $ result ->next ;
150- }
144+ /** @var AbstractCollection $collection */
145+ $ collection = $ this ->getResourceCollectionObject (
146+ $ previous ,
147+ $ next
148+ );
151149
152- /** @var AbstractCollection $collection */
153- $ collection = $ this ->getResourceCollectionObject (
154- $ previous ,
155- $ next
156- );
150+ if (is_object ($ result )) {
151+ $ result = $ result ->{$ collection ->getCollectionResourceName ()};
152+ }
157153
158- foreach ($ result ->{$ collection ->getCollectionResourceName ()} as $ dataResult ) {
159- $ collection [] = ResourceFactory::createFromApiResult ($ dataResult , $ this ->getResourceObject ());
160- }
161- } else {
162- foreach ($ result as $ dataResult ) {
163- $ collection [] = ResourceFactory::createFromApiResult ($ dataResult , $ this ->getResourceObject ());
164- }
154+ foreach ($ result as $ dataResult ) {
155+ $ collection [] = ResourceFactory::createFromApiResult ($ dataResult , $ this ->getResourceObject ());
165156 }
166157
167158 return $ collection ;
@@ -196,6 +187,22 @@ public function getResourcePath()
196187 return $ this ->resourcePath ;
197188 }
198189
190+ /**
191+ * @return string
192+ */
193+ protected function getSingleResourceKey ()
194+ {
195+ return $ this ->singleResourceKey ;
196+ }
197+
198+ /**
199+ * @return string
200+ */
201+ protected function getListResourceKey ()
202+ {
203+ return $ this ->listResourceKey ;
204+ }
205+
199206 /**
200207 * @param array $body
201208 * @return null|string
@@ -206,8 +213,9 @@ protected function parseRequestBody(array $body)
206213 if (empty ($ body )) {
207214 return null ;
208215 }
216+
209217 try {
210- $ encoded = json_encode ($ body );
218+ $ encoded = \ GuzzleHttp \ json_encode ($ body );
211219 } catch (InvalidArgumentException $ e ) {
212220 throw new ApiException ("Error encoding parameters into JSON: ' " . $ e ->getMessage () . "' " );
213221 }
0 commit comments