Skip to content

Commit

Permalink
Handle new UV response format (#157)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Flach <cmfcmf.flach@gmail.com>
  • Loading branch information
mikepsinn and cmfcmf authored Mar 4, 2021
1 parent edb4df4 commit 7027e60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
20 changes: 14 additions & 6 deletions Cmfcmf/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ public function getForecastUVIndex($lat, $lon, $cnt = 8)
{
$answer = $this->getRawUVIndexData('forecast', $lat, $lon, $cnt);
$data = $this->parseJson($answer);

return array_map(function ($entry) {
return new UVIndex($entry);
if (is_object($data)) {
$lat = $data->coord->lat;
$lon = $data->coord->lon;
$data = $data->list;
}
return array_map(function ($entry) use ($lat, $lon) {
return new UVIndex($entry, $lat, $lon);
}, $data);
}

Expand All @@ -335,9 +339,13 @@ public function getHistoricUVIndex($lat, $lon, $start, $end)
{
$answer = $this->getRawUVIndexData('historic', $lat, $lon, null, $start, $end);
$data = $this->parseJson($answer);

return array_map(function ($entry) {
return new UVIndex($entry);
if (is_object($data)) {
$lat = $data->coord->lat;
$lon = $data->coord->lon;
$data = $data->list;
}
return array_map(function ($entry) use ($lat, $lon) {
return new UVIndex($entry, $lat, $lon);
}, $data);
}

Expand Down
22 changes: 16 additions & 6 deletions Cmfcmf/OpenWeatherMap/UVIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ class UVIndex
* Create a new current uv index object.
*
* @param object $data
*
* @param float $lat
* @param float $lon
* @throws \Exception
* @internal
*/
public function __construct($data)
public function __construct($data, $lat = null, $lon = null)
{
$utctz = new \DateTimeZone('UTC');
$this->time = new \DateTime($data->date_iso, $utctz);
$this->location = new Location($data->lat, $data->lon);
$this->uvIndex = (float)$data->value;
if (isset($data->dt)) {
$this->time = \DateTime::createFromFormat('U', $data->dt);
} else {
$utctz = new \DateTimeZone('UTC');
$this->time = new \DateTime($data->date_iso, $utctz);
}
$this->location = new Location($data->lat ?? $lat, $data->lon ?? $lon);
if (isset($data->uvi)) {
$this->uvIndex = (float)$data->uvi;
} else {
$this->uvIndex = (float)$data->value;
}
}
}

0 comments on commit 7027e60

Please sign in to comment.