1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Anax\Models\WeatherApi; |
8
|
|
|
|
9
|
|
|
// use Anax\Models\IpValidator; |
10
|
|
|
// use Anax\Models\GeoApi; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controllerclass for the JSON-return of weather forecast |
14
|
|
|
*/ |
15
|
|
|
class WeatherToJSONController implements ContainerInjectableInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerInjectableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* based on ip address or coordinates for a location |
21
|
|
|
* returning the forecast for 7 days |
22
|
|
|
*/ |
23
|
|
|
public function searchAction() |
24
|
|
|
{ |
25
|
|
|
$search = $_GET["location"]; |
26
|
|
|
$type = $this->di->get("request")->getGet("type"); |
27
|
|
|
|
28
|
|
|
$weatherObj = $this->di->get("weatherapi"); |
29
|
|
|
$geoObj = $this->di->get("geoapi"); |
30
|
|
|
|
31
|
|
|
if (strpos($search, ",") == true) { |
|
|
|
|
32
|
|
|
$split = explode(",", $search); |
33
|
|
|
if (!ctype_alpha($split[0]) && !ctype_alpha($split[1])) { |
34
|
|
|
$weatherObj->setCoordinates($split[0], $split[1]); |
35
|
|
|
$weather = ($type == "coming") ? ($weatherObj->comingWeather($split[0], $split[1])) : $weatherObj->pastWeather($split[0], $split[1]); |
36
|
|
|
} else { |
37
|
|
|
$weather = "Felaktig söksträng, försök igen."; |
38
|
|
|
} |
39
|
|
|
} else { |
40
|
|
|
$res = $geoObj->findGeoLocation($search); |
41
|
|
|
if ($res["longitude"] !== "-") { |
42
|
|
|
$weatherObj->setCoordinates($res["latitude"], $res["longitude"]); |
43
|
|
|
$weather = ($type == "coming") ? ($weatherObj->comingWeather($res["latitude"], $res["longitude"])) : $weatherObj->pastWeather($res["latitude"], $res["longitude"]); |
44
|
|
|
} else { |
45
|
|
|
$weather = "Felaktig söksträng, försök igen."; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$data = [ |
50
|
|
|
"weather" => $weather ?? null, |
51
|
|
|
"coordinates" => $weatherObj->getCoordinates() ?? null, |
52
|
|
|
"location" => $weatherObj->getLocation() ?? null |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
json_encode($data, true); |
|
|
|
|
57
|
|
|
|
58
|
|
|
return [[ $data ]]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|