|
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
|
3 |
|
public function __construct($xml, $units, $days) |
|
76
|
|
|
{ |
|
77
|
3 |
|
$this->city = new City(-1, $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country); |
|
78
|
3 |
|
$this->sun = new Sun(new \DateTime($xml->sun['rise']), new \DateTime($xml->sun['set'])); |
|
79
|
3 |
|
$this->lastUpdate = new \DateTime($xml->meta->lastupdate); |
|
80
|
|
|
|
|
81
|
3 |
|
$today = new \DateTime(); |
|
82
|
3 |
|
$today->setTime(0, 0, 0); |
|
83
|
3 |
|
$counter = 0; |
|
84
|
3 |
|
foreach ($xml->forecast->time as $time) { |
|
85
|
3 |
|
$date = new \DateTime(isset($time['day']) ? $time['day'] : $time['to']); |
|
86
|
3 |
|
if ($date < $today) { |
|
87
|
|
|
// Sometimes OpenWeatherMap returns results which aren't real |
|
88
|
|
|
// forecasts. The best we can do is to ignore them. |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
3 |
|
$forecast = new Forecast($time, $units); |
|
92
|
3 |
|
$forecast->city = $this->city; |
|
93
|
3 |
|
$this->forecasts[] = $forecast; |
|
94
|
|
|
|
|
95
|
3 |
|
$counter++; |
|
96
|
|
|
// Make sure to only return the requested number of days. |
|
97
|
3 |
|
if ($days <= 5 && $counter == $days * 8) { |
|
98
|
|
|
break; |
|
99
|
3 |
|
} elseif ($days > 5 && $counter == $days) { |
|
100
|
3 |
|
break; |
|
101
|
|
|
} |
|
102
|
3 |
|
} |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @internal |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function rewind() |
|
109
|
|
|
{ |
|
110
|
3 |
|
$this->position = 0; |
|
111
|
3 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @internal |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function current() |
|
117
|
|
|
{ |
|
118
|
3 |
|
return $this->forecasts[$this->position]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @internal |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function key() |
|
125
|
|
|
{ |
|
126
|
3 |
|
return $this->position; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @internal |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function next() |
|
133
|
|
|
{ |
|
134
|
3 |
|
++$this->position; |
|
135
|
3 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @internal |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function valid() |
|
141
|
|
|
{ |
|
142
|
3 |
|
return isset($this->forecasts[$this->position]); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|