1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KW\Models; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
class JsonWeather implements ContainerInjectableInterface |
9
|
|
|
{ |
10
|
|
|
use ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns weather info in json format |
15
|
|
|
* |
16
|
|
|
* @return json |
17
|
|
|
*/ |
18
|
2 |
|
public function jsonify($res) |
19
|
|
|
{ |
20
|
2 |
|
$days = []; |
21
|
|
|
$mapUrl = "https://www.openstreetmap.org/?mlat=" |
22
|
2 |
|
. $res["latitude"]. "&mlon=" |
23
|
2 |
|
. $res["longitude"] ."&zoom=12#map=12/" |
24
|
2 |
|
. $res["latitude"] ."/" |
25
|
2 |
|
. $res["longitude"]; |
26
|
|
|
|
27
|
2 |
|
$type = ($res["type"] == "past" ? "past mounth" : "future week"); |
28
|
|
|
|
29
|
|
|
$backgroundinfo = array( |
30
|
2 |
|
"tempus"=>$type, |
31
|
2 |
|
"country"=>$res["country"], |
32
|
2 |
|
"town"=>($res["town"] ?? "---"), |
33
|
2 |
|
"longitude"=>$res["longitude"], |
34
|
2 |
|
"latitude"=>$res["latitude"], |
35
|
2 |
|
"mapUrl"=>$mapUrl |
36
|
|
|
); |
37
|
|
|
|
38
|
2 |
|
if (isset($res["weatherresult"]->daily->data)) { |
39
|
1 |
|
foreach ($res["weatherresult"]->daily->data as $key => $value) { |
40
|
1 |
|
array_push($days, array("day"=>($key ?? "---"), |
41
|
1 |
|
"summary"=>($value->summary ?? "---"), |
42
|
1 |
|
"highesttemp"=>($value->temperatureHigh ?? "---"), |
43
|
1 |
|
"lowesttemp"=>($value->temperatureLow ?? "---"), |
44
|
1 |
|
"windspeed"=>($value->windSpeed ?? "---"),)); |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
1 |
|
array_push($days, "data för aktuell tid saknas!"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = [ |
51
|
2 |
|
"backgroundinfo"=>$backgroundinfo, |
52
|
2 |
|
"days"=>$days |
53
|
|
|
]; |
54
|
|
|
//$result = json_decode(json_encode($result)); |
55
|
|
|
|
56
|
2 |
|
return [$result]; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|