1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jérémy Marodon <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Th3Mouk\YahooWeatherAPI; |
11
|
|
|
|
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use Th3Mouk\YahooWeatherAPI\Query\Query; |
14
|
|
|
|
15
|
|
|
class YahooWeatherAPI implements YahooWeatherAPIInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Client Goutte client |
19
|
|
|
*/ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array last response from API |
24
|
|
|
*/ |
25
|
|
|
protected $lastResponse; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string last woeid explicitly called |
29
|
|
|
*/ |
30
|
|
|
protected $woeid; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string last city name explicitly called |
34
|
|
|
*/ |
35
|
|
|
protected $city; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string the last url called |
39
|
|
|
*/ |
40
|
|
|
protected $yql; |
41
|
|
|
|
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->client = new Client(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function callApiWoeid($woeid = null, $unit = 'c') |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
if ($woeid === null && $this->woeid === null) { |
53
|
|
|
throw new \Exception('Please provide a woeid code', 400); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($woeid !== null) { |
57
|
|
|
$this->woeid = $woeid; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->yql = Query::URL_BASE.urlencode(sprintf(Query::WOEID_QUERY, $this->woeid, $unit)); |
61
|
|
|
|
62
|
|
|
return $this->callApi(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function callApiCityName($city = null, $unit = 'c') |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($city === null && $this->city === null) { |
71
|
|
|
throw new \Exception('Please provide a city\'s name', 400); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($city !== null) { |
75
|
|
|
$this->city = $city; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->yql = Query::URL_BASE.urlencode(sprintf(Query::CITY_NAME_QUERY, $this->city, $unit)); |
79
|
|
|
|
80
|
|
|
return $this->callApi(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function callApi($yql = null) |
87
|
|
|
{ |
88
|
|
|
if ($yql === null && empty($this->yql)) { |
89
|
|
|
throw new \Exception('Please provide a YQL request', 400); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($yql !== null) { |
93
|
|
|
$this->yql = $yql; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
$headers = array( |
98
|
|
|
'verify' => false, |
99
|
|
|
); |
100
|
|
|
$response = $this->client->request('GET', $this->yql, $headers); |
101
|
|
|
$response = json_decode($response->getBody(), true); |
102
|
|
|
if (!isset($response['query']['results']['channel']['item']['condition'])) { |
103
|
|
|
$this->lastResponse = false; |
|
|
|
|
104
|
|
|
} else { |
105
|
|
|
$this->lastResponse = $response['query']['results']['channel']; |
106
|
|
|
} |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
throw new \Exception("Something error happen, please check your request url whether it's correct.", 400); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->lastResponse; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get lastResponse. |
116
|
|
|
* |
117
|
|
|
* @param bool $toJson choose format for the return value (array or json) |
118
|
|
|
* |
119
|
|
|
* @return array|string |
120
|
|
|
*/ |
121
|
|
|
public function getLastResponse($toJson = false) |
122
|
|
|
{ |
123
|
|
|
return ($toJson) ? json_encode($this->lastResponse) : $this->lastResponse; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set lastResponse. |
128
|
|
|
* |
129
|
|
|
* @param array $data data from json_encode |
130
|
|
|
*/ |
131
|
|
|
public function setLastResponse($data) |
132
|
|
|
{ |
133
|
|
|
$this->lastResponse = $data; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get current temperature. |
138
|
|
|
* |
139
|
|
|
* @param bool $withUnit return or not the unit |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getTemperature($withUnit = false) |
144
|
|
|
{ |
145
|
|
|
if (!$this->lastResponse || !isset($this->lastResponse['item']['condition']['temp'])) { |
|
|
|
|
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
$return = $this->lastResponse['item']['condition']['temp']; |
149
|
|
|
if ($withUnit) { |
150
|
|
|
$return .= ' '.$this->lastResponse['units']['temperature']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $return; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get Location. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function getLocation() |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
if (!$this->lastResponse || !isset($this->lastResponse['location']['city'])) { |
|
|
|
|
164
|
|
|
return ''; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->lastResponse['location']['city']; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* get Forecast. |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function getForecast() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
if (!$this->lastResponse || !isset($this->lastResponse['item']['forecast'])) { |
|
|
|
|
178
|
|
|
return array(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->lastResponse['item']['forecast']; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* get Wind. |
186
|
|
|
* |
187
|
|
|
* @param bool $withUnit return or not the unit |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getWind($withUnit = false) |
192
|
|
|
{ |
193
|
|
|
if (!$this->lastResponse || !isset($this->lastResponse['wind']['speed'])) { |
|
|
|
|
194
|
|
|
return array(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$response = array( |
198
|
|
|
'chill' => $this->lastResponse['wind']['chill'], |
199
|
|
|
'direction' => $this->lastResponse['wind']['direction'], |
200
|
|
|
'speed' => $this->lastResponse['wind']['speed'], |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
if ($withUnit) { |
204
|
|
|
$response['speed'] .= ' '.$this->lastResponse['units']['speed']; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $response; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param GoutteClient $client |
212
|
|
|
*/ |
213
|
|
|
public function setClient(Client $client) |
214
|
|
|
{ |
215
|
|
|
$this->client = $client; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function getWoeid() |
222
|
|
|
{ |
223
|
|
|
return $this->woeid; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getCity() |
230
|
|
|
{ |
231
|
|
|
return $this->city; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function getYql() |
238
|
|
|
{ |
239
|
|
|
return $this->yql; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.