|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Anax\Curl\Curl as Curl; |
|
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
|
1 |
|
public function indexAction() : object |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
1 |
|
$title = "Weather"; |
|
31
|
|
|
|
|
32
|
1 |
|
$address = $_SERVER["REMOTE_ADDR"] ?? "No IP detected."; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
1 |
|
$page = $this->di->get("page"); |
|
37
|
|
|
|
|
38
|
|
|
$data = [ |
|
39
|
1 |
|
"address" => $address |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
1 |
|
$page->add("weatherjson/verify", $data); |
|
44
|
|
|
|
|
45
|
1 |
|
return $page->render([ |
|
46
|
1 |
|
"title" => $title, |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function weatherjsonActionGet() |
|
51
|
|
|
{ |
|
52
|
1 |
|
$request = $this->di->request; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
$doVerify = $request->getGet("verify", null); |
|
55
|
1 |
|
$address = $request->getGet("ip", null); |
|
56
|
1 |
|
$latitude = $request->getGet("latitude", null); |
|
57
|
1 |
|
$longitude = $request->getGet("longitude", null); |
|
58
|
|
|
|
|
59
|
1 |
|
$check = $this->di->get("validator"); |
|
60
|
|
|
|
|
61
|
1 |
|
$weather = new Curl(); |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
1 |
|
if ($doVerify && $address) { |
|
65
|
1 |
|
$result = $check->checkIpv($address); |
|
66
|
|
|
|
|
67
|
1 |
|
$geotag = $check->geoTag($address); |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
$fetchWeather = $weather->weather($geotag->latitude, $geotag->longitude); |
|
71
|
1 |
|
$fetchPastWeather = $weather->pastWeather($geotag->latitude, $geotag->longitude); |
|
72
|
|
|
|
|
73
|
1 |
|
$weatherData = $fetchWeather->daily; |
|
74
|
|
|
|
|
75
|
1 |
|
$pastData = $fetchPastWeather; |
|
76
|
|
|
|
|
77
|
|
|
$data = [ |
|
78
|
1 |
|
"valid" => $result, |
|
79
|
1 |
|
"week" => $weatherData, |
|
80
|
1 |
|
"past Month" => $pastData, |
|
81
|
|
|
|
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
1 |
|
return [$data]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($doVerify && $latitude) { |
|
88
|
|
|
$verifyCoord = $check->verifyGeo($latitude, $longitude); |
|
89
|
|
|
|
|
90
|
|
|
if ($verifyCoord) { |
|
91
|
|
|
$fetchWeather = $weather->weather($latitude, $longitude); |
|
92
|
|
|
$fetchPastWeather = $weather->pastWeather($latitude, $longitude); |
|
93
|
|
|
|
|
94
|
|
|
$weatherData = $fetchWeather->daily; |
|
95
|
|
|
$pastData = $fetchPastWeather; |
|
96
|
|
|
|
|
97
|
|
|
$valid = "The coordinates $latitude, $longitude are valid."; |
|
98
|
|
|
|
|
99
|
|
|
$data = [ |
|
100
|
|
|
"valid" => $valid, |
|
101
|
|
|
"week" => $weatherData, |
|
102
|
|
|
"past Month" => $pastData, |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
return [$data]; |
|
106
|
|
|
} else { |
|
107
|
|
|
$invalid = "The coordinates are invalid. Try again."; |
|
108
|
|
|
|
|
109
|
|
|
return [$invalid]; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|