1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\v2; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Stevenmaguire\Yelp\Contract\Http as HttpContract; |
12
|
|
|
use Stevenmaguire\Yelp\Exception\HttpException; |
13
|
|
|
use Stevenmaguire\Yelp\Tool\ConfigurationTrait; |
14
|
|
|
use Stevenmaguire\Yelp\Tool\HttpTrait; |
15
|
|
|
|
16
|
|
|
class Client implements HttpContract |
17
|
|
|
{ |
18
|
|
|
use ConfigurationTrait, |
19
|
|
|
HttpTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Consumer key |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $consumerKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Consumer secret |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $consumerSecret; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Access token |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $token; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Access token secret |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $tokenSecret; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Default search term |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $defaultTerm = 'bar'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Default location |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $defaultLocation = 'Chicago, IL'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Default search limit |
65
|
|
|
* |
66
|
|
|
* @var integer |
67
|
|
|
*/ |
68
|
|
|
protected $searchLimit = 3; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create new client |
72
|
|
|
* |
73
|
|
|
* @param array $options |
74
|
|
|
*/ |
75
|
16 |
|
public function __construct(array $options = array()) |
76
|
|
|
{ |
77
|
|
|
$defaults = array( |
78
|
16 |
|
'consumerKey' => null, |
79
|
8 |
|
'consumerSecret' => null, |
80
|
8 |
|
'token' => null, |
81
|
8 |
|
'tokenSecret' => null, |
82
|
8 |
|
'apiHost' => 'api.yelp.com', |
83
|
|
|
// 'scheme' => 'http' |
84
|
8 |
|
); |
85
|
|
|
|
86
|
16 |
|
$this->parseConfiguration($options, $defaults); |
87
|
|
|
|
88
|
16 |
|
if (!$this->getHttpClient()) { |
89
|
16 |
|
$this->setHttpClient($this->createDefaultHttpClient()); |
90
|
8 |
|
} |
91
|
16 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Creates default http client with appropriate authorization configuration. |
95
|
|
|
* |
96
|
|
|
* @return HttpClient |
97
|
|
|
*/ |
98
|
16 |
|
public function createDefaultHttpClient() |
99
|
|
|
{ |
100
|
16 |
|
$stack = HandlerStack::create(); |
101
|
|
|
|
102
|
16 |
|
$middleware = new Oauth1([ |
103
|
16 |
|
'consumer_key' => $this->consumerKey, |
104
|
16 |
|
'consumer_secret' => $this->consumerSecret, |
105
|
16 |
|
'token' => $this->token, |
106
|
16 |
|
'token_secret' => $this->tokenSecret |
107
|
8 |
|
]); |
108
|
|
|
|
109
|
16 |
|
$stack->push($middleware); |
110
|
|
|
|
111
|
16 |
|
return new HttpClient([ |
112
|
8 |
|
'handler' => $stack |
113
|
8 |
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Fetches a specific business by id. |
118
|
|
|
* |
119
|
|
|
* @param string $businessId |
120
|
|
|
* @param array $parameters |
121
|
|
|
* |
122
|
|
|
* @return stdClass |
123
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
124
|
|
|
*/ |
125
|
4 |
|
public function getBusiness($businessId, $parameters = []) |
126
|
|
|
{ |
127
|
4 |
|
$path = $this->appendParametersToUrl('/v2/business/'.$businessId, $parameters); |
128
|
4 |
|
$request = $this->getRequest('GET', $path); |
129
|
|
|
|
130
|
4 |
|
return $this->processRequest($request); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sends a request instance and returns a response instance. |
135
|
|
|
* |
136
|
|
|
* WARNING: This method does not attempt to catch exceptions caused by HTTP |
137
|
|
|
* errors! It is recommended to wrap this method in a try/catch block. |
138
|
|
|
* |
139
|
|
|
* @param RequestInterface $request |
140
|
|
|
* @return ResponseInterface |
141
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
142
|
|
|
*/ |
143
|
10 |
|
public function getResponse(RequestInterface $request) |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
10 |
|
return $this->getHttpClient()->get((string) $request->getUri(), ['auth' => 'oauth']); |
147
|
2 |
|
} catch (BadResponseException $e) { |
148
|
2 |
|
$exception = new HttpException($e->getMessage()); |
149
|
|
|
|
150
|
2 |
|
throw $exception->setResponseBody((string) $e->getResponse()->getBody()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Provides a hook that handles the response before returning to the consumer. |
156
|
|
|
* |
157
|
|
|
* @param ResponseInterface $response |
158
|
|
|
* |
159
|
|
|
* @return ResponseInterface |
160
|
|
|
*/ |
161
|
8 |
|
protected function handleResponse(ResponseInterface $response) |
162
|
|
|
{ |
163
|
8 |
|
return $response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Fetches results from the Business Search API. |
168
|
|
|
* |
169
|
|
|
* @param array $parameters |
170
|
|
|
* |
171
|
|
|
* @return stdClass |
172
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
173
|
|
|
*/ |
174
|
4 |
|
public function search($parameters = []) |
175
|
|
|
{ |
176
|
4 |
|
$parameters = array_merge([ |
177
|
4 |
|
'term' => $this->defaultTerm, |
178
|
4 |
|
'location' => $this->defaultLocation, |
179
|
4 |
|
'limit' => $this->searchLimit |
180
|
4 |
|
], $parameters); |
181
|
|
|
|
182
|
4 |
|
$path = $this->appendParametersToUrl('/v2/search', $parameters); |
183
|
4 |
|
$request = $this->getRequest('GET', $path); |
184
|
|
|
|
185
|
4 |
|
return $this->processRequest($request); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Fetches results from the Business Search API by Phone. |
190
|
|
|
* |
191
|
|
|
* @param array $parameters |
192
|
|
|
* |
193
|
|
|
* @return stdClass |
194
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
195
|
|
|
*/ |
196
|
2 |
|
public function searchByPhone($parameters = []) |
197
|
|
|
{ |
198
|
2 |
|
$path = $this->appendParametersToUrl('/v2/phone_search', $parameters); |
199
|
2 |
|
$request = $this->getRequest('GET', $path); |
200
|
|
|
|
201
|
2 |
|
return $this->processRequest($request); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set default location |
206
|
|
|
* |
207
|
|
|
* @param string $location |
208
|
|
|
* |
209
|
|
|
* @return Client |
210
|
|
|
*/ |
211
|
2 |
|
public function setDefaultLocation($location) |
212
|
|
|
{ |
213
|
2 |
|
$this->defaultLocation = $location; |
214
|
|
|
|
215
|
2 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set default term |
220
|
|
|
* |
221
|
|
|
* @param string $term |
222
|
|
|
* |
223
|
|
|
* @return Client |
224
|
|
|
*/ |
225
|
2 |
|
public function setDefaultTerm($term) |
226
|
|
|
{ |
227
|
2 |
|
$this->defaultTerm = $term; |
228
|
|
|
|
229
|
2 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set search limit |
234
|
|
|
* |
235
|
|
|
* @param integer $limit |
236
|
|
|
* |
237
|
|
|
* @return Client |
238
|
|
|
*/ |
239
|
2 |
|
public function setSearchLimit($limit) |
240
|
|
|
{ |
241
|
2 |
|
if (is_int($limit)) { |
242
|
2 |
|
$this->searchLimit = $limit; |
243
|
1 |
|
} |
244
|
|
|
|
245
|
2 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|