|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Anax\Controller\IpModel; |
|
8
|
|
|
|
|
9
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
10
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
11
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
15
|
|
|
* The controller will be injected with $di if implementing the interface |
|
16
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
17
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
18
|
|
|
* requests for that mount point. |
|
19
|
|
|
* |
|
20
|
|
|
@SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
21
|
|
|
*/ |
|
22
|
|
|
class WeatherJsonController implements ContainerInjectableInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use ContainerInjectableTrait; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $db a sample member variable that gets initialised |
|
30
|
|
|
*/ |
|
31
|
|
|
private $model; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The initialize method is optional and will always be called before the |
|
37
|
|
|
* target method/action. This is a convienient method where you could |
|
38
|
|
|
* setup internal properties that are commonly used by several methods. |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function initialize() : void |
|
43
|
|
|
{ |
|
44
|
|
|
// Use to initialise member variables. |
|
45
|
2 |
|
$this->model = new IpModel(); |
|
|
|
|
|
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* This is the index method action, it handles: |
|
52
|
|
|
* ANY METHOD mountpoint |
|
53
|
|
|
* ANY METHOD mountpoint/ |
|
54
|
|
|
* ANY METHOD mountpoint/index |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
1 |
|
public function indexAction() : object |
|
60
|
|
|
{ |
|
61
|
1 |
|
$title = "IP JSON"; |
|
62
|
1 |
|
$page = $this->di->get("page"); |
|
63
|
|
|
|
|
64
|
1 |
|
$page->add("weather/json_docs"); |
|
65
|
1 |
|
return $page->render([ |
|
66
|
1 |
|
"title" => $title, |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return array |
|
72
|
|
|
*@SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function jsonAction() : array |
|
75
|
|
|
{ |
|
76
|
1 |
|
$weather = $this->di->get("weather"); |
|
77
|
1 |
|
$title = "ip validator with map"; |
|
|
|
|
|
|
78
|
1 |
|
$request = $this->di->get("request"); |
|
79
|
1 |
|
$ipAddress = $request->getGet("ipMap") ?? null; |
|
80
|
1 |
|
$version = null; |
|
81
|
1 |
|
$localIP = $this->model->local(); |
|
|
|
|
|
|
82
|
1 |
|
$res = null; |
|
83
|
1 |
|
$lat = $res;//["latitude"]; |
|
84
|
1 |
|
$long = $res;//["longitude"]; |
|
85
|
|
|
// $t = $weather->histWeather($lat, $long); |
|
86
|
1 |
|
$country = $res;//["country_name"]; |
|
87
|
1 |
|
$region = $res;//["region_name"]; |
|
88
|
1 |
|
$hostname = null; |
|
89
|
1 |
|
$check = null; |
|
90
|
1 |
|
$answer = null; |
|
91
|
1 |
|
$summaries = []; |
|
|
|
|
|
|
92
|
1 |
|
$dates = null; |
|
93
|
1 |
|
$type = $request->getGet("type") ?? null; |
|
94
|
1 |
|
$json = null; |
|
95
|
1 |
|
if ($ipAddress !== null && $type !== null) { |
|
96
|
|
|
if (strpos($ipAddress, ":") || strpos($ipAddress, ",") == false) { |
|
|
|
|
|
|
97
|
|
|
$response = $this->model->ipValidate($ipAddress); |
|
98
|
|
|
$check = $response[0]; |
|
99
|
|
|
$hostname = $response[1]; |
|
100
|
|
|
$version = $response[2]; |
|
101
|
|
|
$res = $this->model->ipStack($ipAddress); |
|
102
|
|
|
$lat = $res["latitude"]; |
|
103
|
|
|
$long = $res["longitude"]; |
|
104
|
|
|
} elseif (strpos($ipAddress, ",")) { |
|
105
|
|
|
$latlong = explode(",", $ipAddress); |
|
106
|
|
|
$lat = $latlong[0]; |
|
107
|
|
|
$long = $latlong[1]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($type == "Historik") { |
|
111
|
|
|
$answer = $weather->histWeather($lat, $long); |
|
112
|
|
|
$dates = $weather->dates('-31 day', 30); |
|
113
|
|
|
$json = $weather->json($answer, $dates, "h"); |
|
114
|
|
|
} elseif ($type == "Kommande") { |
|
115
|
|
|
$answer = $weather->newWeather($lat, $long); |
|
116
|
|
|
$dates = $weather->dates(' +0 day', 7); |
|
117
|
|
|
$json = $weather->json($answer, $dates, "k"); |
|
118
|
|
|
} |
|
119
|
|
|
$country = $res["country_name"]; |
|
120
|
|
|
$region = $res["region_name"]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$data = [ |
|
124
|
1 |
|
"check" => $check, |
|
125
|
1 |
|
"hostname" => $hostname, |
|
126
|
1 |
|
"type" => $version, |
|
127
|
1 |
|
"lat" => $lat, |
|
128
|
1 |
|
"long" => $long, |
|
129
|
1 |
|
"country" => $country, |
|
130
|
1 |
|
"region" => $region, |
|
131
|
1 |
|
"weather" => $json, |
|
132
|
|
|
]; |
|
133
|
|
|
|
|
134
|
1 |
|
return [$data]; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..