1 | <?php |
||
22 | class Api |
||
23 | { |
||
24 | /** |
||
25 | * Example: http://www.example.com |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $host; |
||
30 | |||
31 | /** |
||
32 | * The API key from your Dandomain admin |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $apiKey; |
||
37 | |||
38 | /** |
||
39 | * This is the HTTP client |
||
40 | * |
||
41 | * @var ClientInterface |
||
42 | */ |
||
43 | protected $client; |
||
44 | |||
45 | /** |
||
46 | * This is the last response |
||
47 | * |
||
48 | * @var Response |
||
49 | */ |
||
50 | protected $response; |
||
51 | |||
52 | /** |
||
53 | * These are the options used for the next request |
||
54 | * After that request, these options are reset |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $options; |
||
59 | |||
60 | /** |
||
61 | * These are default options used for every request |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $defaultOptions; |
||
66 | |||
67 | /** |
||
68 | * These are the resolved options used in the last request |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $lastOptions; |
||
73 | |||
74 | /* |
||
75 | * These are endpoints in the Dandomain API |
||
76 | */ |
||
77 | /** |
||
78 | * @var Endpoint\Customer |
||
79 | */ |
||
80 | protected $customer; |
||
81 | |||
82 | /** |
||
83 | * @var Endpoint\Discount |
||
84 | */ |
||
85 | protected $discount; |
||
86 | |||
87 | /** |
||
88 | * @var Endpoint\Order; |
||
89 | */ |
||
90 | protected $order; |
||
91 | |||
92 | /** |
||
93 | * @var Endpoint\Product; |
||
94 | */ |
||
95 | protected $product; |
||
96 | |||
97 | /** |
||
98 | * @var Endpoint\ProductData; |
||
99 | */ |
||
100 | protected $productData; |
||
101 | |||
102 | /** |
||
103 | * @var Endpoint\ProductTag; |
||
104 | */ |
||
105 | protected $productTag; |
||
106 | |||
107 | /** |
||
108 | * @var Endpoint\RelatedData; |
||
109 | */ |
||
110 | protected $relatedData; |
||
111 | |||
112 | /** |
||
113 | * @var Endpoint\Settings; |
||
114 | */ |
||
115 | protected $settings; |
||
116 | |||
117 | public function __construct(string $host, string $apiKey, array $defaultOptions = []) |
||
129 | |||
130 | /** |
||
131 | * This ensures lazy loading of the endpoint classes |
||
132 | * |
||
133 | * @param string $name |
||
134 | * @return Endpoint\Endpoint |
||
135 | */ |
||
136 | public function __get($name) |
||
154 | |||
155 | /** |
||
156 | * Will always return a JSON result contrary to Dandomains API |
||
157 | * Errors are formatted as described here: http://jsonapi.org/format/#errors |
||
158 | * |
||
159 | * @param string $method |
||
160 | * @param string $uri |
||
161 | * @param array|\stdClass $body The body is sent as JSON |
||
162 | * @param array $options |
||
163 | * @return mixed |
||
164 | */ |
||
165 | public function doRequest(string $method, string $uri, $body = null, array $options = []) |
||
225 | |||
226 | /** |
||
227 | * @return ClientInterface |
||
228 | */ |
||
229 | public function getClient() : ClientInterface |
||
237 | |||
238 | /** |
||
239 | * @param ClientInterface $client |
||
240 | * @return Api |
||
241 | */ |
||
242 | public function setClient(ClientInterface $client) : Api |
||
247 | |||
248 | /** |
||
249 | * Returns the latest response |
||
250 | * |
||
251 | * @return ResponseInterface |
||
252 | */ |
||
253 | public function getResponse() : ResponseInterface |
||
257 | |||
258 | /** |
||
259 | * Sets request options for the next request |
||
260 | * |
||
261 | * @param array $options |
||
262 | * @return Api |
||
263 | */ |
||
264 | public function setOptions(array $options) : Api |
||
269 | |||
270 | /** |
||
271 | * Sets default request options |
||
272 | * |
||
273 | * Notice that these options effectively are merged with the options in Api::resolveOptions |
||
274 | * so if you want to override those, you need to supply all those options |
||
275 | * |
||
276 | * @param array $defaultOptions |
||
277 | * @return Api |
||
278 | */ |
||
279 | public function setDefaultOptions(array $defaultOptions) : Api |
||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | public function getLastOptions(): array |
||
292 | |||
293 | /** |
||
294 | * Helper method to convert a \stdClass into an array |
||
295 | * |
||
296 | * @param $obj |
||
297 | * @return array |
||
298 | */ |
||
299 | protected function objectToArray($obj) : array |
||
307 | |||
308 | protected function resolveOptions(array ...$options) : array |
||
327 | } |
||
328 |