1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bashar\WeatherModel; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A sample controller to show how a controller class can be implemented. |
10
|
|
|
* The controller will be injected with $di if implementing the interface |
11
|
|
|
* ContainerInjectableInterface, like this sample class does. |
12
|
|
|
* The controller is mounted on a particular route and can then handle all |
13
|
|
|
* requests for that mount point. |
14
|
|
|
* |
15
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
16
|
|
|
*/ |
17
|
|
|
class OpenWeatherMapModel implements ContainerInjectableInterface |
18
|
|
|
{ |
19
|
|
|
use ContainerInjectableTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $message = "127.0.0.1"; |
25
|
|
|
private $weatherApiPrevious = []; |
26
|
|
|
private $weatherApiNext; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* returns the api from the config file "ipstackcfg" |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
2 |
|
public function getDetails() : string |
34
|
|
|
{ |
35
|
2 |
|
return $this->message; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets the api from the config file |
41
|
|
|
* into the controller. |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
13 |
|
public function setMessage($message) : void |
45
|
|
|
{ |
46
|
13 |
|
$this->message = $message; |
47
|
13 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the api from the config file |
52
|
|
|
* into the controller. |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
3 |
|
public function setGeoApi($enteredIp, $geoApi) : void |
56
|
|
|
{ |
57
|
3 |
|
$this->geoApiUrl = "http://api.ipstack.com/". $enteredIp . "?access_key=" . $geoApi . |
|
|
|
|
58
|
3 |
|
'&hostname=1&security=1'; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* returns the api from the config file "ipstackcfg" |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
3 |
|
public function getGeoApiUrl() |
67
|
|
|
{ |
68
|
3 |
|
return $this->geoApiUrl; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the api from the config file |
74
|
|
|
* into the controller. |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
1 |
|
public function setWeatherApi5PreviousDays($geoUrlLat, $geoUrlLon) : void |
78
|
|
|
{ |
79
|
1 |
|
$options = '&exclude=minutely,hourly,alerts&units=metric&lang=en'; |
80
|
1 |
|
$baseUrl = 'http://api.openweathermap.org/data/2.5/onecall/timemachine?'; |
81
|
1 |
|
for ($i = -5; $i <= -1; $i++) { |
82
|
1 |
|
$datePrevious = strtotime("$i days"); |
83
|
1 |
|
array_push($this->weatherApiPrevious, $baseUrl . 'lat=' . $geoUrlLat . '&lon=' . |
84
|
1 |
|
$geoUrlLon . '&dt=' . $datePrevious . $options . '&appid=' . $this->message); |
85
|
|
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the api from the config file |
91
|
|
|
* into the controller. |
92
|
|
|
* |
93
|
|
|
*/ |
94
|
1 |
|
public function setWeatherApiNext10Days($geoUrlLat, $geoUrlLon) : void |
95
|
|
|
{ |
96
|
1 |
|
$options = '&exclude=current,minutely,hourly,alerts&units=metric&lang=en'; |
97
|
1 |
|
$baseUrl = 'https://api.openweathermap.org/data/2.5/onecall?'; |
98
|
|
|
|
99
|
1 |
|
$this->weatherApiNext = $baseUrl . 'lat=' . $geoUrlLat . '&lon=' . |
100
|
1 |
|
$geoUrlLon . $options . '&appid=' . $this->message; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* a json format |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
2 |
|
public function getWeatherInJson($weatherApi) |
109
|
|
|
{ |
110
|
2 |
|
$encoded = json_encode($weatherApi, true); |
|
|
|
|
111
|
2 |
|
$pattern1 = "/\"{/i"; |
112
|
2 |
|
$first = preg_replace($pattern1, "{", $encoded); |
113
|
2 |
|
$pattern2 = "/[\]]$/i"; |
114
|
2 |
|
$second = preg_replace($pattern2, "<br>]", $first); |
115
|
2 |
|
$pattern3 = "/,/i"; |
116
|
2 |
|
$third = preg_replace($pattern3, ",<br>********", $second); |
117
|
2 |
|
$pattern4 = "/{/i"; |
118
|
2 |
|
$fourth = preg_replace($pattern4, "{<br>********", $third); |
119
|
2 |
|
$pattern5 = "/}/i"; |
120
|
2 |
|
$fifth = preg_replace($pattern5, "<br>****}", $fourth); |
121
|
2 |
|
$pattern6 = "/[\\\\]/i"; |
122
|
2 |
|
$sixth = preg_replace($pattern6, "", $fifth); |
123
|
2 |
|
$string = str_replace("*", " ", $sixth); |
124
|
|
|
|
125
|
2 |
|
return $string; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* returns the api from the config file "openWeatherMap" |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
1 |
|
public function getWeatherApiPrevious() |
133
|
|
|
{ |
134
|
1 |
|
return $this->weatherApiPrevious; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* returns the api from the config file "openWeatherMap" |
140
|
|
|
* |
141
|
|
|
*/ |
142
|
1 |
|
public function getWeatherApiNext() |
143
|
|
|
{ |
144
|
1 |
|
return $this->weatherApiNext; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|