1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\WeatherAPI; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
14
|
|
|
* The controller will be injected with $app if implementing the interface |
15
|
|
|
* AppInjectableInterface, like this sample class does. |
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
17
|
|
|
* requests for that mount point. |
18
|
|
|
* |
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
20
|
|
|
*/ |
21
|
|
|
class WeatherJSONController implements ContainerInjectableInterface |
22
|
|
|
{ |
23
|
|
|
use ContainerInjectableTrait; |
24
|
|
|
|
25
|
|
|
public $model; |
26
|
|
|
|
27
|
3 |
|
public function initialize() |
28
|
|
|
{ |
29
|
3 |
|
$this->model = new WeatherModel(); |
30
|
3 |
|
} |
31
|
|
|
/** |
32
|
|
|
* JSON form |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
1 |
|
public function indexAction() : object |
36
|
|
|
{ |
37
|
1 |
|
$page = $this->di->get("page"); |
38
|
1 |
|
$weather = $this->di->get("weather"); |
39
|
1 |
|
$message = $weather->welcomeMsg(); |
40
|
1 |
|
$title = "Väder API"; |
41
|
|
|
$data = [ |
42
|
1 |
|
"message" => $message["message"], |
43
|
|
|
]; |
44
|
|
|
|
45
|
1 |
|
$page->add("weatherapi/weather-json", $data); |
46
|
|
|
|
47
|
1 |
|
return $page->render([ |
48
|
1 |
|
"title" => $title, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
/** |
52
|
|
|
* JSON API |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
2 |
|
public function fetchActionGet() : array |
56
|
|
|
{ |
57
|
2 |
|
$session = $this->di->get("session"); |
58
|
2 |
|
$request = $this->di->get("request"); |
59
|
|
|
|
60
|
2 |
|
$searchReq = $request->getGet("searchReq") ?? ""; |
61
|
2 |
|
$date = $request->getGet("date"); |
62
|
|
|
|
63
|
2 |
|
$this->model->getWeatherData($session, $searchReq, $date); |
64
|
|
|
$json = [ |
65
|
2 |
|
"address" => $session->get("address"), |
66
|
2 |
|
"weather_data" => $session->get("jsonData") |
67
|
|
|
]; |
68
|
|
|
|
69
|
2 |
|
$session->set("jsonData", null); |
70
|
2 |
|
$session->set("address", null); |
71
|
|
|
|
72
|
2 |
|
return [$json]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|