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