Skip to content

Commit 7027e60

Browse files
mikepsinncmfcmf
andauthored
Handle new UV response format (#157)
Co-authored-by: Christian Flach <cmfcmf.flach@gmail.com>
1 parent edb4df4 commit 7027e60

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

Cmfcmf/OpenWeatherMap.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,13 @@ public function getForecastUVIndex($lat, $lon, $cnt = 8)
310310
{
311311
$answer = $this->getRawUVIndexData('forecast', $lat, $lon, $cnt);
312312
$data = $this->parseJson($answer);
313-
314-
return array_map(function ($entry) {
315-
return new UVIndex($entry);
313+
if (is_object($data)) {
314+
$lat = $data->coord->lat;
315+
$lon = $data->coord->lon;
316+
$data = $data->list;
317+
}
318+
return array_map(function ($entry) use ($lat, $lon) {
319+
return new UVIndex($entry, $lat, $lon);
316320
}, $data);
317321
}
318322

@@ -335,9 +339,13 @@ public function getHistoricUVIndex($lat, $lon, $start, $end)
335339
{
336340
$answer = $this->getRawUVIndexData('historic', $lat, $lon, null, $start, $end);
337341
$data = $this->parseJson($answer);
338-
339-
return array_map(function ($entry) {
340-
return new UVIndex($entry);
342+
if (is_object($data)) {
343+
$lat = $data->coord->lat;
344+
$lon = $data->coord->lon;
345+
$data = $data->list;
346+
}
347+
return array_map(function ($entry) use ($lat, $lon) {
348+
return new UVIndex($entry, $lat, $lon);
341349
}, $data);
342350
}
343351

Cmfcmf/OpenWeatherMap/UVIndex.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,24 @@ class UVIndex
4444
* Create a new current uv index object.
4545
*
4646
* @param object $data
47-
*
47+
* @param float $lat
48+
* @param float $lon
49+
* @throws \Exception
4850
* @internal
4951
*/
50-
public function __construct($data)
52+
public function __construct($data, $lat = null, $lon = null)
5153
{
52-
$utctz = new \DateTimeZone('UTC');
53-
$this->time = new \DateTime($data->date_iso, $utctz);
54-
$this->location = new Location($data->lat, $data->lon);
55-
$this->uvIndex = (float)$data->value;
54+
if (isset($data->dt)) {
55+
$this->time = \DateTime::createFromFormat('U', $data->dt);
56+
} else {
57+
$utctz = new \DateTimeZone('UTC');
58+
$this->time = new \DateTime($data->date_iso, $utctz);
59+
}
60+
$this->location = new Location($data->lat ?? $lat, $data->lon ?? $lon);
61+
if (isset($data->uvi)) {
62+
$this->uvIndex = (float)$data->uvi;
63+
} else {
64+
$this->uvIndex = (float)$data->value;
65+
}
5666
}
5767
}

0 commit comments

Comments
 (0)