Skip to content

Commit 02bf37c

Browse files
committed
Fixed number of days returned for forecasts, thanks @goranata, closes #10.
1 parent c290995 commit 02bf37c

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

Cmfcmf/OpenWeatherMap.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
219219
if ($days <= 5) {
220220
$answer = $this->getRawHourlyForecastData($query, $units, $lang, $appid, 'xml');
221221
} else if ($days <= 14) {
222-
$answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml');
222+
$answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml', $days);
223223
} else {
224224
throw new \InvalidArgumentException('Error: forecasts are only available for the next 14 days. $days must be lower than 15.');
225225
}
@@ -237,7 +237,7 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
237237
}
238238
}
239239

240-
return new WeatherForecast($xml, $units);
240+
return new WeatherForecast($xml, $units, $days);
241241
}
242242

243243
/**
@@ -404,8 +404,10 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
404404
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
405405
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
406406
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
407-
* @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
407+
* @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default)
408+
* @param int $cnt How many days of forecast shall be returned? Maximum (and default): 14
408409
*
410+
* @throws \InvalidArgumentException If $cnt is higher than 14.
409411
* @return string Returns false on failure and the fetched data in the format you specified on success.
410412
*
411413
* Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
@@ -436,9 +438,12 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
436438
*
437439
* @api
438440
*/
439-
public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
441+
public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml', $cnt = 14)
440442
{
441-
$url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherDailyForecastUrl);
443+
if ($cnt > 14) {
444+
throw new \InvalidArgumentException('$cnt must be 14 or below!');
445+
}
446+
$url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherDailyForecastUrl) . "&cnt=$cnt";
442447

443448
return $this->cacheOrFetchResult('dailyForecast', $query, $units, $lang, $mode, $url);
444449
}

Cmfcmf/OpenWeatherMap/WeatherForecast.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,28 @@ class WeatherForecast implements \Iterator
5959
*
6060
* @param $xml
6161
* @param string $units
62+
* @param int $days How many days of forecast to receive.
6263
*
6364
* @internal
6465
*/
65-
public function __construct($xml, $units)
66+
public function __construct($xml, $units, $days)
6667
{
6768
$this->city = new City(-1, $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
6869
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);
6970

71+
$counter = 0;
7072
foreach ($xml->forecast->time as $time) {
7173
$forecast = new Forecast($time, $units);
7274
$forecast->city = $this->city;
7375
$this->forecasts[] = $forecast;
76+
77+
$counter++;
78+
// Make sure to only return the requested number of days.
79+
if ($days <= 5 && $counter == $days * 8) {
80+
break;
81+
} else if ($days > 5 && $counter == $days) {
82+
break;
83+
}
7484
}
7585
}
7686

Examples/WeatherForecast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@
6060

6161
foreach ($forecast as $weather) {
6262
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i') . "<br />";
63-
echo $weather->temperature . "<br />";
64-
echo "---<br />";
63+
echo $weather->temperature . "<br />\n";
64+
echo "---<br />\n";
6565
}

0 commit comments

Comments
 (0)