Skip to content

Commit 7c4878a

Browse files
committed
Fixed minor bugs.
1 parent 7bf5202 commit 7c4878a

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Cmfcmf/OpenWeatherMap/WeatherHistory.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,24 @@ class WeatherHistory implements \Iterator
5151
*/
5252
private $position = 0;
5353

54-
function __construct($weatherHistory, $query)
54+
public function __construct($weatherHistory, $query)
5555
{
56-
$this->city = new OpenWeatherMap\Util\City($weatherHistory['city_id'], (is_string($query)) ? $query : null, (isset($query['lon'])) ? $query['lon'] : null, (isset($query['lat'])) ? $query['lat'] : null, $weatherHistory['list'][0]['city']['country'], $weatherHistory['list'][0]['city']['population']);
56+
if (isset($weatherHistory['list'][0]['city'])) {
57+
$country = $weatherHistory['list'][0]['city']['country'];
58+
$population = $weatherHistory['list'][0]['city']['population'];
59+
} else {
60+
$country = null;
61+
$population = null;
62+
}
63+
64+
$this->city = new OpenWeatherMap\Util\City($weatherHistory['city_id'], (is_string($query)) ? $query : null, (isset($query['lon'])) ? $query['lon'] : null, (isset($query['lat'])) ? $query['lat'] : null, $country, $population);
5765
$this->calctime = $weatherHistory['calctime'];
5866

5967
foreach ($weatherHistory['list'] as $history) {
60-
$units = array_keys($history['rain']);
68+
if (isset($history['rain'])) {
69+
$units = array_keys($history['rain']);
70+
}
71+
6172
$this->histories[] = new History(
6273
$this->city,
6374
$history['weather'][0],
@@ -69,7 +80,7 @@ function __construct($weatherHistory, $query)
6980
$history['main']['pressure'],
7081
$history['main']['humidity'],
7182
$history['clouds']['all'],
72-
array('val' => $history['rain'][($units[0])], 'unit' => $units[0]),
83+
isset($history['rain']) ? array('val' => $history['rain'][($units[0])], 'unit' => $units[0]) : null,
7384
$history['wind'],
7485
\DateTime::createFromFormat('U', $history['dt'])
7586
);

Examples/WeatherHistory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
3434
$owm = new OpenWeatherMap();
3535

36-
// Example 1: Get hourly weather history between yesterday and today.
37-
$history = $owm->getWeatherHistory('Berlin', new \DateTime('yesterday'), new \DateTime('now'), 'day', $units, $lang);
36+
// Example 1: Get hourly weather history between 2014-01-01 and today.
37+
$history = $owm->getWeatherHistory('Berlin', new \DateTime('2014-01-01'), new \DateTime('now'), 'hour', $units, $lang);
3838

3939
foreach ($history as $weather) {
4040
echo "Average temperature at " . $weather->time->format('d.m.Y H:i') . ": " . $weather->temperature . "\n\r<br />";

0 commit comments

Comments
 (0)