1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jenel\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* WeatherController |
10
|
|
|
* get weather forecast from location. |
11
|
|
|
* |
12
|
|
|
* uses $di "weather" -> WeatherModel |
13
|
|
|
* uses $di "curl -> curl functions |
14
|
|
|
*/ |
15
|
|
|
class WeatherApiController implements ContainerInjectableInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerInjectableTrait; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** Declare variables */ |
21
|
|
|
private $WeatherModel; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The initialize the class. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
3 |
|
public function initialize() : void |
30
|
|
|
{ |
31
|
3 |
|
$this->WeatherModel = $this->di->get("weather"); |
32
|
3 |
|
$this->WeatherModel->setCurl($this->di->get("curl")); |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return json for method get |
37
|
|
|
* GET mountpoint /json?=<location> |
38
|
|
|
* @param location |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
2 |
|
public function jsonActionGet() : array |
43
|
|
|
{ |
44
|
2 |
|
$request = $this->di->get("request"); |
45
|
2 |
|
$weatherModel = $this->WeatherModel; |
46
|
|
|
|
47
|
2 |
|
$location = $request->getGet("location", ""); |
48
|
2 |
|
$radio = $request->getGet("forecast", "week"); |
49
|
2 |
|
$geo = $weatherModel->getGeo($location); |
|
|
|
|
50
|
|
|
|
51
|
2 |
|
if (empty($location)) { |
52
|
|
|
$data = [ |
53
|
1 |
|
"message" => "No location provided. Please try again." |
54
|
|
|
]; |
55
|
1 |
|
return [$data, 400]; |
56
|
|
|
} else { |
57
|
1 |
|
$data = $weatherModel->getAll($radio); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return [$data, 200]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Try to access a forbidden resource. |
66
|
|
|
* ANY mountpoint/forbidden |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
1 |
|
public function forbiddenAction() : array |
71
|
|
|
{ |
72
|
|
|
// Deal with the action and return a response. |
73
|
|
|
$json = [ |
74
|
1 |
|
"message" => __METHOD__ . ", forbidden to access.", |
75
|
|
|
]; |
76
|
1 |
|
return [$json, 403]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|