This repository was archived by the owner on Apr 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMETnoDay.php
297 lines (253 loc) · 10.6 KB
/
METnoDay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* @author Martin Kluska @ iMakers, s.r.o. <martin.kluska@imakers.cz>
* @copyright iMakers, s.r.o.
* @copyright Martin Kluska
* @web http://imakers.cz
*
*
*
* @todo Morning forecast
* @todo correct hour symbol from different symbol form to
*/
class METnoDay extends METnoForecast {
/**
* For internal work with main METno instance (about location etc)
* @var METno
*/
protected $metNo = false;
protected $today = false;
protected $hourWeather = array();
/**
* Forecast for the night, selected from the lowest temperature in night
* Hour > 21:00 or by the time is setted (def 23)
*
* From these props is detection made:
* <METnoFactory::$nightForecastByHour>
* <METnoFactory::$nightForecastLowest>
*
* @var METnoForecast
*/
protected $nightForecast;
/**
* Forecast for the hour where the temperature is highest
*
* @var METnoForecast
*/
protected $hourForecastForHighestTemperature;
/**
* Forecast for the hour where the temperature is lowest
*
* @var METnoForecast
*/
protected $hourForecastForTheLowestTemperature;
public function __construct($date,$arrayOfHours,$metNo = false) {
$this->date = $date;
$this->metNo = $metNo;
/**
* We must fill the hours with the closest time entry of weather symbol
* and weather info.
*
* if the weather info is not defined is hour with symbols, we fill find
* the nearest weather info the symbol details
*
* - This happens only on today entries
*
* - first fill hours that have detail
*/
$cleanForecastByHour = array();
$otherForecastByHour = array();
$nearestForecastHour = 24;
$nearestForecast = false;
foreach ($arrayOfHours as $hour => $hourForecast) {
if (is_object($hourForecast["detail"])) {
$cleanForecastByHour[$hour] = $hourForecast;
if ($hour < $nearestForecastHour) {
$nearestForecastHour = $hour;
$nearestForecast = $hourForecast["detail"];
}
} else {
$otherForecastByHour[$hour] = $hourForecast["symbols"];
}
}
/**
* Fill hour forecast with symbol only entries and select whethear info
* by lowest hour
*/
if (!empty($otherForecastByHour) && is_object($nearestForecast)) {
foreach ($otherForecastByHour as $hour => $hourSymbols) {
if (!isset($cleanForecastByHour[$hour]) && !empty($hourSymbols)) {
$cleanForecastByHour[$hour] = array(
"detail" => $nearestForecast,
"symbols" => $hourSymbols
);
} else if (!empty($hourSymbols)) {
// merge if need, not verified, just protection
$cleanForecastByHour[$hour]["symbols"] = array_merge($cleanForecastByHour[$hour]["symbols"],$hourSymbols);
}
}
}
/**
* Now when we are sure that all hours or part of them are cleaned and
* compine forecast for hours that are not setted
*/
$this->today = $this->date == date("Y-m-d");
$startHour = 0; // start from the 0 hour
if ($this->today) { // fill only hours that will be in future, start on first filled hour
$startHourKeys = array_keys($cleanForecastByHour);
$startHour = reset($startHourKeys);
}
for ($hour = $startHour; $hour < 24; $hour++) {
if (!isset($cleanForecastByHour[$hour])) {
/**
* Search for the closest forecast and duplicate the information for the hour
*/
$hourForecast = METnoFactory::getNearestForecastForHour($hour, $cleanForecastByHour);
if (is_bool($hourForecast)) {
if (is_object($this->metNo)) {
$this->metNo->error(new Exception("Could not find nereast forecast, this should not happen.", METno::FORECAST_INVALID));
continue;
} else {
die("Could not find nereast forecast, this should not happen. Code: ".METno::FORECAST_INVALID);
}
}
} else {
$hourForecast = $cleanForecastByHour[$hour];
}
$this->hourWeather[$hour] = new METnoForecast($this,$this->date,$hour,$hourForecast["detail"],$hourForecast["symbols"]);
/**
* get the hightest and lowest temperature and the night forecast if
* needed. We need only hour forecast wich were defined in xml
*/
if (isset($cleanForecastByHour[$hour])) {
/**
* Get the highest and the lowest temperature for the day
*/
$this->hourForecastForHighestTemperature = $this->getHourForecastForTemperature($this->hourForecastForHighestTemperature, $this->hourWeather[$hour]);
$this->hourForecastForTheLowestTemperature = $this->getHourForecastForTemperature($this->hourForecastForTheLowestTemperature, $this->hourWeather[$hour],false);
/**
* Get the night forecast if the detection is by the lowest temperature in night
* Night detection is made by hour METnoFactory::getNightStartHour()
*/
if (METnoFactory::isNightForecastByLowestTemp() && METnoFactory::getHourWhenNightStarts() <= $this->hourWeather[$hour]->getHour()) {
$this->nightForecast = $this->getHourForecastForTemperature($this->nightForecast, $this->hourWeather[$hour],false);
}
}
}
/**
* Get the night forecast from setted hour if the night forecast is not made
* by the lowest temperature of the night <METnoFactory::getHourForNightForecast()>
*/
if (!METnoFactory::isNightForecastByLowestTemp()) {
$this->nightForecast = $this->hourWeather[METnoFactory::getHourForNightForecast()];
}
/**
* We need to create weather info for the whole day so we will
* take for the highest temperature for the day if defined in
* <METnoFactory::isDayForecastByHighestTemp()> or from
* specified hour <METnoForecast::getHourForDayForecast()>
*
*/
if ($this->today) { // dont take this day
$weatherToCopy = reset($this->hourWeather);
} else {
if (METnoFactory::isDayForecastByHighestTemp()) {
$weatherToCopy = $this->hourForecastForHighestTemperature;
} else if (isset($this->hourWeather[METnoFactory::getHourForDayForecast()])) {
$weatherToCopy = $this->hourWeather[METnoFactory::getHourForDayForecast()];
}
}
/**
* Copy the properties of the forecast to the day forecast properities
*/
if (isset($weatherToCopy) && is_object($weatherToCopy)) {
foreach ($weatherToCopy as $property => $value) {
if ($property != "parent") {
$this->$property = $value;
}
}
}
}
public function isToday() {
return $this->today;
}
/**
* Returns self becouse of overiding the hour forecast
* @return \METnoDay
*/
public function getMETDay() {
return $this;
}
/**
* Return the hour forecast if exist, if not, return self
* @param int $hour
* @return METnoForecast
*/
public function getForecastForHour($hour) {
if (isset($this->hourWeather[$hour])) {
return $this->hourWeather[$hour];
} else {
return $this;
}
}
/**
* Gets the night forecast
* @return METnoForecast
*/
public function getNightForecast() {
return $this->nightForecast;
}
/**
* Gets the lowest temperature of the day
* @return METnoForecast
*/
public function getLowestTemperature() {
return $this->getLowestTemperatureForecast()->getTemperature();
}
/**
* Gets the hour forecast with lowest temperature of the day
* @return METnoForecast
*/
public function getLowestTemperatureForecast() {
return $this->hourForecastForTheLowestTemperature;
}
/**
* Gets the highest temperature of the day
* @return METnoForecast
*/
public function getHighestTemperature() {
return $this->getHighestTemperatureForecast()->getTemperature();
}
/**
* Gets the hour forecast with highest temperature of the day
* @return METnoForecast
*/
public function getHighestTemperatureForecast() {
return $this->hourForecastForHighestTemperature;
}
/**
* Detects wich hour forecast (METnoForecast) has the highest or lowest temperature,
*
* If the instance is false, return instanceForCheck
*
* @param METnoForecast|boolean $instance
* @param METnoForecast $instanceForCheck
* @param boolean $higher defines if the higher temperature forecast should be returned
* @return METnoForecast
*/
protected function getHourForecastForTemperature($instance,METnoForecast $instanceForCheck,$higher = true) {
if (is_object($instance)) {
if ($higher) {
if ($instance->getTemperature() < $instanceForCheck->getTemperature()) {
return $instanceForCheck;
}
} else if ($instance->getTemperature() > $instanceForCheck->getTemperature()) {
return $instanceForCheck;
}
} else {
return $instanceForCheck;
}
return $instance;
}
}
?>