1
|
|
|
<?php namespace Stevenmaguire\Yelp; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Client as HttpClient; |
4
|
|
|
use GuzzleHttp\HandlerStack; |
5
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
|
8
|
|
|
class Client |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* API host url |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $apiHost; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Consumer key |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $consumerKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Consumer secret |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $consumerSecret; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Access token |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $token; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Access token secret |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $tokenSecret; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Default search term |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $defaultTerm = 'bar'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Default location |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $defaultLocation = 'Chicago, IL'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Default search limit |
61
|
|
|
* |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
protected $searchLimit = 3; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Search path |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $searchPath = '/v2/search/'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Business path |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $businessPath = '/v2/business/'; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Phone search path |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
protected $phoneSearchPath = '/v2/phone_search/'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* [$httpClient description] |
89
|
|
|
* |
90
|
|
|
* @var [type] |
91
|
|
|
*/ |
92
|
|
|
protected $httpClient; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create new client |
96
|
|
|
* |
97
|
|
|
* @param array $configuration |
98
|
|
|
*/ |
99
|
16 |
|
public function __construct($configuration = []) |
100
|
|
|
{ |
101
|
16 |
|
$this->parseConfiguration($configuration); |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
$this->consumerKey = $configuration['consumerKey']; |
105
|
|
|
$this->consumerSecret = $configuration['consumerSecret']; |
106
|
|
|
$this->token = $configuration['token']; |
107
|
|
|
$this->tokenSecret = $configuration['tokenSecret']; |
108
|
|
|
$this->apiHost = $configuration['apiHost']; |
109
|
|
|
*/ |
110
|
16 |
|
$this->createHttpClient(); |
111
|
16 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Build query string params using defaults |
115
|
|
|
* |
116
|
|
|
* @param array $attributes |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
4 |
|
public function buildQueryParams($attributes = []) |
121
|
|
|
{ |
122
|
|
|
$defaults = array( |
123
|
4 |
|
'term' => $this->defaultTerm, |
124
|
4 |
|
'location' => $this->defaultLocation, |
125
|
4 |
|
'limit' => $this->searchLimit |
126
|
4 |
|
); |
127
|
4 |
|
$attributes = array_merge($defaults, $attributes); |
128
|
|
|
|
129
|
4 |
|
return http_build_query($attributes); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Build unsigned url |
134
|
|
|
* |
135
|
|
|
* @param string $host |
136
|
|
|
* @param string $path |
137
|
|
|
* |
138
|
|
|
* @return string Unsigned url |
139
|
|
|
*/ |
140
|
14 |
|
protected function buildUnsignedUrl($host, $path) |
141
|
|
|
{ |
142
|
14 |
|
return "http://" . $host . $path; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Builds and sets a preferred http client. |
147
|
|
|
* |
148
|
|
|
* @return Client |
149
|
|
|
*/ |
150
|
16 |
|
protected function createHttpClient() |
151
|
|
|
{ |
152
|
16 |
|
$stack = HandlerStack::create(); |
153
|
|
|
|
154
|
16 |
|
$middleware = new Oauth1([ |
155
|
16 |
|
'consumer_key' => $this->consumerKey, |
156
|
16 |
|
'consumer_secret' => $this->consumerSecret, |
157
|
16 |
|
'token' => $this->token, |
158
|
16 |
|
'token_secret' => $this->tokenSecret |
159
|
16 |
|
]); |
160
|
|
|
|
161
|
16 |
|
$stack->push($middleware); |
162
|
|
|
|
163
|
16 |
|
$client = new HttpClient([ |
164
|
|
|
'handler' => $stack |
165
|
16 |
|
]); |
166
|
|
|
|
167
|
16 |
|
return $this->setHttpClient($client); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Query the Business API by business id |
172
|
|
|
* |
173
|
|
|
* @param string $businessId The ID of the business to query |
174
|
|
|
* |
175
|
|
|
* @return stdClass The JSON response from the request |
176
|
|
|
*/ |
177
|
8 |
|
public function getBusiness($businessId) |
178
|
|
|
{ |
179
|
8 |
|
$businessPath = $this->businessPath . urlencode($businessId); |
180
|
|
|
|
181
|
8 |
|
return $this->request($businessPath); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Maps legacy configuration keys to updated keys. |
186
|
|
|
* |
187
|
|
|
* @param array $configuration |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function mapConfiguration(array $configuration) |
192
|
|
|
{ |
193
|
16 |
|
array_walk($configuration, function ($value, $key) use (&$configuration) { |
194
|
16 |
|
$newKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))); |
195
|
16 |
|
$configuration[$newKey] = $value; |
196
|
16 |
|
}); |
197
|
|
|
|
198
|
16 |
|
return $configuration; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Parse configuration using defaults |
203
|
|
|
* |
204
|
|
|
* @param array $configuration |
205
|
|
|
* |
206
|
|
|
* @return array $configuration |
207
|
|
|
*/ |
208
|
16 |
|
protected function parseConfiguration(&$configuration = []) |
209
|
|
|
{ |
210
|
|
|
$defaults = array( |
211
|
16 |
|
'consumerKey' => null, |
212
|
16 |
|
'consumerSecret' => null, |
213
|
16 |
|
'token' => null, |
214
|
16 |
|
'tokenSecret' => null, |
215
|
|
|
'apiHost' => 'api.yelp.com' |
216
|
16 |
|
); |
217
|
|
|
|
218
|
16 |
|
$configuration = array_merge( |
219
|
16 |
|
$defaults, |
220
|
16 |
|
$this->mapConfiguration($configuration) |
221
|
16 |
|
); |
222
|
|
|
|
223
|
16 |
|
array_walk($configuration, [$this, 'setConfig']); |
224
|
16 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Makes a request to the Yelp API and returns the response |
228
|
|
|
* |
229
|
|
|
* @param string $path The path of the APi after the domain |
230
|
|
|
* |
231
|
|
|
* @return stdClass The JSON response from the request |
232
|
|
|
* @throws Exception |
233
|
|
|
*/ |
234
|
14 |
|
protected function request($path) |
235
|
|
|
{ |
236
|
14 |
|
$url = $this->buildUnsignedUrl($this->apiHost, $path); |
237
|
|
|
|
238
|
|
|
try { |
239
|
14 |
|
$response = $this->httpClient->get($url, ['auth' => 'oauth']); |
240
|
14 |
|
} catch (ClientException $e) { |
241
|
4 |
|
$exception = new Exception($e->getMessage()); |
242
|
|
|
|
243
|
4 |
|
throw $exception->setResponseBody($e->getResponse()->getBody()); |
244
|
|
|
} |
245
|
|
|
|
246
|
10 |
|
return json_decode($response->getBody()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Query the Search API by a search term and location |
251
|
|
|
* |
252
|
|
|
* @param array $attributes Query attributes |
253
|
|
|
* |
254
|
|
|
* @return stdClass The JSON response from the request |
255
|
|
|
*/ |
256
|
4 |
|
public function search($attributes = []) |
257
|
|
|
{ |
258
|
4 |
|
$query_string = $this->buildQueryParams($attributes); |
259
|
4 |
|
$searchPath = $this->searchPath . "?" . $query_string; |
260
|
|
|
|
261
|
4 |
|
return $this->request($searchPath); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Search for businesses by phone number |
266
|
|
|
* |
267
|
|
|
* @see https://www.yelp.com/developers/documentation/v2/phone_search |
268
|
|
|
* |
269
|
|
|
* @param array $attributes Query attributes |
270
|
|
|
* |
271
|
|
|
* @return stdClass The JSON response from the request |
272
|
|
|
*/ |
273
|
2 |
|
public function searchByPhone($attributes = []) |
274
|
|
|
{ |
275
|
2 |
|
$searchPath = $this->phoneSearchPath . "?" . http_build_query($attributes); |
276
|
|
|
|
277
|
2 |
|
return $this->request($searchPath); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Attempts to set a given value. |
282
|
|
|
* |
283
|
|
|
* @param mixed $value |
284
|
|
|
* @param string $key |
285
|
|
|
* |
286
|
|
|
* @return Client |
287
|
|
|
*/ |
288
|
16 |
|
protected function setConfig($value, $key) |
289
|
|
|
{ |
290
|
16 |
|
if (property_exists($this, $key)) { |
291
|
16 |
|
$this->$key = $value; |
292
|
16 |
|
} |
293
|
|
|
|
294
|
16 |
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Set default location |
299
|
|
|
* |
300
|
|
|
* @param string $location |
301
|
|
|
* |
302
|
|
|
* @return Client |
303
|
|
|
*/ |
304
|
2 |
|
public function setDefaultLocation($location) |
305
|
|
|
{ |
306
|
2 |
|
$this->defaultLocation = $location; |
307
|
2 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set default term |
312
|
|
|
* |
313
|
|
|
* @param string $term |
314
|
|
|
* |
315
|
|
|
* @return Client |
316
|
|
|
*/ |
317
|
2 |
|
public function setDefaultTerm($term) |
318
|
|
|
{ |
319
|
2 |
|
$this->defaultTerm = $term; |
320
|
2 |
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Updates the yelp client's http client to the given http client. Client. |
325
|
|
|
* |
326
|
|
|
* @param HttpClient $client |
327
|
|
|
* |
328
|
|
|
* @return Client |
329
|
|
|
*/ |
330
|
16 |
|
public function setHttpClient(HttpClient $client) |
331
|
|
|
{ |
332
|
16 |
|
$this->httpClient = $client; |
333
|
|
|
|
334
|
16 |
|
return $this; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Set search limit |
339
|
|
|
* |
340
|
|
|
* @param integer $limit |
341
|
|
|
* |
342
|
|
|
* @return Client |
343
|
|
|
*/ |
344
|
2 |
|
public function setSearchLimit($limit) |
345
|
|
|
{ |
346
|
2 |
|
if (is_int($limit)) { |
347
|
2 |
|
$this->searchLimit = $limit; |
348
|
2 |
|
} |
349
|
2 |
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Retrives the value of a given property from the client. |
354
|
|
|
* |
355
|
|
|
* @param string $property |
356
|
|
|
* |
357
|
|
|
* @return mixed|null |
358
|
|
|
*/ |
359
|
2 |
|
public function __get($property) |
360
|
|
|
{ |
361
|
2 |
|
if (property_exists($this, $property)) { |
362
|
2 |
|
return $this->$property; |
363
|
|
|
} |
364
|
|
|
|
365
|
2 |
|
return null; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|