1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\WeatherService; |
4
|
|
|
|
5
|
|
|
use Anax\LocationProvider\LocationProviderInterface; |
6
|
|
|
|
7
|
|
|
class DarkSky |
8
|
|
|
{ |
9
|
|
|
private $locationProvider; |
10
|
|
|
private $lat; |
11
|
|
|
private $long; |
12
|
|
|
private $apiKey; |
13
|
|
|
private $curl; |
14
|
|
|
private $city; |
15
|
|
|
private $country; |
16
|
|
|
|
17
|
9 |
|
public function __construct(LocationProviderInterface $locationProvider, $curl, $cfg) |
18
|
|
|
{ |
19
|
9 |
|
$this->locationProvider = $locationProvider; |
20
|
9 |
|
$this->curl = $curl; |
21
|
9 |
|
$keys = $cfg->load("weatherService.php"); |
22
|
9 |
|
$this->apiKey = $keys["config"]["apiKey"]; |
23
|
9 |
|
} |
24
|
|
|
|
25
|
7 |
|
public function setLocation($ip) |
26
|
|
|
{ |
27
|
7 |
|
$locationProvider = $this->locationProvider; |
28
|
|
|
|
29
|
7 |
|
$locationProvider->setLocation($ip); |
30
|
7 |
|
$this->lat = $locationProvider->getLat(); |
31
|
7 |
|
$this->long = $locationProvider->getLong(); |
32
|
7 |
|
$this->city = $locationProvider->getCity(); |
33
|
7 |
|
$this->country = $locationProvider->getCountry(); |
34
|
7 |
|
} |
35
|
|
|
|
36
|
3 |
|
public function getForecast() |
37
|
|
|
{ |
38
|
3 |
|
$url = "https://api.darksky.net/forecast/" . $this->apiKey . "/" . $this->lat . "," . $this->long . "?units=si"; |
39
|
3 |
|
$res = $this->curl->get($url); |
40
|
|
|
|
41
|
3 |
|
if (isset($res->code) && $res->code === 400) { |
42
|
1 |
|
return ["error" => $res->error]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$data = [ |
46
|
2 |
|
"lat" => $this->lat, |
47
|
2 |
|
"long" => $this->long, |
48
|
2 |
|
"city" => $this->city, |
49
|
2 |
|
"country" => $this->country, |
50
|
2 |
|
"currentTemp" => $res->currently->temperature, |
51
|
2 |
|
"currentIcon" => $this->translateIcon($res->currently->icon), |
52
|
2 |
|
"currentSum" => $res->currently->summary, |
53
|
|
|
"dailyTempHigh" => [ |
54
|
2 |
|
$res->daily->data[0]->temperatureHigh, |
55
|
2 |
|
$res->daily->data[1]->temperatureHigh, |
56
|
2 |
|
$res->daily->data[2]->temperatureHigh, |
57
|
2 |
|
$res->daily->data[3]->temperatureHigh |
58
|
|
|
], |
59
|
|
|
"dailyTempLow" => [ |
60
|
2 |
|
$res->daily->data[0]->temperatureLow, |
61
|
2 |
|
$res->daily->data[1]->temperatureLow, |
62
|
2 |
|
$res->daily->data[2]->temperatureLow, |
63
|
2 |
|
$res->daily->data[3]->temperatureLow |
64
|
|
|
], |
65
|
|
|
"dailyIcon" => [ |
66
|
2 |
|
$this->translateIcon($res->daily->data[0]->icon), |
67
|
2 |
|
$this->translateIcon($res->daily->data[1]->icon), |
68
|
2 |
|
$this->translateIcon($res->daily->data[2]->icon), |
69
|
2 |
|
$this->translateIcon($res->daily->data[3]->icon) |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
|
73
|
2 |
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function getOldCast() |
77
|
|
|
{ |
78
|
4 |
|
$baseUrl = "https://api.darksky.net/forecast/"; |
79
|
4 |
|
$today = date("Y-m-d H:i:s"); |
80
|
4 |
|
$urls = []; |
81
|
4 |
|
$data = []; |
82
|
|
|
|
83
|
|
|
|
84
|
4 |
|
for ($i=1; $i < 31; $i++) { |
85
|
4 |
|
$old = date_create($today)->modify(-$i . ' days')->format('Y-m-d'); |
86
|
4 |
|
$fetchURL = $baseUrl . $this->apiKey . "/" . $this->lat . "," . $this->long . "," . $old . "T12:00:00" . "?units=si"; |
87
|
4 |
|
array_push($urls, $fetchURL); |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
$res = $this->curl->getMulti($urls); |
91
|
|
|
|
92
|
4 |
|
if (isset($res[0]->code) && $res[0]->code === 400) { |
93
|
2 |
|
return ["error" => $res[0]->error]; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
foreach ($res as $i => $forecast) { |
97
|
2 |
|
$data["history"][$i] = [ |
98
|
2 |
|
"date" => date("D, d M", $res[$i]->currently->time), |
99
|
2 |
|
"currentTemp" => $res[$i]->currently->temperature, |
100
|
2 |
|
"currentIcon" => $this->translateIcon($res[$i]->currently->icon), |
101
|
2 |
|
"currentSum" => $res[$i]->currently->summary, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
$data["city"] = $this->city; |
106
|
2 |
|
$data["lat"] = $this->lat; |
107
|
2 |
|
$data["long"] = $this->long; |
108
|
2 |
|
$data["country"] = $this->country; |
109
|
|
|
|
110
|
2 |
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
5 |
|
public function translateIcon($icon) |
114
|
|
|
{ |
115
|
5 |
|
switch ($icon) { |
116
|
5 |
|
case 'clear-night': |
117
|
1 |
|
return "moon"; |
118
|
5 |
|
case 'rain': |
119
|
1 |
|
return "cloud-rain"; |
120
|
5 |
|
case 'clear-day': |
121
|
1 |
|
return "sun"; |
122
|
5 |
|
case 'snow': |
123
|
1 |
|
return "snowflake"; |
124
|
5 |
|
case 'sleet': |
125
|
1 |
|
return "cloud-sleet"; |
126
|
5 |
|
case 'cloudy': |
127
|
1 |
|
return "cloud"; |
128
|
5 |
|
case 'partly-cloudy-day': |
129
|
1 |
|
return "cloud-sun"; |
130
|
|
|
default: |
131
|
5 |
|
return $icon; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|