Completed
Pull Request — master (#78)
by Alex
02:15
created

WeatherForecast   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 2 Features 2
Metric Value
wmc 13
c 7
b 2
f 2
lcom 1
cbo 3
dl 0
loc 118
ccs 0
cts 46
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rewind() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
C __construct() 0 31 8
1
<?php
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
namespace Cmfcmf\OpenWeatherMap;
19
20
use Cmfcmf\OpenWeatherMap;
21
use Cmfcmf\OpenWeatherMap\Util\City;
22
use Cmfcmf\OpenWeatherMap\Util\Sun;
23
24
/**
25
 * Weather class returned by Cmfcmf\OpenWeatherMap->getWeather().
26
 *
27
 * @see Cmfcmf\OpenWeatherMap::getWeather() The function using it.
28
 */
29
class WeatherForecast implements \Iterator
30
{
31
    /**
32
     * A city object.
33
     *
34
     * @var Util\City
35
     */
36
    public $city;
37
38
    /**
39
     * A sun object
40
     *
41
     * @var Util\Sun
42
     */
43
    public $sun;
44
45
    /**
46
     * The time of the last update of this weather data.
47
     *
48
     * @var \DateTime
49
     */
50
    public $lastUpdate;
51
52
    /**
53
     * An array of {@link Forecast} objects.
54
     *
55
     * @var Forecast[]
56
     *
57
     * @see Forecast The Forecast class.
58
     */
59
    private $forecasts;
60
61
    /**
62
     * @internal
63
     */
64
    private $position = 0;
65
66
    /**
67
     * Create a new Forecast object.
68
     *
69
     * @param        $xml
70
     * @param string $units
71
     * @param int    $days How many days of forecast to receive.
72
     *
73
     * @internal
74
     */
75
    public function __construct($xml, $units, $days)
76
    {
77
        $this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
78
        $utctz = new \DateTimeZone('UTC');
79
        $this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
80
        $this->lastUpdate = new \DateTime($xml->meta->lastupdate);
81
82
        $today = new \DateTime();
83
        $today->setTime(0, 0, 0);
84
        $counter = 0;
85
        foreach ($xml->forecast->time as $time) {
86
            $date = new \DateTime(isset($time['day']) ? $time['day'] : $time['to']);
87
            if ($date < $today) {
88
                // Sometimes OpenWeatherMap returns results which aren't real
89
                // forecasts. The best we can do is to ignore them.
90
                continue;
91
            }
92
            $forecast = new Forecast($time, $units);
93
            $forecast->city = $this->city;
94
            $forecast->sun = $this->sun;
95
            $this->forecasts[] = $forecast;
96
97
            $counter++;
98
            // Make sure to only return the requested number of days.
99
            if ($days <= 5 && $counter == $days * 8) {
100
                break;
101
            } elseif ($days > 5 && $counter == $days) {
102
                break;
103
            }
104
        }
105
    }
106
107
    /**
108
     * @internal
109
     */
110
    public function rewind()
111
    {
112
        $this->position = 0;
113
    }
114
115
    /**
116
     * @internal
117
     */
118
    public function current()
119
    {
120
        return $this->forecasts[$this->position];
121
    }
122
123
    /**
124
     * @internal
125
     */
126
    public function key()
127
    {
128
        return $this->position;
129
    }
130
131
    /**
132
     * @internal
133
     */
134
    public function next()
135
    {
136
        ++$this->position;
137
    }
138
139
    /**
140
     * @internal
141
     */
142
    public function valid()
143
    {
144
        return isset($this->forecasts[$this->position]);
145
    }
146
}
147