1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\Tool; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
8
|
|
|
use GuzzleHttp\Psr7\Request; |
9
|
|
|
use GuzzleHttp\Psr7\Uri; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Stevenmaguire\Yelp\Exception\ClientConfigurationException; |
12
|
|
|
use Stevenmaguire\Yelp\Exception\HttpException; |
13
|
|
|
|
14
|
|
|
trait HttpTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* API host url |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $apiHost; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HTTP client |
25
|
|
|
* |
26
|
|
|
* @var \GuzzleHttp\Client |
27
|
|
|
*/ |
28
|
|
|
protected $httpClient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* HTTP scheme |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $scheme; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Prepares and appends parameters, if provided, to the given url. |
39
|
|
|
* |
40
|
|
|
* @param string $url |
41
|
|
|
* @param array $parameters |
42
|
|
|
* @param string[] $options |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
protected function appendParametersToUrl($url, array $parameters = array(), array $options = array()) |
47
|
|
|
{ |
48
|
|
|
$url = rtrim($url, '?'); |
49
|
|
|
$queryString = $this->prepareQueryParams($parameters, $options); |
50
|
|
|
|
51
|
|
|
if ($queryString) { |
52
|
|
|
$uri = new Uri($url); |
53
|
|
|
$existingQuery = $uri->getQuery(); |
54
|
|
|
$updatedQuery = empty($existingQuery) ? $queryString : $existingQuery . '&' . $queryString; |
55
|
|
|
$url = (string) $uri->withQuery($updatedQuery); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $url; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Flattens given array into comma separated value. |
63
|
|
|
* |
64
|
|
|
* @param mixed $input |
65
|
|
|
* |
66
|
|
|
* @return string|mixed |
67
|
|
|
*/ |
68
|
|
|
private function arrayToCsv($input) |
69
|
|
|
{ |
70
|
|
|
if (is_array($input)) { |
71
|
|
|
$input = implode(',', $input); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $input; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Coerces given value into boolean and returns string representation |
79
|
|
|
* |
80
|
|
|
* @param boolean $value |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function getBoolString($value) |
85
|
|
|
{ |
86
|
|
|
return (bool) $value ? 'true' : 'false'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the yelp client's http client to the given http client. Client. |
91
|
|
|
* |
92
|
|
|
* @return GuzzleHttp\Client|null |
93
|
|
|
*/ |
94
|
|
|
public function getHttpClient() |
95
|
|
|
{ |
96
|
|
|
return $this->httpClient; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Creates a PSR-7 Request instance. |
101
|
|
|
* |
102
|
|
|
* @param null|string $method HTTP method for the request. |
103
|
|
|
* @param null|string $uri URI for the request. |
104
|
|
|
* @param array $headers Headers for the message. |
105
|
|
|
* @param string|resource|StreamInterface $body Message body. |
106
|
|
|
* @param string $version HTTP protocol version. |
107
|
|
|
* |
108
|
|
|
* @return Request |
109
|
|
|
*/ |
110
|
|
|
public function getRequest( |
111
|
|
|
$method, |
112
|
|
|
$uri, |
113
|
|
|
array $headers = [], |
114
|
|
|
$body = null, |
115
|
|
|
$version = '1.1' |
116
|
|
|
) { |
117
|
|
|
$uri = new Uri($uri); |
118
|
|
|
|
119
|
|
|
if (!$uri->getHost()) { |
120
|
|
|
$uri = $uri->withHost($this->apiHost); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!$uri->getScheme()) { |
124
|
|
|
$uri = $uri->withScheme(($this->scheme ?: 'https')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new Request($method, $uri, $headers, $body, $version); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sends a request instance and returns a response instance. |
132
|
|
|
* |
133
|
|
|
* WARNING: This method does not attempt to catch exceptions caused by HTTP |
134
|
|
|
* errors! It is recommended to wrap this method in a try/catch block. |
135
|
|
|
* |
136
|
|
|
* @param RequestInterface $request |
137
|
|
|
* @return ResponseInterface |
138
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
139
|
|
|
*/ |
140
|
|
|
public function getResponse(RequestInterface $request) |
141
|
|
|
{ |
142
|
|
|
try { |
143
|
|
|
return $this->getHttpClient()->send($request); |
144
|
|
|
} catch (BadResponseException $e) { |
145
|
|
|
$exception = new HttpException($e->getMessage()); |
146
|
|
|
|
147
|
|
|
throw $exception->setResponseBody((string) $e->getResponse()->getBody()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Updates query params array to apply yelp specific formatting rules. |
153
|
|
|
* |
154
|
|
|
* @param array $params |
155
|
|
|
* @param string[] $csvParams |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
protected function prepareQueryParams($params = [], $csvParams = []) |
160
|
|
|
{ |
161
|
|
|
array_walk($params, function ($value, $key) use (&$params, $csvParams) { |
162
|
|
|
if (is_bool($value)) { |
163
|
|
|
$params[$key] = $this->getBoolString($value); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (in_array($key, $csvParams)) { |
167
|
|
|
$params[$key] = $this->arrayToCsv($value); |
168
|
|
|
} |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
return http_build_query($params); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Makes a request to the Yelp API and returns the response |
176
|
|
|
* |
177
|
|
|
* @param RequestInterface $request |
178
|
|
|
* |
179
|
|
|
* @return stdClass The JSON response from the request |
180
|
|
|
* @throws Stevenmaguire\Yelp\Exception\ClientConfigurationException |
181
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
182
|
|
|
*/ |
183
|
|
|
protected function processRequest(RequestInterface $request) |
184
|
|
|
{ |
185
|
|
|
$response = $this->getResponse($request); |
186
|
|
|
|
187
|
|
|
return json_decode($response->getBody()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Updates the yelp client's http client to the given http client. Client. |
192
|
|
|
* |
193
|
|
|
* @param HttpClient $client |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function setHttpClient(HttpClient $client) |
198
|
|
|
{ |
199
|
|
|
$this->httpClient = $client; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|