Cmfcmf\OpenWeatherMap\WeatherForecast::__construct PHP Method

__construct() public method

Create a new Forecast object.
public __construct ( $xml, string $units, integer $days )
$xml
$units string
$days integer How many days of forecast to receive.
    public function __construct($xml, $units, $days)
    {
        $this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
        $utctz = new \DateTimeZone('UTC');
        $this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
        $this->lastUpdate = new \DateTime($xml->meta->lastupdate);
        $today = new \DateTime();
        $today->setTime(0, 0, 0);
        $counter = 0;
        foreach ($xml->forecast->time as $time) {
            $date = new \DateTime(isset($time['day']) ? $time['day'] : $time['to']);
            if ($date < $today) {
                // Sometimes OpenWeatherMap returns results which aren't real
                // forecasts. The best we can do is to ignore them.
                continue;
            }
            $forecast = new Forecast($time, $units);
            $forecast->city = $this->city;
            $forecast->sun = $this->sun;
            $this->forecasts[] = $forecast;
            $counter++;
            // Make sure to only return the requested number of days.
            if ($days <= 5 && $counter == $days * 8) {
                break;
            } elseif ($days > 5 && $counter == $days) {
                break;
            }
        }
    }