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