1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Anax\Models\WeatherForecast; |
8
|
|
|
use Anax\Models\GetGeo; |
9
|
|
|
|
10
|
|
|
class WeatherJSONController implements ContainerInjectableInterface |
11
|
|
|
{ |
12
|
|
|
use ContainerInjectableTrait; |
13
|
|
|
|
14
|
|
|
public function checkWeatherRestAction() |
15
|
|
|
{ |
16
|
|
|
$location = $_GET["location"]; |
17
|
|
|
|
18
|
|
|
$weather = $this->di->get("weather"); |
19
|
|
|
$type = $this->di->get("request")->getGet("type"); |
20
|
|
|
$geo = new GetGeo(); |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
if ($type == 'prognosAPI') { |
24
|
|
|
if (strpos($location, ",") == true) { |
|
|
|
|
25
|
|
|
$exploded = explode(",", $location); |
26
|
|
|
$data = $weather->checkWeather($exploded[0], $exploded[1]); |
27
|
|
|
} else { |
28
|
|
|
$res = $geo->getGeo($location); |
29
|
|
|
if ($res["longitude"] !== null) { |
30
|
|
|
$data = $weather->checkWeather($res["latitude"], $res["longitude"]); |
31
|
|
|
} else { |
32
|
|
|
$error = "Felaktig input, prova igen!"; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} elseif ($type == 'historyAPI') { |
36
|
|
|
if (strpos($location, ",") == true) { |
|
|
|
|
37
|
|
|
$exploded = explode(",", $location); |
38
|
|
|
$data = $weather->checkHistory($exploded[0], $exploded[1]); |
39
|
|
|
} else { |
40
|
|
|
$res = $geo->getGeo($location); |
41
|
|
|
if ($res["longitude"] !== null) { |
42
|
|
|
$data = $weather->checkHistory($res["latitude"], $res["longitude"]); |
43
|
|
|
} else { |
44
|
|
|
$error = "Felaktig input, prova igen!"; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
$data = [ |
51
|
|
|
"forecast" => $data ?? null, |
52
|
|
|
"error" => $error ?? null |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
json_encode($data, true); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return [[ $data ]]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|