diff --git a/Cmfcmf/OpenWeatherMap.php b/Cmfcmf/OpenWeatherMap.php index b0ef85c..aabf0e6 100644 --- a/Cmfcmf/OpenWeatherMap.php +++ b/Cmfcmf/OpenWeatherMap.php @@ -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); } @@ -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); } diff --git a/Cmfcmf/OpenWeatherMap/UVIndex.php b/Cmfcmf/OpenWeatherMap/UVIndex.php index 58a2d97..bccf8fa 100644 --- a/Cmfcmf/OpenWeatherMap/UVIndex.php +++ b/Cmfcmf/OpenWeatherMap/UVIndex.php @@ -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; + } } }