|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jodn14\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Jodn14\Models\Validator; |
|
8
|
|
|
use Jodn14\Models\Locator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Json Locator Controller. Validates and gets location data from third part api(ipstack) |
|
12
|
|
|
* returns response in json format to blank page |
|
13
|
|
|
*/ |
|
14
|
|
|
class OwnJSONWeatherController implements ContainerInjectableInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use ContainerInjectableTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string $db a sample member variable that gets initialised |
|
20
|
|
|
*/ |
|
21
|
|
|
private $db = "not active"; |
|
22
|
|
|
private $validator; |
|
23
|
|
|
private $locator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The initialize method is optional and will always be called before the |
|
27
|
|
|
* target method/action. This is a convienient method where you could |
|
28
|
|
|
* setup internal properties that are commonly used by several methods. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function initialize() : void |
|
33
|
|
|
{ |
|
34
|
|
|
// Use to initialise member variables. |
|
35
|
5 |
|
$this->db = "active"; |
|
36
|
5 |
|
$this->validator = new Validator(); |
|
37
|
5 |
|
$this->locator = new Locator(); |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Display index resultset in JSON format |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function indexAction() : array |
|
48
|
|
|
{ |
|
49
|
|
|
$json = [ |
|
50
|
1 |
|
"result" => "JSON REST API working", |
|
51
|
|
|
]; |
|
52
|
1 |
|
return [$json]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* This sample method action takes one argument: |
|
58
|
|
|
* GET mountpoint/argument/<value> |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $value |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function forecastActionGet($value) : array |
|
65
|
|
|
{ |
|
66
|
1 |
|
$curl = $this->di->get("curl"); |
|
67
|
|
|
|
|
68
|
1 |
|
$valid = $this->validator->validateIp($value); |
|
69
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
|
70
|
|
|
//valid, check what the return contents are |
|
71
|
1 |
|
$location = $curl->getCoordinates($value); |
|
72
|
1 |
|
if (in_array(400, $location)) { |
|
73
|
1 |
|
if ($location["code"] == 400) { |
|
74
|
1 |
|
$json = $location; |
|
75
|
|
|
|
|
76
|
1 |
|
return [$json]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
1 |
|
$latitude = $location["lat"]; |
|
80
|
1 |
|
$longitud = $location["lon"]; |
|
81
|
|
|
} else { |
|
82
|
1 |
|
$location = $this->locator->getLocation($value); |
|
83
|
1 |
|
$longitud = $location["longitud"]; |
|
84
|
1 |
|
$latitude = $location["latitude"]; |
|
85
|
1 |
|
$value = $location["city"]; |
|
86
|
|
|
} |
|
87
|
1 |
|
$weather = $curl->getForecast($latitude, $longitud); |
|
88
|
|
|
$info = [ |
|
89
|
1 |
|
"city" => $value, |
|
90
|
1 |
|
"latitude" => $latitude ?? null, |
|
91
|
1 |
|
"longitud" => $longitud ?? null, |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
1 |
|
$json = array_merge($valid, array_merge($weather, $info)); |
|
95
|
1 |
|
return [$json]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* This sample method action takes one argument: |
|
100
|
|
|
* GET mountpoint/argument/<value> |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function pastActionGet($value) : array |
|
107
|
|
|
{ |
|
108
|
1 |
|
$curl = $this->di->get("curl"); |
|
109
|
|
|
|
|
110
|
1 |
|
$valid = $this->validator->validateIp($value); |
|
111
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
|
112
|
|
|
//valid, check what the return contents are |
|
113
|
1 |
|
$location = $curl->getCoordinates($value); |
|
114
|
1 |
|
if (in_array(400, $location)) { |
|
115
|
1 |
|
if ($location["code"] == 400) { |
|
116
|
1 |
|
$json = $location; |
|
117
|
|
|
|
|
118
|
1 |
|
return [$json]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
1 |
|
$latitude = $location["lat"]; |
|
122
|
1 |
|
$longitud = $location["lon"]; |
|
123
|
|
|
} else { |
|
124
|
1 |
|
$location = $this->locator->getLocation($value); |
|
125
|
1 |
|
$longitud = $location["longitud"]; |
|
126
|
1 |
|
$latitude = $location["latitude"]; |
|
127
|
1 |
|
$value = $location["city"]; |
|
128
|
|
|
} |
|
129
|
1 |
|
$weather = $curl->getPast($latitude, $longitud); |
|
130
|
|
|
|
|
131
|
|
|
$info = [ |
|
132
|
1 |
|
"city" => $value, |
|
133
|
1 |
|
"latitude" => $latitude ?? null, |
|
134
|
1 |
|
"longitud" => $longitud ?? null, |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
1 |
|
$json = array_merge($valid, array_merge($weather, $info)); |
|
138
|
1 |
|
return [$json]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* POST to get forecast in json |
|
143
|
|
|
* |
|
144
|
|
|
* @return array JSON Array |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function forecastActionPost() : array |
|
147
|
|
|
{ |
|
148
|
1 |
|
$request = $this->di->get("request"); |
|
149
|
1 |
|
$userInput = $request->getPost("userInput"); |
|
150
|
1 |
|
$curl = $this->di->get("curl"); |
|
151
|
|
|
|
|
152
|
1 |
|
$valid = $this->validator->validateIp($userInput); |
|
153
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
|
154
|
|
|
//valid, check what the return contents are |
|
155
|
1 |
|
$location = $curl->getCoordinates($userInput); |
|
156
|
1 |
|
if (in_array(400, $location)) { |
|
157
|
1 |
|
if ($location["code"] == 400) { |
|
158
|
1 |
|
$json = $location; |
|
159
|
|
|
|
|
160
|
1 |
|
return [$json]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
1 |
|
$latitude = $location["lat"]; |
|
164
|
1 |
|
$longitud = $location["lon"]; |
|
165
|
|
|
} else { |
|
166
|
1 |
|
$location = $this->locator->getLocation($userInput); |
|
167
|
1 |
|
$longitud = $location["longitud"]; |
|
168
|
1 |
|
$latitude = $location["latitude"]; |
|
169
|
1 |
|
$userInput = $location["city"]; |
|
170
|
|
|
} |
|
171
|
1 |
|
$weather = $curl->getForecast($latitude, $longitud); |
|
172
|
|
|
|
|
173
|
|
|
$info = [ |
|
174
|
1 |
|
"city" => $userInput, |
|
175
|
1 |
|
"latitude" => $latitude ?? null, |
|
176
|
1 |
|
"longitud" => $longitud ?? null, |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
1 |
|
$json= array_merge($valid, array_merge($weather, $info)); |
|
180
|
1 |
|
return [$json]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* POST to get forecast in json |
|
185
|
|
|
* |
|
186
|
|
|
* @return array JSON Array |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function pastActionPost() : array |
|
189
|
|
|
{ |
|
190
|
1 |
|
$request = $this->di->get("request"); |
|
191
|
1 |
|
$userInput = $request->getPost("userInput"); |
|
192
|
1 |
|
$curl = $this->di->get("curl"); |
|
193
|
|
|
|
|
194
|
1 |
|
$valid = $this->validator->validateIp($userInput); |
|
195
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
|
196
|
1 |
|
$location = $curl->getCoordinates($userInput); |
|
197
|
1 |
|
if (in_array(400, $location)) { |
|
198
|
1 |
|
if ($location["code"] == 400) { |
|
199
|
1 |
|
$json = $location; |
|
200
|
|
|
|
|
201
|
1 |
|
return [$json]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
1 |
|
$latitude = $location["lat"]; |
|
205
|
1 |
|
$longitud = $location["lon"]; |
|
206
|
|
|
} else { |
|
207
|
1 |
|
$location = $this->locator->getLocation($userInput); |
|
208
|
1 |
|
$longitud = $location["longitud"]; |
|
209
|
1 |
|
$latitude = $location["latitude"]; |
|
210
|
1 |
|
$userInput = $location["city"]; |
|
211
|
|
|
} |
|
212
|
1 |
|
$weather = $curl->getPast($latitude, $longitud); |
|
213
|
|
|
|
|
214
|
|
|
$info = [ |
|
215
|
1 |
|
"city" => $userInput, |
|
216
|
1 |
|
"latitude" => $latitude ?? null, |
|
217
|
1 |
|
"longitud" => $longitud ?? null, |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
1 |
|
$json = array_merge($valid, array_merge($weather, $info)); |
|
221
|
1 |
|
return [$json]; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|