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 pathMETnoFactory.php
442 lines (393 loc) · 12.5 KB
/
METnoFactory.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/**
* @author Martin Kluska @ iMakers, s.r.o. <martin.kluska@imakers.cz>
* @copyright iMakers, s.r.o.
* @copyright Martin Kluska
* @web http://imakers.cz
*
*/
interface METnoInterface {
const DOWNLOAD_FAILED = 100;
const XML_INVALID = 101;
const DATA_EMPTY = 102;
const FORECAST_INVALID = 103;
}
class METnoFactory implements METnoInterface {
/**
* class name for extending function for symbols. Sub class must extend
* <METnoSymbol>
* @var METnoSymbol
*/
static protected $classSymbol = "METnoSymbol";
/**
* class name for precipitation wich must subclass <METnoPrecipitation>
* @var METnoPrecipitation
*/
static protected $classPrecipitation = "METnoPrecipitation";
/**
* Defines hour wich will be used when selecting day forecast
* - used only if <$dayForecastHighest> is false
* - range of 0 - 23
* @var int
*/
static protected $dayForecastByHour = 14;
/**
* Defines hour wich will be used when selecting night forecast
* - used only if <$nightForecastLowest> is false
* - range of 0 - 23
* @var int
*/
static protected $nightForecastByHour = 23;
/**
* Defines hour wich defines when the night starts
* - used only if <$nightForecastLowest> is true
* - range of 0 - 23
* @todo sunset time by location
* @var int
*/
static protected $nightHourStart = 20;
/**
* Weather forecast for all day is selected from the hightest
* temperature
* @var boolean
*/
static protected $dayForecastHighest = true;
/**
* Weather forecast for night is selected from the lowest temperature
* @var boolean
*/
static protected $nightForecastLowest = true;
/**
* Folder for caching by location and hour
* @var string
*/
static protected $cacheDir = "_METcache/";
/**
* Count of decimals for rounding of wind speed
* @var int
*/
static protected $decimalWindSpeed = 2;
/**
* Count of decimals for rounding of percente values
* @var int
*/
static protected $decimalPercente = 0;
/**
* Count of decimals for rounding of temperature
* @var int
*/
static protected $decimalTemperature = 0;
/**
* Display error and stop php
* @var boolean
*/
static protected $dieOnError = false;
/**
* Display error and continue
* @var boolean
*/
static protected $displayErrors = false;
/**
* Display error and stop php
* @param type $set
*/
static public function setDieOnError($set = true) {
self::$dieOnError = $set;
}
/**
* Display error and continue
* @param type $set
*/
static public function setDisplayErrors($set = true) {
self::$displayErrors = $set;
}
/**
* Sets the hour for selecting day forecast (disables selecting by highest temperature)
* @param int $hour - range of 0 - 23
* @return boolean
*/
static public function setHourForDayForecast($hour) {
$hour = intval($hour);
if ($hour >= 0 && $hour <= 23) {
self::$dayForecastHighest = false;
self::$dayForecastByHour = $hour;
return true;
}
return false;
}
/**
* Sets the hour for selecting night forecast (disables selecting by lowest temperature)
* @param int $hour - range of 0 - 23
* @return boolean
*/
static public function setHourForNightForecast($hour) {
$hour = intval($hour);
if ($hour >= 0 && $hour <= 23) {
self::$nightForecastLowest = false;
self::$nightForecastByHour = $hour;
return true;
}
return false;
}
/**
* Defines when the night starts
* @param int $hour - range of 0 - 23
* @return boolean
*/
static public function setHourWhenNightStarts($hour) {
$hour = intval($hour);
if ($hour >= 0 && $hour <= 23) {
self::$nightHourStart = $hour;
return true;
}
return false;
}
/**
* Sets if the day forecast should be choosed by highest temperature or defined
* hour <METnoFactory::setHourForDayForecast()>
* @param type $set
* @return boolean
*/
static public function setDetectDayForecastByTemperature($set = true) {
self::$dayForecastHighest = $set;
return true;
}
/**
* Sets if the night forecast should be choosed by lowest temperature or defined
* hour <METnoFactory::setHourForNightForecast()>
* @param type $set
* @return boolean
*/
static public function setDetectNightForecastByTemperature($set = true) {
self::$nightForecastLowest = $set;
return true;
}
/**
* Sets a class name for symbol. Must be subclass of <METnoSymbol>
*
* - the class must exists or it tries to load it in the same directory
*
* @param type $class_name
* @return boolean
*/
static public function setSymbolClass($class_name) {
self::loadClass($class_name); // try to load class if not present
if (class_exists($class_name)) {
self::$classSymbol = $class_name;
return true;
}
return false;
}
/**
* Sets a class name for precipitation. Must be subclass of <METnoPrecipitation>
*
* - the class must exists or it tries to load it in the same directory
*
* @param type $class_name
* @return boolean
*/
static public function setPrecipitationClass($class_name) {
if (!class_exists($class_name) && file_exists($class_name.".php")) {
require_once $class_name.".php";
}
if (class_exists($class_name)) {
self::$classPrecipitation = $class_name;
return true;
}
return false;
}
static protected function loadClass($class_name) {
if (!class_exists($class_name)) {
if (file_exists(__DIR__."/".$class_name.".php")) {
require_once __DIR__."/".$class_name.".php";
} else if (file_exists($class_name.".php")) {
require_once $class_name.".php";
}
}
}
/**
* Returns the number of decimals for windSpeed
* @return boolean
*/
static public function setWindSpeedDecimals($set) {
self::$decimalWindSpeed = $set;
return true;
}
/**
* Returns the number of decimals for percente values
* @return boolean
*/
static public function setPercenteDecimals($set) {
self::$decimalPercente = $set;
return true;
}
/**
* Returns the number of decimals for temperature
* @return boolean
*/
static public function setTemperatureDecimals($set) {
self::$decimalTemperature = $set;
return true;
}
/**
* Gets forecast for location defined by Lat and Lon
* @param type $lat
* @param type $lon
* @param type $seeLevel
* @return \METno
*/
static public function getForecastByLatLon($lat, $lon, $seeLevel = false) {
$yr = new METno($lat, $lon, $seeLevel);
$yr->getForecast();
return $yr;
}
/**
* Gets forecast for the location defined by the adress using google geocoding
* @todo
* @param type $locationName
* @return \METno
*/
static public function getForecastByLocation($locationName) {
$lat = 0;
$lon = 0;
$yr = new METno($lat, $lon);
$yr->getForecast();
return $yr;
}
/**
* Returns only date 2012-08-27 from 2012-08-27T18:00:00Z
* @param type $date Date in format 2012-08-27T18:00:00Z
* @return boolean|string
*/
static public function getDate($date) {
if (preg_match("~([\d]{4})-([\d]{2})-([\d]{2})~", $date, $match)) {
return $match[0];
}
return false;
}
/**
* Returns only time 18:00 from 2012-08-27T18:00:00Z
* @param type $date Date in format 2012-08-27T18:00:00Z
* @return boolean|string
*/
static public function getTime($date) {
if (preg_match("~[\d]{4}-[\d]{2}-[\d]{2}T([\d]{2}):([\d]{2})~", $date, $match) && isset($match[1]) && isset($match[2])) {
return $match[1].":".$match[2];
}
return false;
}
/**
* Returns only hour 18 from 2012-08-27T18:00:00Z
* @param type $date Date in format 2012-08-27T18:00:00Z
* @return boolean|int
*/
static public function getHour($date) {
if (preg_match("~[\d]{4}-[\d]{2}-[\d]{2}T([\d]{2}):[\d]{2}~", $date, $match) && isset($match[1])) {
return intval($match[1]);
}
return false;
}
/**
* Checks in attributes array if there is an attribute key and returns string
* or float with defined decimals
* @param SimpleXMLElement $attributes
* @param string $attributeKey
* @param int $floatValAndRoundByDecimals if defined, the value is floated and
* rounded with defined decimals
* @return int|boolean|string
*/
static public function getAttributeValue(SimpleXMLElement $attributes,$attributeKey,$floatValAndRoundByDecimals = -1) {
if (isset($attributes[$attributeKey])) {
$value = $attributes[$attributeKey]->__toString();
if ($floatValAndRoundByDecimals != -1) {
$value = round(floatval($value),$floatValAndRoundByDecimals);
}
return $value;
} else if ($floatValAndRoundByDecimals != -1) {
return 0;
}
return false;
}
/**
* Returns the number of decimals for windSpeed
* @return int
*/
static public function getWindSpeedDecimals() {
return self::$decimalWindSpeed;
}
/**
* Returns the number of decimals for percente values
* @return int
*/
static public function getPercenteDecimals() {
return self::$decimalPercente;
}
/**
* Returns the number of decimals for temperature
* @return int
*/
static public function getTemperatureDecimals() {
return self::$decimalTemperature;
}
/**
* Get an entry of forecast by offset hour, first look in forecast Array with
* hour - offset, if not set, find hour + offset, if not found, increse offset +1
* and start again. Max loops are 10, then boolean returned
* @param type $hour
* @param type $forecastArrayByHour
* @param type $offset
* @return boolean|SimpleXMLElement
*/
static public function getNearestForecastForHour($hour,$forecastArrayByHour,$offset = 1) {
$prevHour = $hour - $offset;
$nextHour = $hour + $offset;
if (isset($forecastArrayByHour[$nextHour])) {
return $forecastArrayByHour[$nextHour];
}
if (isset($forecastArrayByHour[$prevHour])) {
return $forecastArrayByHour[$prevHour];
}
if ($offset == 24) {
return false;
}
return self::getNearestForecastForHour($hour, $forecastArrayByHour, $offset+1);
}
/**
* Get hour for day forecast
* @return int
*/
static public function getHourForDayForecast() {
return self::$dayForecastByHour;
}
/**
* Returns hour for night forecast
* @return int
*/
static public function getHourForNightForecast() {
return self::$nightForecastByHour;
}
/**
* Gets the hour when night starts
* @return int
*/
static public function getHourWhenNightStarts() {
return self::$nightHourStart;
}
/**
* Should the day info from the highest temperature?
* @return int
*/
static public function isDayForecastByHighestTemp() {
return self::$dayForecastHighest;
}
/**
* Should the night info from the lowest temperature?
* Night detection is defined by the hour wich the night starts
* <METnoFactory::getHourWhenNightStarts()>
* @return int
*/
static public function isNightForecastByLowestTemp() {
return self::$nightForecastLowest;
}
}
?>