1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* IP Controller |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Hepa19\Weather; |
8
|
|
|
|
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller for IP validator API |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class WeatherJSONController implements ContainerInjectableInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerInjectableTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Index page |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
3 |
|
public function indexActionGet() : array |
26
|
|
|
{ |
27
|
3 |
|
$request = $this->di->get("request"); |
28
|
3 |
|
$location = $request->getGet("location"); |
29
|
3 |
|
$type = $request->getGet("type"); |
30
|
|
|
|
31
|
3 |
|
if ($type == "past") { |
32
|
1 |
|
$weather = $this->di->get("weather"); |
33
|
|
|
} else { |
34
|
2 |
|
$weather = $this->di->get("weather_prog"); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
if (empty($location)) { |
38
|
1 |
|
$ipgetcurrent = $this->di->get("ipgetcurrent"); |
39
|
1 |
|
$location = $ipgetcurrent->getIP(); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
$ipstackRes = $this->getIPData($location); |
43
|
3 |
|
$lon = $ipstackRes["longitude"] ?? null; |
44
|
3 |
|
$lat = $ipstackRes["latitude"] ?? null; |
45
|
|
|
|
46
|
3 |
|
if (!empty($lon) && !empty($lat) && $type == "past") { |
47
|
1 |
|
$weather->setUrls($lat, $lon); |
48
|
1 |
|
$weatherInfo = $this->getMultiData($weather, $lat, $lon); |
49
|
2 |
|
} else if (!empty($lon) && !empty($lat) && $type == "future") { |
50
|
1 |
|
$weather->setUrl($lat, $lon); |
51
|
1 |
|
$weatherInfo = $this->getData($weather, $lat, $lon); |
52
|
|
|
} else { |
53
|
1 |
|
$data = ["status" => 400, "message" => "Hittade inget resultat."]; |
54
|
1 |
|
return [$data]; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$map = $this->di->get("map"); |
58
|
2 |
|
$mapLink = $map->getMap($ipstackRes["longitude"], $ipstackRes["latitude"]); |
59
|
|
|
|
60
|
|
|
$data = [ |
61
|
2 |
|
"location" => $location ?? null, |
62
|
2 |
|
"longitude" => $lon ?? null, |
63
|
2 |
|
"latitude" => $lat ?? null, |
64
|
2 |
|
"weather" => $weatherInfo ?? null, |
65
|
2 |
|
"map_link" => $mapLink ?? null |
66
|
|
|
]; |
67
|
|
|
|
68
|
2 |
|
return [$data]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get ip data |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function getIPData($location) |
78
|
|
|
{ |
79
|
|
|
$ipstack = $this->di->get("ipstack"); |
80
|
|
|
$ipstack->setUrl($location); |
81
|
|
|
$ipstackRes = $ipstack->getData(); |
82
|
|
|
|
83
|
|
|
return $ipstackRes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get weather data |
90
|
|
|
* |
91
|
|
|
*/ |
92
|
|
|
public function getData($weather, $lat, $lon) |
93
|
|
|
{ |
94
|
|
|
$weatherInfo = $weather->getData($lat, $lon); |
95
|
|
|
$weatherInfo = $weather->filterFuture($weatherInfo); |
96
|
|
|
|
97
|
|
|
return $weatherInfo; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get weather data multi |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
public function getMultiData($weather, $lat, $lon) |
107
|
|
|
{ |
108
|
|
|
$weatherInfo = $weather->getMultiData($lat, $lon); |
109
|
|
|
$weatherInfo = $weather->filterPast($weatherInfo); |
110
|
|
|
|
111
|
|
|
return $weatherInfo; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|