1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Weather. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Anax\Weather; |
8
|
|
|
|
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
use Anax\IpGeo\IpGeo; |
13
|
|
|
use DateInterval; |
14
|
|
|
use DateTime; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Showing off a standard class with methods and properties. |
18
|
|
|
*/ |
19
|
|
|
class Weather implements ContainerInjectableInterface |
20
|
|
|
{ |
21
|
|
|
use ContainerInjectableTrait; |
22
|
|
|
|
23
|
|
|
protected $apiKey; |
24
|
|
|
protected $ipGeo; |
25
|
|
|
protected $output; |
26
|
|
|
protected $curl; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor for object. |
30
|
|
|
* |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
|
34
|
20 |
|
public function __construct($di) |
35
|
|
|
{ |
36
|
20 |
|
$this->di = $di; |
37
|
20 |
|
$this->ipGeo = new IpGeo(); |
38
|
20 |
|
$this->output = [ |
39
|
|
|
"currently" => "", |
40
|
|
|
"daily" => "", |
41
|
|
|
"history" => "", |
42
|
|
|
"long" => "", |
43
|
|
|
"lat" => "", |
44
|
|
|
"country" => "", |
45
|
|
|
"city" => "", |
46
|
|
|
"map" => "", |
47
|
|
|
"embed" => "", |
48
|
|
|
"matched" => true, |
49
|
|
|
]; |
50
|
20 |
|
$allKeys = require ANAX_INSTALL_PATH . "/config/api_keys.php"; |
51
|
20 |
|
$this->apiKey = $allKeys["darkSky"]; |
52
|
20 |
|
$this->curl = $this->di->get("curl"); |
53
|
20 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get data. |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
|
60
|
8 |
|
public function getCity(string $lat, string $long) |
61
|
|
|
{ |
62
|
8 |
|
$url = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=$lat&lon=$long&[email protected]&limit=1"; |
63
|
8 |
|
$response = $this->curl->getData($url); |
64
|
8 |
|
if (isset($response["display_name"])) { |
65
|
6 |
|
$this->output["city"] = $response["display_name"]; |
66
|
|
|
} else { |
67
|
2 |
|
$this->output["matched"] = false; |
68
|
|
|
} |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get data. |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
|
|
|
76
|
4 |
|
public function getCoords(string $city) |
77
|
|
|
{ |
78
|
4 |
|
$url = "https://nominatim.openstreetmap.org/?format=json&q=$city&format=json&[email protected]&limit=1"; |
79
|
4 |
|
if ($this->curl->getData($url)) { |
80
|
3 |
|
$response = $this->curl->getData($url)[0]; |
81
|
|
|
return [ |
82
|
3 |
|
"long" => $response["lon"], |
83
|
3 |
|
"lat" => $response["lat"], |
84
|
|
|
]; |
85
|
|
|
} else { |
86
|
1 |
|
$this->output["matched"] = false; |
87
|
|
|
return [ |
88
|
1 |
|
"long" => "Missing", |
89
|
|
|
"lat" => "Missing", |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get data. |
96
|
|
|
* |
97
|
|
|
*/ |
98
|
|
|
|
99
|
5 |
|
public function getForecast(string $lat, string $long) |
100
|
|
|
{ |
101
|
5 |
|
$url = "https://api.darksky.net/forecast/$this->apiKey/$lat,$long?lang=sv&units=si&exclude=hourly,minutely,alerts,flags"; |
102
|
5 |
|
$response = $this->curl->getData($url); |
103
|
5 |
|
if (isset($response["currently"])) { |
104
|
4 |
|
$this->output["currently"] = $response["currently"]; |
105
|
4 |
|
$this->output["daily"] = $response["daily"]; |
106
|
4 |
|
$this->output["long"] = $response["longitude"]; |
107
|
4 |
|
$this->output["lat"] = $response["latitude"]; |
108
|
|
|
} else { |
109
|
1 |
|
$this->output["matched"] = false; |
110
|
|
|
} |
111
|
5 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get data. |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
|
118
|
3 |
|
public function getHistory(string $lat, string $long) |
119
|
|
|
{ |
120
|
3 |
|
$time = new DateTime(); |
121
|
3 |
|
$urls = array(); |
122
|
|
|
|
123
|
3 |
|
for ($i=1; $i < 30; $i++) { |
124
|
3 |
|
$unixTime = $time->getTimestamp(); |
125
|
3 |
|
$url = "https://api.darksky.net/forecast/$this->apiKey/$lat,$long,$unixTime?exclude=minutely,hourly,daily,alerts,flags&lang=sv&units=si"; |
126
|
3 |
|
$urls[$i] = $url; |
127
|
3 |
|
$time->sub(new DateInterval('P1D')); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
$response = $this->curl->getMultiData($urls); |
131
|
3 |
|
$out = []; |
132
|
3 |
|
if (isset($response[0]["error"])) { |
133
|
1 |
|
$this->output["matched"] = false; |
134
|
|
|
} else { |
135
|
2 |
|
foreach ($response as $day) { |
136
|
2 |
|
$out[] = $day["currently"]; |
137
|
|
|
} |
138
|
2 |
|
$this->output["history"] = $out; |
139
|
|
|
} |
140
|
3 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get data. |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
|
|
|
147
|
8 |
|
public function getWeather(string $lat, string $long, $searchType) |
148
|
|
|
{ |
149
|
8 |
|
$this->getCity($lat, $long); |
150
|
8 |
|
if ($searchType == "currently" || $searchType == "forecast") { |
151
|
5 |
|
$this->getForecast($lat, $long); |
152
|
3 |
|
} elseif ($searchType == "history") { |
153
|
3 |
|
$this->getHistory($lat, $long); |
154
|
|
|
} |
155
|
8 |
|
$this->output["embed"] = $this->ipGeo->createEmbedMap($long, $lat); |
156
|
|
|
|
157
|
8 |
|
return $this->output; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|