1 | <?php |
||
21 | abstract class AbstractApi extends VersionableRequestMaker |
||
22 | { |
||
23 | /** |
||
24 | * The HTTP client. |
||
25 | * |
||
26 | * @author Andrea Marco Sartori |
||
27 | * @var Cerbero\FluentApi\Clients\ClientInterface |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * The resource inflector. |
||
33 | * |
||
34 | * @author Andrea Marco Sartori |
||
35 | * @var Cerbero\FluentApi\Inflectors\ResourceInflectorInterface |
||
36 | */ |
||
37 | protected $inflector; |
||
38 | |||
39 | /** |
||
40 | * The latest resource invoked. |
||
41 | * |
||
42 | * @var Cerbero\FluentApi\AbstractResource|null |
||
43 | */ |
||
44 | protected $resource; |
||
45 | |||
46 | /** |
||
47 | * Set the dependencies. |
||
48 | * |
||
49 | * @author Andrea Marco Sartori |
||
50 | * @param string|null $version |
||
51 | * @param Cerbero\FluentApi\Clients\ClientInterface|null $client |
||
52 | * @param Cerbero\FluentApi\Inflectors\ResourceInflectorInterface|null $inflector |
||
53 | * @return void |
||
|
|||
54 | */ |
||
55 | public function __construct( |
||
65 | |||
66 | /** |
||
67 | * Dynamically resolve resources. |
||
68 | * |
||
69 | * @author Andrea Marco Sartori |
||
70 | * @param string $name |
||
71 | * @param array $parameters |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function __call($name, array $parameters) |
||
84 | |||
85 | /** |
||
86 | * Retrieve the updated request from the previously resolved resource. |
||
87 | * |
||
88 | * @param string $method |
||
89 | * @param array $parameters |
||
90 | * @return Cerbero\FluentApi\Requests\Request |
||
91 | */ |
||
92 | private function getRequestFromPreviousResource($method, array $parameters) |
||
98 | |||
99 | /** |
||
100 | * Retrieve the request from the newly resolved resource. |
||
101 | * |
||
102 | * @param string $resource |
||
103 | * @param array $parameters |
||
104 | * @return Cerbero\FluentApi\Requests\Request |
||
105 | */ |
||
106 | private function getRequestByResolvingResource($resource, array $parameters) |
||
114 | |||
115 | /** |
||
116 | * Retrieve the base URL. |
||
117 | * |
||
118 | * @author Andrea Marco Sartori |
||
119 | * @return string |
||
120 | */ |
||
121 | abstract public function getUrl(); |
||
122 | |||
123 | /** |
||
124 | * Retrieve the resource to call by inflecting the given name. |
||
125 | * |
||
126 | * @author Andrea Marco Sartori |
||
127 | * @param string $name |
||
128 | * @return Cerbero\FluentApi\Resources\ResourceInterface |
||
129 | */ |
||
130 | protected function inflectResource($name) |
||
144 | |||
145 | /** |
||
146 | * Retrieve the resource inflector. |
||
147 | * |
||
148 | * @return Cerbero\FluentApi\Inflectors\ResourceInflectorInterface |
||
149 | */ |
||
150 | public function getInflector() |
||
154 | |||
155 | /** |
||
156 | * Retrieve the default inflector. |
||
157 | * |
||
158 | * @author Andrea Marco Sartori |
||
159 | * @return Cerbero\FluentApi\Inflectors\ResourceInflectorInterface |
||
160 | */ |
||
161 | protected function defaultInflector() |
||
165 | |||
166 | /** |
||
167 | * Set the resource inflector. |
||
168 | * |
||
169 | * @author Andrea Marco Sartori |
||
170 | * @param Cerbero\FluentApi\Inflectors\ResourceInflectorInterface $inflector |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setInflector(ResourceInflectorInterface $inflector) |
||
179 | |||
180 | /** |
||
181 | * Perform a synchronous call to the eventual endpoint. |
||
182 | * |
||
183 | * @return Psr\Http\Message\ResponseInterface |
||
184 | */ |
||
185 | public function call() |
||
189 | |||
190 | /** |
||
191 | * Perform an HTTP call by using the client. |
||
192 | * |
||
193 | * @param Closure|null $success |
||
194 | * @param Closure|null $failure |
||
195 | * @return mixed |
||
196 | */ |
||
197 | private function performCall(Closure $success = null, Closure $failure = null) |
||
211 | |||
212 | /** |
||
213 | * Perform an asynchronous call to the eventual endpoint. |
||
214 | * |
||
215 | * @param Closure $success |
||
216 | * @param Closure|null $failure |
||
217 | * @return mixed |
||
218 | */ |
||
219 | public function then(Closure $success, Closure $failure = null) |
||
227 | |||
228 | /** |
||
229 | * Retrieve the body of the response as a JSON object. |
||
230 | * |
||
231 | * @return \StdClass |
||
232 | */ |
||
233 | public function toJson() |
||
237 | |||
238 | /** |
||
239 | * Retrieve the decoded response body. |
||
240 | * |
||
241 | * @param boolean $toArray |
||
242 | * @return \StdClass|array |
||
243 | */ |
||
244 | private function decodeBody($toArray = false) |
||
250 | |||
251 | /** |
||
252 | * Retrieve the body of the response as an array. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | public function toArray() |
||
260 | |||
261 | /** |
||
262 | * Retrieve the eventual API URL. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function toUrl() |
||
274 | |||
275 | /** |
||
276 | * Retrieve the query string parameters by the given options. |
||
277 | * |
||
278 | * @param array $options |
||
279 | * @return string |
||
280 | */ |
||
281 | private function getQueryByOptions(array $options) |
||
287 | |||
288 | /** |
||
289 | * Retrieve the request to pass through resources. |
||
290 | * |
||
291 | * @return Cerbero\FluentApi\Requests\Request |
||
292 | */ |
||
293 | public function getRequest() |
||
297 | |||
298 | /** |
||
299 | * Set the HTTP client. |
||
300 | * |
||
301 | * @author Andrea Marco Sartori |
||
302 | * @param Cerbero\FluentApi\Clients\ClientInterface $client |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setClient(ClientInterface $client) |
||
311 | |||
312 | /** |
||
313 | * Retrieve the HTTP client. |
||
314 | * |
||
315 | * @author Andrea Marco Sartori |
||
316 | * @return Cerbero\FluentApi\Clients\ClientInterface |
||
317 | */ |
||
318 | public function getClient() |
||
322 | |||
323 | /** |
||
324 | * Retrieve the default HTTP client to use if none is provided. |
||
325 | * |
||
326 | * @author Andrea Marco Sartori |
||
327 | * @return Cerbero\FluentApi\Clients\ClientInterface |
||
328 | */ |
||
329 | protected function defaultClient() |
||
333 | } |
||
334 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.