1 | <?php namespace Stevenmaguire\Yelp; |
||
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 | 18 | public function __construct($configuration = []) |
|
100 | { |
||
101 | 18 | $this->parseConfiguration($configuration) |
|
102 | 18 | ->createHttpClient(); |
|
103 | 18 | } |
|
104 | |||
105 | /** |
||
106 | * Build query string params using defaults |
||
107 | * |
||
108 | * @param array $attributes |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 4 | public function buildQueryParams($attributes = []) |
|
113 | { |
||
114 | $defaults = array( |
||
115 | 4 | 'term' => $this->defaultTerm, |
|
116 | 4 | 'location' => $this->defaultLocation, |
|
117 | 4 | 'limit' => $this->searchLimit |
|
118 | 4 | ); |
|
119 | 4 | $attributes = array_merge($defaults, $attributes); |
|
120 | |||
121 | 4 | return $this->prepareQueryParams($attributes); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Build unsigned url |
||
126 | * |
||
127 | * @param string $host |
||
128 | * @param string $path |
||
129 | * |
||
130 | * @return string Unsigned url |
||
131 | */ |
||
132 | 16 | protected function buildUnsignedUrl($host, $path) |
|
136 | |||
137 | /** |
||
138 | * Builds and sets a preferred http client. |
||
139 | * |
||
140 | * @return Client |
||
141 | */ |
||
142 | 18 | protected function createHttpClient() |
|
161 | |||
162 | /** |
||
163 | * Query the Business API by business id |
||
164 | * |
||
165 | * @param string $businessId The ID of the business to query |
||
166 | * @param array $attributes Optional attributes to include in query string |
||
167 | * |
||
168 | * @return stdClass The JSON response from the request |
||
169 | */ |
||
170 | 10 | public function getBusiness($businessId, $attributes = []) |
|
176 | |||
177 | /** |
||
178 | * Maps legacy configuration keys to updated keys. |
||
179 | * |
||
180 | * @param array $configuration |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | 18 | protected function mapConfiguration(array $configuration) |
|
193 | |||
194 | /** |
||
195 | * Parse configuration using defaults |
||
196 | * |
||
197 | * @param array $configuration |
||
198 | * |
||
199 | * @return client |
||
200 | */ |
||
201 | 18 | protected function parseConfiguration($configuration = []) |
|
217 | |||
218 | /** |
||
219 | * Updates query params array to apply yelp specific formatting rules. |
||
220 | * |
||
221 | * @param array $params |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | protected function prepareQueryParams($params = []) |
||
235 | |||
236 | /** |
||
237 | * Makes a request to the Yelp API and returns the response |
||
238 | * |
||
239 | * @param string $path The path of the APi after the domain |
||
240 | * |
||
241 | * @return stdClass The JSON response from the request |
||
242 | * @throws Exception |
||
243 | */ |
||
244 | 16 | protected function request($path) |
|
258 | |||
259 | /** |
||
260 | * Query the Search API by a search term and location |
||
261 | * |
||
262 | * @param array $attributes Query attributes |
||
263 | * |
||
264 | * @return stdClass The JSON response from the request |
||
265 | */ |
||
266 | 4 | public function search($attributes = []) |
|
273 | |||
274 | /** |
||
275 | * Search for businesses by phone number |
||
276 | * |
||
277 | * @see https://www.yelp.com/developers/documentation/v2/phone_search |
||
278 | * |
||
279 | * @param array $attributes Query attributes |
||
280 | * |
||
281 | * @return stdClass The JSON response from the request |
||
282 | */ |
||
283 | 2 | public function searchByPhone($attributes = []) |
|
289 | |||
290 | /** |
||
291 | * Attempts to set a given value. |
||
292 | * |
||
293 | * @param mixed $value |
||
294 | * @param string $key |
||
295 | * |
||
296 | * @return Client |
||
297 | */ |
||
298 | 18 | protected function setConfig($value, $key) |
|
306 | |||
307 | /** |
||
308 | * Set default location |
||
309 | * |
||
310 | * @param string $location |
||
311 | * |
||
312 | * @return Client |
||
313 | */ |
||
314 | 2 | public function setDefaultLocation($location) |
|
319 | |||
320 | /** |
||
321 | * Set default term |
||
322 | * |
||
323 | * @param string $term |
||
324 | * |
||
325 | * @return Client |
||
326 | */ |
||
327 | 2 | public function setDefaultTerm($term) |
|
332 | |||
333 | /** |
||
334 | * Updates the yelp client's http client to the given http client. Client. |
||
335 | * |
||
336 | * @param HttpClient $client |
||
337 | * |
||
338 | * @return Client |
||
339 | */ |
||
340 | 18 | public function setHttpClient(HttpClient $client) |
|
346 | |||
347 | /** |
||
348 | * Set search limit |
||
349 | * |
||
350 | * @param integer $limit |
||
351 | * |
||
352 | * @return Client |
||
353 | */ |
||
354 | 2 | public function setSearchLimit($limit) |
|
361 | |||
362 | /** |
||
363 | * Retrives the value of a given property from the client. |
||
364 | * |
||
365 | * @param string $property |
||
366 | * |
||
367 | * @return mixed|null |
||
368 | */ |
||
369 | 2 | public function __get($property) |
|
377 | } |
||
378 |