From be18e16a1d7c5c74f7a4718df5ff430739d6cedd Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Sun, 21 Feb 2021 12:33:28 -0600 Subject: [PATCH 1/3] Handle new UV response format --- .gitignore | 1 + Cmfcmf/OpenWeatherMap.php | 20 ++++++++++++++------ Cmfcmf/OpenWeatherMap/UVIndex.php | 20 +++++++++++++++----- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index bd72e96..5edccfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor /.vagrant/ /ubuntu-xenial-16.04-cloudimg-console.log +/.idea/ diff --git a/Cmfcmf/OpenWeatherMap.php b/Cmfcmf/OpenWeatherMap.php index b0ef85c..2bc4f00 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..bc0b643 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 { + $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; + } } } From 3a5be2c7768add005e90b77bebd97d38b0b1f660 Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Sun, 21 Feb 2021 12:36:17 -0600 Subject: [PATCH 2/3] Complying with StyleCI demands --- Cmfcmf/OpenWeatherMap.php | 4 ++-- Cmfcmf/OpenWeatherMap/UVIndex.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cmfcmf/OpenWeatherMap.php b/Cmfcmf/OpenWeatherMap.php index 2bc4f00..aabf0e6 100644 --- a/Cmfcmf/OpenWeatherMap.php +++ b/Cmfcmf/OpenWeatherMap.php @@ -310,7 +310,7 @@ public function getForecastUVIndex($lat, $lon, $cnt = 8) { $answer = $this->getRawUVIndexData('forecast', $lat, $lon, $cnt); $data = $this->parseJson($answer); - if(is_object($data)){ + if (is_object($data)) { $lat = $data->coord->lat; $lon = $data->coord->lon; $data = $data->list; @@ -339,7 +339,7 @@ public function getHistoricUVIndex($lat, $lon, $start, $end) { $answer = $this->getRawUVIndexData('historic', $lat, $lon, null, $start, $end); $data = $this->parseJson($answer); - if(is_object($data)){ + if (is_object($data)) { $lat = $data->coord->lat; $lon = $data->coord->lon; $data = $data->list; diff --git a/Cmfcmf/OpenWeatherMap/UVIndex.php b/Cmfcmf/OpenWeatherMap/UVIndex.php index bc0b643..ea430f4 100644 --- a/Cmfcmf/OpenWeatherMap/UVIndex.php +++ b/Cmfcmf/OpenWeatherMap/UVIndex.php @@ -52,13 +52,13 @@ class UVIndex public function __construct($data, $lat = null, $lon = null) { $utctz = new \DateTimeZone('UTC'); - if(isset($data->dt)){ - $this->time = \DateTime::createFromFormat( 'U', $data->dt ); + if (isset($data->dt)) { + $this->time = \DateTime::createFromFormat('U', $data->dt); } else { $this->time = new \DateTime($data->date_iso, $utctz); } $this->location = new Location($data->lat ?? $lat, $data->lon ?? $lon); - if(isset($data->uvi)){ + if (isset($data->uvi)) { $this->uvIndex = (float)$data->uvi; } else { $this->uvIndex = (float)$data->value; From 12c2e7db05cadea264f52796947bc89710518e90 Mon Sep 17 00:00:00 2001 From: Christian Flach Date: Thu, 4 Mar 2021 17:18:29 +0100 Subject: [PATCH 3/3] Minor fixes --- .gitignore | 1 - Cmfcmf/OpenWeatherMap/UVIndex.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5edccfd..bd72e96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /vendor /.vagrant/ /ubuntu-xenial-16.04-cloudimg-console.log -/.idea/ diff --git a/Cmfcmf/OpenWeatherMap/UVIndex.php b/Cmfcmf/OpenWeatherMap/UVIndex.php index ea430f4..bccf8fa 100644 --- a/Cmfcmf/OpenWeatherMap/UVIndex.php +++ b/Cmfcmf/OpenWeatherMap/UVIndex.php @@ -51,10 +51,10 @@ class UVIndex */ public function __construct($data, $lat = null, $lon = null) { - $utctz = new \DateTimeZone('UTC'); 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);