Skip to content

Commit

Permalink
Bugfix and a new query method
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardfischer committed Jan 31, 2017
1 parent f4b7aed commit fbafa0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
class API extends Request
{
/**
* Retrieves weather data by a (API internal) "city ID".
* Retrieves weather data by a weather station ID.
* See https://www.wunderground.com/wundermap/ for more information
*
* @param string $id
* @return array
* @throws \ErrorException
*/
public function getByPWSId ($id)
{
Expand All @@ -22,10 +24,11 @@ public function getByPWSId ($id)


/**
* Retrieves weather data by a (API internal) "city ID".
* Retrieves weather data by a airport code.
*
* @param string $code
* @return array
* @throws \ErrorException
*/
public function getByAirportCode ($code)
{
Expand All @@ -34,11 +37,12 @@ public function getByAirportCode ($code)


/**
* Retrieves weather data by given coordinates.
* Retrieves weather data by geo coordinates.
*
* @param float $lat
* @param float $lng
* @return array
* @throws \ErrorException
*/
public function getByCoordinates ($lat, $lng)
{
Expand All @@ -47,14 +51,28 @@ public function getByCoordinates ($lat, $lng)


/**
* Retrieves weather data by given coordinates.
* Retrieves weather data by a given country and city name.
*
* @param string $city
* @param string $country
* @param string $city
* @return array
* @throws \ErrorException
*/
public function getByLocation ($city, $country)
public function getByLocation ($country, $city)
{
return $this->fetch(['query' => $country . '/' . $city])->getResponseArray();
} // function


/**
* Retrieves weather data by a given country and city name.
*
* @param string $zipcode
* @return array
* @throws \ErrorException
*/
public function getByUSZipcode ($zipcode)
{
return $this->fetch(['query' => $zipcode])->getResponseArray();
} // function
} // class
9 changes: 5 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function setQuery ($query)
*
* @param array $parameters
* @return $this
* @throws \ErrorException
*/
public function fetch (array $parameters = [])
{
Expand All @@ -153,21 +154,21 @@ public function fetch (array $parameters = [])
]);

$this->responseJSON = file_get_contents($url);
$this->responseArray = json_decode($this->response, true);
$this->responseArray = json_decode($this->responseJSON, true);

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

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

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

return $this;
Expand Down

0 comments on commit fbafa0d

Please sign in to comment.