1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Osln\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
10
|
|
|
*/ |
11
|
|
|
class WeatherJsonController implements ContainerInjectableInterface |
12
|
|
|
{ |
13
|
|
|
use ContainerInjectableTrait; |
14
|
3 |
|
public function initialize() : void |
15
|
|
|
{ |
16
|
3 |
|
$this->config = new LoadConfig(); |
|
|
|
|
17
|
3 |
|
$this->ipvalidate = new Ipvalidate(); |
|
|
|
|
18
|
3 |
|
} |
19
|
|
|
/** |
20
|
|
|
* This is the index method action, it handles: |
21
|
|
|
* ANY METHOD mountpoint |
22
|
|
|
* ANY METHOD mountpoint/ |
23
|
|
|
* ANY METHOD mountpoint/index |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
1 |
|
public function indexAction() : object |
28
|
|
|
{ |
29
|
1 |
|
$page = $this->di->get("page"); |
30
|
1 |
|
$session = $this->di->get("session"); |
31
|
|
|
|
32
|
|
|
$data = [ |
33
|
1 |
|
"title" => "Väder", |
34
|
1 |
|
"forecast" => $session->has("forecast") ? $session->get("forecast") : "", |
35
|
1 |
|
"month" => $session->has("month") ? $session->get("month") : "", |
36
|
1 |
|
"error" => $session->has("error") ? $session->get("error") : "", |
37
|
1 |
|
"location" => "", |
38
|
|
|
]; |
39
|
1 |
|
$page->add("osln/weather/defaultJson", $data); |
40
|
1 |
|
$session->delete("error"); |
41
|
1 |
|
return $page->render(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function responseActionGet() |
45
|
|
|
{ |
46
|
1 |
|
$search = $this->di->request->getGet("location"); |
|
|
|
|
47
|
1 |
|
$session = $this->di->get("session"); |
48
|
1 |
|
$session->delete("month"); |
49
|
1 |
|
$url = $this->ipvalidate->getUrl($search); |
50
|
|
|
|
51
|
1 |
|
$data = $this->di->curl->fetch($url); |
|
|
|
|
52
|
|
|
// $forecast = null; |
53
|
|
|
|
54
|
1 |
|
if (isset($data["latitude"])) { |
55
|
1 |
|
$forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]); |
|
|
|
|
56
|
1 |
|
} elseif (isset($data[0]["lat"])) { |
57
|
1 |
|
$forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]); |
58
|
|
|
} else { |
59
|
1 |
|
return [["Could not find this location"]]; |
60
|
|
|
} |
61
|
1 |
|
$data = []; |
62
|
1 |
|
$i = 1; |
63
|
|
|
// var_dump($forecast); |
64
|
1 |
|
foreach ($forecast[0]["daily"]["data"] as $day) { |
65
|
|
|
$newDay = [ |
66
|
1 |
|
"day" => $i++, |
67
|
1 |
|
"summary" => $day["summary"], |
68
|
1 |
|
"temp" => $day["temperatureHigh"], |
69
|
1 |
|
"wind" => $day["windSpeed"]]; |
70
|
1 |
|
$data[] = $newDay; |
71
|
|
|
} |
72
|
1 |
|
return [$data]; |
73
|
|
|
} |
74
|
1 |
|
public function previousActionGet() |
75
|
|
|
{ |
76
|
1 |
|
$search = $this->di->request->getGet("location"); |
|
|
|
|
77
|
1 |
|
$url = $this->ipvalidate->getUrl($search); |
78
|
|
|
|
79
|
1 |
|
$data = $this->di->curl->fetch($url); |
|
|
|
|
80
|
1 |
|
$forecast = null; |
81
|
1 |
|
if (isset($data["latitude"])) { |
82
|
1 |
|
$forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]); |
|
|
|
|
83
|
1 |
|
$month = $this->di->weather->getLastMonth($data["latitude"], $data["longitude"]); |
84
|
1 |
|
} elseif (isset($data[0]["lat"])) { |
85
|
1 |
|
$forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]); |
86
|
1 |
|
$month = $this->di->weather->getLastMonth($data[0]["lat"], $data[0]["lon"]); |
87
|
|
|
} |
88
|
1 |
|
$data = []; |
89
|
1 |
|
$i = -1; |
90
|
1 |
|
foreach ($month[0] as $day) { |
|
|
|
|
91
|
|
|
$newDay = [ |
92
|
1 |
|
"day" => $i++, |
93
|
1 |
|
"summary" => $day["daily"]["data"][0]["summary"], |
94
|
1 |
|
"temp" => $day["daily"]["data"][0]["temperatureHigh"], |
95
|
1 |
|
"wind" => $day["daily"]["data"][0]["windSpeed"] |
96
|
|
|
]; |
97
|
1 |
|
$data[] = $newDay; |
98
|
|
|
} |
99
|
1 |
|
return [$data]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|