|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PeterColes\Betfair\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Response; |
|
8
|
|
|
use PeterColes\Betfair\Api\Auth; |
|
9
|
|
|
use stdClass; |
|
10
|
|
|
|
|
11
|
|
|
class Client |
|
12
|
|
|
{ |
|
13
|
|
|
protected $guzzleClient; |
|
14
|
|
|
|
|
15
|
|
|
protected $method = 'get'; |
|
16
|
|
|
|
|
17
|
|
|
protected $uri = ''; |
|
18
|
|
|
|
|
19
|
|
|
protected $options = [ ]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* instantiate Guzzle client (unless one is injected). |
|
23
|
|
|
* |
|
24
|
|
|
* @param GuzzleClient $client |
|
25
|
|
|
* @return void |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($client = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->guzzleClient = $client ?: new Client; |
|
30
|
|
|
$this->options[ 'headers' ] = [ 'Accept' => 'application/json' ]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Setter for request method. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $method |
|
37
|
|
|
* @return Client |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setMethod(string $method) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->method = $method; |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Setter for request end point URI. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $endPoint |
|
49
|
|
|
* @return Client |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setEndPoint(string $endPoint) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->uri = $endPoint; |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Setter for request headers. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $header |
|
61
|
|
|
* @return Client |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addHeader(array $header) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->options[ 'headers' ] += $header; |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Setter for authentication headers. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $headers |
|
73
|
|
|
* @return Client |
|
74
|
|
|
*/ |
|
75
|
|
|
public function authHeaders(array $headers = [ ]) |
|
76
|
|
|
{ |
|
77
|
|
|
if (count($headers) == 0) { |
|
78
|
|
|
$headers = [ 'X-Application' => Auth::$appKey, 'X-Authentication' => Auth::$sessionToken ]; |
|
79
|
|
|
} |
|
80
|
|
|
$this->options[ 'headers' ] = array_merge($this->options[ 'headers' ], $headers); |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Setter for request form data. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $formData |
|
88
|
|
|
* @return Client |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setFormData(array $formData) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->options[ 'form_params' ] = $formData; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Setter for request filter(s). mandatory, but can be empty |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $filter |
|
100
|
|
|
* @return Client |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setFilter($filter = null) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->options[ 'json' ][ 'filter' ] = $filter ?: new stdClass; |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Setter for marketId(s). |
|
110
|
|
|
* This param is sometime mandatory, but othertimes breaks the API if an empty value is provided |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $marketIds |
|
113
|
|
|
* @param boolean $mandatory |
|
114
|
|
|
* @return Client |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setMarketIds($marketIds = null, $mandatory = false) |
|
117
|
|
|
{ |
|
118
|
|
|
if ($marketIds || $mandatory) { |
|
119
|
|
|
$this->options[ 'json' ][ 'marketIds' ] = $marketIds ?: new stdClass; |
|
120
|
|
|
} |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Setter for optional betId(s). |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $betIds |
|
128
|
|
|
* @return Client |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setBetIds($betIds = null) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($betIds) { |
|
133
|
|
|
$this->options[ 'json' ][ 'betIds' ] = $betIds ?: new stdClass; |
|
134
|
|
|
} |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Setter for optional market projection, i.e. what market-related data should be returned. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @param array $marketProjection |
|
|
|
|
|
|
143
|
|
|
* @return Client |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setProjection($name, $projection = null) |
|
146
|
|
|
{ |
|
147
|
|
|
if ($projection) { |
|
148
|
|
|
$this->options[ 'json' ][ $name ] = $projection; |
|
149
|
|
|
} |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Setter for optional request sort. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $sort |
|
157
|
|
|
* @return Client |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setSort($sort) |
|
160
|
|
|
{ |
|
161
|
|
|
if ($sort) { |
|
162
|
|
|
$this->options[ 'json' ][ 'sort' ] = $sort; |
|
163
|
|
|
} |
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Setter for mandatory request max results limiter. |
|
169
|
|
|
* |
|
170
|
|
|
* @param integer $maxResults |
|
171
|
|
|
* @return Client |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setMaxresults($maxResults = 100) |
|
174
|
|
|
{ |
|
175
|
|
|
if ($maxResults) { |
|
176
|
|
|
$this->options[ 'json' ][ 'maxResults' ] = $maxResults; |
|
177
|
|
|
} |
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Setter for optional request locale. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $locale |
|
185
|
|
|
* @return Client |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setLocale($locale) |
|
188
|
|
|
{ |
|
189
|
|
|
if ($locale) { |
|
190
|
|
|
$this->options[ 'json' ][ 'locale' ] = $locale; |
|
191
|
|
|
} |
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Setter for mandatory time granularity. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $granularity |
|
199
|
|
|
* @return Client |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setTimeGranularity($granularity) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->options[ 'json' ][ 'granularity' ] = $granularity; |
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Setter for optional currency code. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $currencyCode |
|
211
|
|
|
* @return Client |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setCurrencyCode($currencyCode) |
|
214
|
|
|
{ |
|
215
|
|
|
if ($currencyCode) { |
|
216
|
|
|
$this->options[ 'json' ][ 'currencyCode' ] = $currencyCode; |
|
217
|
|
|
} |
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Setter for optional flag. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $name |
|
225
|
|
|
* @param boolean $flag |
|
226
|
|
|
* @return Client |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setFlag($name, $flag) |
|
229
|
|
|
{ |
|
230
|
|
|
if ($flag) { |
|
231
|
|
|
$this->options[ 'json' ][ $name ] = true; |
|
232
|
|
|
} |
|
233
|
|
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Setter for date range. |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $name |
|
240
|
|
|
* @param array $range |
|
241
|
|
|
* @return Client |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setDateRange($name, $range) |
|
244
|
|
|
{ |
|
245
|
|
|
if ($range) { |
|
|
|
|
|
|
246
|
|
|
$this->options[ 'json' ][ $name ] = $range; |
|
247
|
|
|
} |
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Setter for sort order. |
|
253
|
|
|
* |
|
254
|
|
|
* @param string $by |
|
255
|
|
|
* @param string $direction |
|
256
|
|
|
* @return Client |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setOrder($by, $direction = null) |
|
259
|
|
|
{ |
|
260
|
|
|
if ($by) { |
|
261
|
|
|
$this->options[ 'json' ][ 'orderBy' ] = $by; |
|
262
|
|
|
|
|
263
|
|
|
if ($direction) { |
|
|
|
|
|
|
264
|
|
|
$this->options[ 'json' ][ 'sortDir' ] = $direction; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Setter for record range. |
|
272
|
|
|
* |
|
273
|
|
|
* @param string $from |
|
274
|
|
|
* @param string $count |
|
275
|
|
|
* @return Client |
|
276
|
|
|
*/ |
|
277
|
|
|
public function setRecordRange($from = null, $count = null) |
|
278
|
|
|
{ |
|
279
|
|
|
if ($from) { |
|
|
|
|
|
|
280
|
|
|
$this->options[ 'json' ][ 'fromRecord' ] = $from; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
if ($count) { |
|
|
|
|
|
|
284
|
|
|
$this->options[ 'json' ][ 'recordCount' ] = $count; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $this; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Setter for optional wallet. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $wallet |
|
294
|
|
|
* @return Client |
|
295
|
|
|
*/ |
|
296
|
|
|
public function setwallet($wallet) |
|
297
|
|
|
{ |
|
298
|
|
|
if ($wallet) { |
|
299
|
|
|
$this->options[ 'json' ][ 'wallet' ] = $wallet; |
|
300
|
|
|
} |
|
301
|
|
|
return $this; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Dispatch the request and provide hooks for error handling for the response. |
|
306
|
|
|
* |
|
307
|
|
|
* @return object stdClass |
|
308
|
|
|
*/ |
|
309
|
|
|
public function send() |
|
310
|
|
|
{ |
|
311
|
|
|
$response = $this->guzzleClient->request($this->method, $this->uri, $this->options); |
|
312
|
|
|
|
|
313
|
|
|
$status = $this->getStatus($response); |
|
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
$body = $this->getBody($response); |
|
316
|
|
|
|
|
317
|
|
|
if (is_object($body) && isset($body->status) && $body->status != 'SUCCESS') { |
|
318
|
|
|
$this->handleApiException($body->error); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
return $body; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Get status code from http response. |
|
326
|
|
|
* |
|
327
|
|
|
* @param Response $response |
|
328
|
|
|
* @return integer |
|
329
|
|
|
*/ |
|
330
|
|
|
protected function getStatus(Response $response) |
|
331
|
|
|
{ |
|
332
|
|
|
return (int) $response->getStatusCode(); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Get http response body, cast to json and decode. |
|
337
|
|
|
* |
|
338
|
|
|
* @param Response $response |
|
339
|
|
|
* @return array |
|
340
|
|
|
*/ |
|
341
|
|
|
protected function getBody(Response $response) |
|
342
|
|
|
{ |
|
343
|
|
|
return json_decode((string) $response->getBody()); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Stub for API exception handling. |
|
348
|
|
|
* |
|
349
|
|
|
* @param String $exception |
|
350
|
|
|
* @return void |
|
351
|
|
|
*/ |
|
352
|
|
|
protected function handleApiException($exception) { |
|
353
|
|
|
throw new Exception('API request failure. API Exception Message: '.$exception); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: