Completed
Push — master ( a15fd8...92edee )
by Christian
9s
created

WeatherForecast   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 110
ccs 0
cts 42
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 6
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
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\Util\City;
21
use Cmfcmf\OpenWeatherMap\Util\Sun;
22
23
/**
24
 * Weather class returned by Cmfcmf\OpenWeatherMap->getWeather().
25
 *
26
 * @see \Cmfcmf\OpenWeatherMap::getWeather() The function using it.
27
 */
28
class WeatherForecast implements \Iterator
29
{
30
    /**
31
     * A city object.
32
     *
33
     * @var Util\City
34
     */
35
    public $city;
36
37
    /**
38
     * A sun object
39
     *
40
     * @var Util\Sun
41
     */
42
    public $sun;
43
44
    /**
45
     * The time of the last update of this weather data.
46
     *
47
     * @var \DateTime
48
     */
49
    public $lastUpdate;
50
51
    /**
52
     * An array of {@link Forecast} objects.
53
     *
54
     * @var Forecast[]
55
     *
56
     * @see Forecast The Forecast class.
57
     */
58
    private $forecasts;
59
60
    /**
61
     * @internal
62
     */
63
    private $position = 0;
64
65
    /**
66
     * Create a new Forecast object.
67
     *
68
     * @param        $xml
69
     * @param string $units
70
     * @param int    $days How many days of forecast to receive.
71
     *
72
     * @internal
73
     */
74
    public function __construct($xml, $units, $days)
75
    {
76
        $this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['latitude'], $xml->location->location['longitude'], $xml->location->country);
77
        $utctz = new \DateTimeZone('UTC');
78
        $this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
79
        $this->lastUpdate = new \DateTime($xml->meta->lastupdate, $utctz);
80
81
        $counter = 0;
82
        foreach ($xml->forecast->time as $time) {
83
            $forecast = new Forecast($time, $units);
84
            $forecast->city = $this->city;
85
            $forecast->sun = $this->sun;
86
            $this->forecasts[] = $forecast;
87
88
            $counter++;
89
            // Make sure to only return the requested number of days.
90
            if ($days <= 5 && $counter == $days * 8) {
91
                break;
92
            } elseif ($days > 5 && $counter == $days) {
93
                break;
94
            }
95
        }
96
    }
97
98
    /**
99
     * @internal
100
     */
101
    public function rewind()
102
    {
103
        $this->position = 0;
104
    }
105
106
    /**
107
     * @internal
108
     */
109
    public function current()
110
    {
111
        return $this->forecasts[$this->position];
112
    }
113
114
    /**
115
     * @internal
116
     */
117
    public function key()
118
    {
119
        return $this->position;
120
    }
121
122
    /**
123
     * @internal
124
     */
125
    public function next()
126
    {
127
        ++$this->position;
128
    }
129
130
    /**
131
     * @internal
132
     */
133
    public function valid()
134
    {
135
        return isset($this->forecasts[$this->position]);
136
    }
137
}
138