1
|
|
|
<?php
|
2
|
|
|
namespace Anax\Controller;
|
3
|
|
|
use Anax\Commons\ContainerInjectableInterface;
|
4
|
|
|
use Anax\Commons\ContainerInjectableTrait;
|
5
|
|
|
// use Anax\Route\Exception\ForbiddenException;
|
6
|
|
|
// use Anax\Route\Exception\NotFoundException;
|
7
|
|
|
// use Anax\Route\Exception\InternalErrorException;
|
8
|
|
|
/**
|
9
|
|
|
* A sample controller to show how a controller class can be implemented.
|
10
|
|
|
*/
|
11
|
|
|
class weatherApiController implements ContainerInjectableInterface
|
12
|
|
|
{
|
13
|
|
|
use ContainerInjectableTrait;
|
14
|
|
|
/**
|
15
|
|
|
* This is the index method action, it handles:
|
16
|
|
|
* ANY METHOD mountpoint
|
17
|
|
|
*
|
18
|
|
|
* @return array
|
19
|
|
|
*/
|
20
|
3 |
|
public function indexAction() : array
|
21
|
|
|
{
|
22
|
3 |
|
$weather = $this->di->get("weather");
|
23
|
3 |
|
$search = $this->di->request->getGet("search");
|
|
|
|
|
24
|
3 |
|
$when = $this->di->request->getGet("when") ?? "future";
|
25
|
|
|
|
26
|
3 |
|
$ip = $this->di->request->getGet("search");
|
27
|
3 |
|
$validator = new \Anax\Model\ipValidation;
|
28
|
3 |
|
$res = $validator->toJson($ip);
|
29
|
|
|
|
30
|
3 |
|
if ($res["valid"] == "valid IP"){
|
31
|
1 |
|
$locationInfo = $weather->getLocationData($res["city"]);
|
32
|
|
|
} else {
|
33
|
2 |
|
if (!isset($search)){
|
34
|
|
|
$locationInfo = $weather->getLocationData("");
|
35
|
|
|
} else{
|
36
|
2 |
|
$locationInfo = $weather->getLocationData($search);
|
37
|
|
|
}
|
38
|
|
|
}
|
39
|
3 |
|
$lon = $locationInfo["lon"] ?? null;
|
40
|
3 |
|
$lat = $locationInfo["lat"] ?? null;
|
41
|
3 |
|
if (!isset($locationInfo["error"])){
|
42
|
|
|
|
43
|
|
|
$weatherInfo = $weather->getWeatherMultiCurl($when,$lon,$lat);
|
44
|
|
|
} else {
|
45
|
3 |
|
$weatherInfo = ["error" => "that is not a city"];
|
46
|
|
|
}
|
47
|
|
|
$data = [
|
48
|
3 |
|
"location" => $search ?? null,
|
49
|
3 |
|
"lon" => $lon ?? null,
|
50
|
3 |
|
"lat" => $lat ?? null,
|
51
|
3 |
|
"weatherInfo" => $weatherInfo,
|
52
|
|
|
];
|
53
|
3 |
|
return [$data];
|
54
|
|
|
}
|
55
|
|
|
}
|
56
|
|
|
|