|
1
|
|
|
<?php |
|
2
|
|
|
namespace Oliver\DarkSky; |
|
3
|
|
|
|
|
4
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
5
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
6
|
|
|
|
|
7
|
|
|
use Oliver\Weather\WeatherServiceInterface; |
|
8
|
|
|
use Oliver\DarkSky\Exception\InvalidLocationException; |
|
9
|
|
|
|
|
10
|
|
|
use Oliver\Curl\CurlInterface; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class DarkSky implements ContainerInjectableInterface, WeatherServiceInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use ContainerInjectableTrait; |
|
19
|
|
|
|
|
20
|
|
|
private $baseUrl; |
|
21
|
|
|
private $apiKey; |
|
22
|
|
|
private $curl; |
|
23
|
|
|
private $coordinates; |
|
24
|
|
|
|
|
25
|
|
|
private $icons = [ |
|
26
|
|
|
"clear-day" => "☀️", |
|
27
|
|
|
"clear-night" => "🌝", |
|
28
|
|
|
"rain" => "🌧", |
|
29
|
|
|
"snow" => "❄️", |
|
30
|
|
|
"sleet" => "🌨", |
|
31
|
|
|
"wind" => "💨", |
|
32
|
|
|
"fog" => "🌫", |
|
33
|
|
|
"cloudy" => "☁️", |
|
34
|
|
|
"partly-cloudy-day" => "🌤", |
|
35
|
|
|
"partly-cloudy-night" => "" |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
12 |
|
public function __construct(CurlInterface $curl) |
|
39
|
|
|
{ |
|
40
|
12 |
|
$this->curl = $curl; |
|
41
|
12 |
|
} |
|
42
|
|
|
|
|
43
|
12 |
|
public function configure(array $config) |
|
44
|
|
|
{ |
|
45
|
12 |
|
$this->baseUrl = $config["baseUrl"]; |
|
46
|
12 |
|
$this->apiKey = $config["apiKey"]; |
|
47
|
12 |
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function formatDate(int $daysBack) : string |
|
50
|
|
|
{ |
|
51
|
2 |
|
$day = time() - ($daysBack * 24 * 60 * 60); |
|
52
|
2 |
|
$date = (date("Y-m-dT12:00:00", $day)); |
|
53
|
2 |
|
$date = str_replace("U", "", $date); |
|
54
|
2 |
|
$date = str_replace("C", "", $date); |
|
55
|
2 |
|
return $date; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
public function addIcons($daily) : array |
|
59
|
|
|
{ |
|
60
|
5 |
|
if (array_key_exists("icon", $daily)) { |
|
61
|
3 |
|
$daily["icon"] = $this->icons[$daily["icon"]]; |
|
62
|
|
|
} |
|
63
|
5 |
|
$data = []; |
|
64
|
5 |
|
foreach ($daily["data"] as $day) { |
|
65
|
5 |
|
$day["icon"] = $this->icons[$day["icon"]]; |
|
66
|
5 |
|
$data[] = $day; |
|
67
|
|
|
} |
|
68
|
5 |
|
$daily["data"] = $data; |
|
69
|
5 |
|
return $daily; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
public function validateResult($result) |
|
73
|
|
|
{ |
|
74
|
6 |
|
if (array_key_exists("error", $result)) { |
|
75
|
1 |
|
throw new InvalidLocationException("Platsen hittades inte.<br>Latitud: <b>{$this->coordinates['lat']}</b><br>Longitud: <b>{$this->coordinates['lat']}</b>"); |
|
76
|
|
|
} |
|
77
|
5 |
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function history($coordinates, $daysBack) : array |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->coordinates = $coordinates; |
|
82
|
2 |
|
$urls = []; |
|
83
|
|
|
|
|
84
|
2 |
|
for ($i=0; $i < $daysBack; $i++) { |
|
85
|
2 |
|
$date = $this->formatDate($i); |
|
86
|
2 |
|
$urls[] = "$this->baseUrl/$this->apiKey/{$coordinates['lat']},{$coordinates['long']},$date?units=si&lang=sv"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$result = $this->curl->fetchData($urls); |
|
90
|
|
|
|
|
91
|
2 |
|
foreach ($result as $key => $day) { |
|
92
|
2 |
|
$this->validateResult($day); |
|
93
|
2 |
|
$result[$key]["daily"] = $this->addIcons($day["daily"]); |
|
94
|
|
|
} |
|
95
|
2 |
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
3 |
|
public function forecast($coordinates) : array |
|
100
|
|
|
{ |
|
101
|
3 |
|
$this->coordinates = $coordinates; |
|
102
|
3 |
|
$url = "$this->baseUrl/$this->apiKey/{$coordinates['lat']},{$coordinates['long']}?units=si&lang=sv"; |
|
103
|
3 |
|
$result = $this->curl->fetchData([$url]); |
|
104
|
|
|
|
|
105
|
3 |
|
foreach ($result as $key => $day) { |
|
106
|
3 |
|
$this->validateResult($day); |
|
107
|
3 |
|
$result[$key]["daily"] = $this->addIcons($day["daily"]); |
|
108
|
|
|
} |
|
109
|
3 |
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|