|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\v3; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Stevenmaguire\Yelp\Contract\Http as HttpContract; |
|
8
|
|
|
use Stevenmaguire\Yelp\Tool\ConfigurationTrait; |
|
9
|
|
|
use Stevenmaguire\Yelp\Tool\HttpTrait; |
|
10
|
|
|
|
|
11
|
|
|
class Client implements HttpContract |
|
12
|
|
|
{ |
|
13
|
|
|
use ConfigurationTrait, |
|
14
|
|
|
HttpTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Access token |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $accessToken; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Api key |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $apiKey; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Rate limit |
|
32
|
|
|
* |
|
33
|
|
|
* @var RateLimit|null |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $rateLimit; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates new client |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $options |
|
41
|
|
|
*/ |
|
42
|
26 |
|
public function __construct(array $options = array()) |
|
43
|
|
|
{ |
|
44
|
|
|
$defaults = [ |
|
45
|
26 |
|
'accessToken' => null, |
|
46
|
13 |
|
'apiHost' => 'api.yelp.com', |
|
47
|
13 |
|
'apiKey' => null, |
|
48
|
13 |
|
]; |
|
49
|
|
|
|
|
50
|
26 |
|
$this->parseConfiguration($options, $defaults); |
|
51
|
|
|
|
|
52
|
26 |
|
if (!$this->getHttpClient()) { |
|
53
|
26 |
|
$this->setHttpClient($this->createDefaultHttpClient()); |
|
54
|
13 |
|
} |
|
55
|
26 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Creates default http client with appropriate authorization configuration. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \GuzzleHttp\Client |
|
61
|
|
|
*/ |
|
62
|
26 |
|
public function createDefaultHttpClient() |
|
63
|
|
|
{ |
|
64
|
26 |
|
return new HttpClient([ |
|
65
|
26 |
|
'headers' => $this->getDefaultHeaders() |
|
66
|
13 |
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Fetches results from the Autocomplete API. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $parameters |
|
73
|
|
|
* |
|
74
|
|
|
* @return stdClass |
|
75
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
76
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/autocomplete |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function getAutocompleteResults($parameters = []) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$path = $this->appendParametersToUrl('/v3/autocomplete', $parameters); |
|
81
|
2 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
82
|
|
|
|
|
83
|
2 |
|
return $this->processRequest($request); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the api key, if available, otherwise returns access token. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string|null |
|
90
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going |
|
91
|
|
|
*/ |
|
92
|
26 |
|
private function getBearerToken() |
|
93
|
|
|
{ |
|
94
|
26 |
|
if ($this->apiKey) { |
|
95
|
24 |
|
return $this->apiKey; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
return $this->accessToken; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Fetches a specific business by id. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $businessId |
|
105
|
|
|
* @param array $parameters |
|
106
|
|
|
* |
|
107
|
|
|
* @return stdClass |
|
108
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
109
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business |
|
110
|
|
|
*/ |
|
111
|
4 |
|
public function getBusiness($businessId, $parameters = []) |
|
112
|
|
|
{ |
|
113
|
4 |
|
$path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters); |
|
114
|
4 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
115
|
|
|
|
|
116
|
4 |
|
return $this->processRequest($request); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Fetches reviews for a specific business by id. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $businessId |
|
123
|
|
|
* @param array $parameters |
|
124
|
|
|
* |
|
125
|
|
|
* @return stdClass |
|
126
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
127
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_reviews |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function getBusinessReviews($businessId, $parameters = []) |
|
130
|
|
|
{ |
|
131
|
2 |
|
$path = $this->appendParametersToUrl('/v3/businesses/'.$businessId.'/reviews', $parameters); |
|
132
|
2 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
133
|
|
|
|
|
134
|
2 |
|
return $this->processRequest($request); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Fetches results from the Business Search API. |
|
139
|
|
|
* |
|
140
|
|
|
* @param array $parameters |
|
141
|
|
|
* |
|
142
|
|
|
* @return stdClass |
|
143
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
144
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_search |
|
145
|
|
|
*/ |
|
146
|
2 |
|
public function getBusinessesSearchResults($parameters = []) |
|
147
|
|
|
{ |
|
148
|
2 |
|
$csvParams = ['attributes', 'categories', 'price']; |
|
149
|
|
|
|
|
150
|
2 |
|
$path = $this->appendParametersToUrl('/v3/businesses/search', $parameters, $csvParams); |
|
151
|
2 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
152
|
|
|
|
|
153
|
2 |
|
return $this->processRequest($request); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Fetches results from the Business Search API by Phone. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $phoneNumber |
|
160
|
|
|
* |
|
161
|
|
|
* @return stdClass |
|
162
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
163
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_search_phone |
|
164
|
|
|
*/ |
|
165
|
2 |
|
public function getBusinessesSearchResultsByPhone($phoneNumber) |
|
166
|
|
|
{ |
|
167
|
|
|
$parameters = [ |
|
168
|
1 |
|
'phone' => $phoneNumber |
|
169
|
1 |
|
]; |
|
170
|
|
|
|
|
171
|
2 |
|
$path = $this->appendParametersToUrl('/v3/businesses/search/phone', $parameters); |
|
172
|
2 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
173
|
|
|
|
|
174
|
2 |
|
return $this->processRequest($request); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Builds and returns default headers, specifically including the Authorization |
|
179
|
|
|
* header used for authenticating HTTP requests to Yelp. |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
26 |
|
protected function getDefaultHeaders() |
|
184
|
|
|
{ |
|
185
|
|
|
return [ |
|
186
|
26 |
|
'Authorization' => 'Bearer ' . $this->getBearerToken(), |
|
187
|
13 |
|
]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Returns the latest rate limit metrics, absorbed from the HTTP headers of |
|
192
|
|
|
* the most recent HTTP request to the Yelp v3 service. |
|
193
|
|
|
* |
|
194
|
|
|
* @return RateLimit|null |
|
195
|
|
|
* |
|
196
|
|
|
* @see https://www.yelp.com/developers/documentation/v3/rate_limiting |
|
197
|
|
|
*/ |
|
198
|
2 |
|
public function getRateLimit() |
|
199
|
|
|
{ |
|
200
|
2 |
|
return $this->rateLimit; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Fetches results from the Business Search API by Type. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $type |
|
207
|
|
|
* @param array $parameters |
|
208
|
|
|
* |
|
209
|
|
|
* @return stdClass |
|
210
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
211
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/transactions_search |
|
212
|
|
|
*/ |
|
213
|
2 |
|
public function getTransactionsSearchResultsByType($type, $parameters = []) |
|
214
|
|
|
{ |
|
215
|
2 |
|
$path = $this->appendParametersToUrl('/v3/transactions/'.$type.'/search', $parameters); |
|
216
|
2 |
|
$request = $this->getRequest('GET', $path, $this->getDefaultHeaders()); |
|
217
|
|
|
|
|
218
|
2 |
|
return $this->processRequest($request); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Provides a hook that handles the response before returning to the consumer. |
|
223
|
|
|
* |
|
224
|
|
|
* @param ResponseInterface $response |
|
225
|
|
|
* |
|
226
|
|
|
* @return ResponseInterface |
|
227
|
|
|
*/ |
|
228
|
12 |
|
protected function handleResponse(ResponseInterface $response) |
|
229
|
|
|
{ |
|
230
|
12 |
|
$this->rateLimit = new RateLimit; |
|
231
|
12 |
|
$this->rateLimit->dailyLimit = (integer) $response->getHeaderLine('RateLimit-DailyLimit'); |
|
232
|
12 |
|
$this->rateLimit->remaining = (integer) $response->getHeaderLine('RateLimit-Remaining'); |
|
233
|
12 |
|
$this->rateLimit->resetTime = $response->getHeaderLine('RateLimit-ResetTime'); |
|
234
|
|
|
|
|
235
|
12 |
|
return $response; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|