Skip to content

Commit fbafa0d

Browse files
Bugfix and a new query method
1 parent f4b7aed commit fbafa0d

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/API.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
class API extends Request
1111
{
1212
/**
13-
* Retrieves weather data by a (API internal) "city ID".
13+
* Retrieves weather data by a weather station ID.
14+
* See https://www.wunderground.com/wundermap/ for more information
1415
*
1516
* @param string $id
1617
* @return array
18+
* @throws \ErrorException
1719
*/
1820
public function getByPWSId ($id)
1921
{
@@ -22,10 +24,11 @@ public function getByPWSId ($id)
2224

2325

2426
/**
25-
* Retrieves weather data by a (API internal) "city ID".
27+
* Retrieves weather data by a airport code.
2628
*
2729
* @param string $code
2830
* @return array
31+
* @throws \ErrorException
2932
*/
3033
public function getByAirportCode ($code)
3134
{
@@ -34,11 +37,12 @@ public function getByAirportCode ($code)
3437

3538

3639
/**
37-
* Retrieves weather data by given coordinates.
40+
* Retrieves weather data by geo coordinates.
3841
*
3942
* @param float $lat
4043
* @param float $lng
4144
* @return array
45+
* @throws \ErrorException
4246
*/
4347
public function getByCoordinates ($lat, $lng)
4448
{
@@ -47,14 +51,28 @@ public function getByCoordinates ($lat, $lng)
4751

4852

4953
/**
50-
* Retrieves weather data by given coordinates.
54+
* Retrieves weather data by a given country and city name.
5155
*
52-
* @param string $city
5356
* @param string $country
57+
* @param string $city
5458
* @return array
59+
* @throws \ErrorException
5560
*/
56-
public function getByLocation ($city, $country)
61+
public function getByLocation ($country, $city)
5762
{
5863
return $this->fetch(['query' => $country . '/' . $city])->getResponseArray();
5964
} // function
65+
66+
67+
/**
68+
* Retrieves weather data by a given country and city name.
69+
*
70+
* @param string $zipcode
71+
* @return array
72+
* @throws \ErrorException
73+
*/
74+
public function getByUSZipcode ($zipcode)
75+
{
76+
return $this->fetch(['query' => $zipcode])->getResponseArray();
77+
} // function
6078
} // class

src/Request.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public function setQuery ($query)
127127
*
128128
* @param array $parameters
129129
* @return $this
130+
* @throws \ErrorException
130131
*/
131132
public function fetch (array $parameters = [])
132133
{
@@ -153,21 +154,21 @@ public function fetch (array $parameters = [])
153154
]);
154155

155156
$this->responseJSON = file_get_contents($url);
156-
$this->responseArray = json_decode($this->response, true);
157+
$this->responseArray = json_decode($this->responseJSON, true);
157158

158159
if (!is_array($this->responseArray))
159160
{
160-
// @todo Throw exception.
161+
throw new \ErrorException('The Weather Underground API response returned no valid JSON: ' . $this->responseJSON);
161162
} // if
162163

163164
if (!isset($this->responseArray['response']))
164165
{
165-
// @todo Throw exception.
166+
throw new \ErrorException('The Weather Underground API response is not set or empty: ' . $this->responseJSON);
166167
} // if
167168

168169
if (isset($this->responseArray['response']) && isset($this->responseArray['response']['error']))
169170
{
170-
// @todo Throw exception.
171+
throw new \ErrorException('The Weather Underground API responded with errors: ' . var_export($this->responseArray['response']['error'], true));
171172
} // if
172173

173174
return $this;

0 commit comments

Comments
 (0)