|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EVB\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Controller for Weather API. |
|
10
|
|
|
* |
|
11
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
12
|
|
|
*/ |
|
13
|
|
|
class JsonController extends PageController implements ContainerInjectableInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use ContainerInjectableTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Controller action for showing documentation |
|
19
|
|
|
* as a web page. |
|
20
|
|
|
* |
|
21
|
|
|
* GET |
|
22
|
|
|
* |
|
23
|
|
|
* @return object |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function docActionGet() : object |
|
26
|
|
|
{ |
|
27
|
1 |
|
$page = $this->di->get("page"); |
|
28
|
|
|
|
|
29
|
1 |
|
$page->add("weather/apiDocumentation"); |
|
30
|
|
|
|
|
31
|
1 |
|
return $page->render([ |
|
32
|
1 |
|
"title" => "API-dokumentation | Väder" |
|
33
|
|
|
]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Helper method for rendering the response. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $ip |
|
40
|
|
|
* @param array $result |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function renderPage(array $search, array $result, string $mapLink, string $error) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$response = []; |
|
46
|
|
|
|
|
47
|
2 |
|
$response["search"] = $search; |
|
48
|
|
|
|
|
49
|
2 |
|
if ($error) { |
|
50
|
1 |
|
$response["error"] = $error; |
|
51
|
1 |
|
} else if (!empty($result)) { |
|
52
|
1 |
|
$response["map"] = $mapLink; |
|
53
|
|
|
|
|
54
|
1 |
|
$forecast = []; |
|
55
|
|
|
|
|
56
|
1 |
|
$forecast["currently"] = |
|
57
|
1 |
|
$result[0]["currently"]["summary"] . |
|
58
|
1 |
|
" med " . |
|
59
|
1 |
|
$result[0]["currently"]["precipProbability"] * 100 . "%" . |
|
60
|
1 |
|
" risk for nederbörd och en temperatur på " . |
|
61
|
1 |
|
$result[0]["currently"]["temperature"] . " grader," . |
|
62
|
1 |
|
" känns som " . |
|
63
|
1 |
|
$result[0]["currently"]["apparentTemperature"] . " grader." . |
|
64
|
1 |
|
" Vindhastighet " . |
|
65
|
1 |
|
$result[0]["currently"]["windSpeed"] . " m/s" . |
|
66
|
1 |
|
" upp till " . |
|
67
|
1 |
|
$result[0]["currently"]["windGust"] . " m/s i byarna."; |
|
68
|
|
|
|
|
69
|
1 |
|
$forecast["comingDays"]["summary"] = $result[0]["hourly"]["summary"]; |
|
70
|
|
|
|
|
71
|
1 |
|
$forecast["comingDays"]["hours"] = []; |
|
72
|
1 |
|
foreach ($result[0]["hourly"]["data"] as $hour) { |
|
73
|
1 |
|
$forecast["comingDays"]["hours"][] = [ |
|
74
|
1 |
|
"time" => \date("H:i", $hour["time"]), |
|
75
|
1 |
|
"summary" => $hour["summary"], |
|
76
|
1 |
|
"precipProb" => $hour["precipProbability"], |
|
77
|
1 |
|
"temp" => $hour["temperature"], |
|
78
|
1 |
|
"apparentTemp" => $hour["apparentTemperature"], |
|
79
|
1 |
|
"windSpeed" => $hour["windSpeed"], |
|
80
|
1 |
|
"windGust" => $hour["windGust"] |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$forecast["comingWeek"]["summary"] = $result[0]["daily"]["summary"]; |
|
85
|
|
|
|
|
86
|
1 |
|
$forecast["comingWeek"]["days"] = []; |
|
87
|
1 |
|
foreach ($result[0]["daily"]["data"] as $day) { |
|
88
|
1 |
|
$forecast["comingWeek"]["days"][] = [ |
|
89
|
1 |
|
"day" => \date("D", $day["time"]), |
|
90
|
1 |
|
"summary" => $day["summary"], |
|
91
|
1 |
|
"precipProb" => $day["precipProbability"], |
|
92
|
1 |
|
"tempMin" => $day["temperatureMin"], |
|
93
|
1 |
|
"tempMax" => $day["temperatureMax"], |
|
94
|
1 |
|
"apparentTempMin" => $day["apparentTemperatureMin"], |
|
95
|
1 |
|
"apparentTempMax" => $day["apparentTemperatureMax"], |
|
96
|
1 |
|
"windSpeed" => $day["windSpeed"], |
|
97
|
1 |
|
"windGust" => $day["windGust"] |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$historical = []; |
|
102
|
|
|
|
|
103
|
1 |
|
foreach (\array_slice($result, 1) as $day) { |
|
104
|
1 |
|
$day = $day["daily"]["data"][0]; |
|
105
|
1 |
|
$historical[] = [ |
|
106
|
1 |
|
"day" => \date("Y-m-d", $day["time"]), |
|
107
|
1 |
|
"summary" => $day["summary"], |
|
108
|
1 |
|
"precipProb" => $day["precipProbability"], |
|
109
|
1 |
|
"tempMin" => $day["temperatureMin"], |
|
110
|
1 |
|
"tempMax" => $day["temperatureMax"], |
|
111
|
1 |
|
"apparentTempMin" => $day["apparentTemperatureMin"], |
|
112
|
1 |
|
"apparentTempMax" => $day["apparentTemperatureMax"], |
|
113
|
1 |
|
"windSpeed" => $day["windSpeed"], |
|
114
|
1 |
|
"windGust" => $day["windGust"] |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
1 |
|
$response["forecast"] = $forecast; |
|
120
|
1 |
|
$response["historical"] = $historical; |
|
121
|
|
|
|
|
122
|
1 |
|
$response["poweredBy"] = "https://darksky.net/poweredby"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
return [$response]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|