|
1
|
|
|
<?php |
|
2
|
|
|
namespace Anax\Weather; |
|
3
|
|
|
|
|
4
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
5
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A model class retrievieng data from an external server. |
|
9
|
|
|
*/ |
|
10
|
|
|
class WeatherModel implements ContainerInjectableInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use ContainerInjectableTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var object For curl requests |
|
16
|
|
|
* @var string Api key |
|
17
|
|
|
* @var string Api base url |
|
18
|
|
|
* @var string Api options |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $curl; |
|
22
|
|
|
protected $apiKey; |
|
23
|
|
|
protected $baseURL; |
|
24
|
|
|
protected $options; |
|
25
|
|
|
/** |
|
26
|
|
|
* Set Curl object for requests |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
10 |
|
public function setCurl(object $curl) |
|
31
|
|
|
{ |
|
32
|
10 |
|
$this->curl = $curl; |
|
33
|
10 |
|
} |
|
34
|
|
|
/** |
|
35
|
|
|
* Set Apikey |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function setKey(string $key) |
|
40
|
|
|
{ |
|
41
|
4 |
|
$this->apiKey = $key; |
|
42
|
4 |
|
} |
|
43
|
|
|
/** |
|
44
|
|
|
* Set baseUrl |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function setBaseURL(string $baseURL) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$this->baseURL = $baseURL; |
|
51
|
4 |
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* Set options |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function setOptions(string $options) |
|
58
|
|
|
{ |
|
59
|
4 |
|
$this->options = $options; |
|
60
|
4 |
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* Get latitude, longitude from location |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
5 |
|
public function convertLocation(string $location) : array |
|
67
|
|
|
{ |
|
68
|
5 |
|
$url = "https://nominatim.openstreetmap.org/?addressdetails=1&q={$location}&format=json&[email protected]&limit=1"; |
|
69
|
5 |
|
$locationInfo = $this->curl->getCurl($url); |
|
70
|
5 |
|
if (!empty($locationInfo)) { |
|
71
|
3 |
|
$res = []; |
|
72
|
3 |
|
$res["match"] = true; |
|
73
|
3 |
|
$res["latitude"] = $locationInfo[0]["lat"]; |
|
74
|
3 |
|
$res["longitude"] = $locationInfo[0]["lon"]; |
|
75
|
3 |
|
$res["openstreetmap_link"] = "https://www.openstreetmap.org/#map=10/" . $res['latitude'] . "/" . $res['longitude']; |
|
76
|
3 |
|
$res["location_summary"] = $locationInfo[0]["display_name"] ?? null; |
|
77
|
3 |
|
return $res; |
|
78
|
|
|
} |
|
79
|
|
|
return [ |
|
80
|
2 |
|
"match" => false, |
|
81
|
|
|
"message" => "Could not find any matching location" |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
/** |
|
85
|
|
|
* Get weather |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getWeather(int $lat, $long) : array |
|
90
|
|
|
{ |
|
91
|
1 |
|
$url = $this->baseURL . $this->apiKey . "/" . $lat . "," . $long . $this->options; |
|
92
|
1 |
|
$weatherInfo = $this->curl->getCurl($url); |
|
93
|
1 |
|
$weatherInfo["match"] = (!empty($weatherInfo)) ? true : false; |
|
94
|
1 |
|
return $weatherInfo; |
|
95
|
|
|
} |
|
96
|
|
|
/** |
|
97
|
|
|
* Get weather mulitcurl |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getWeatherMulti(int $lat, $long, $days) : array |
|
102
|
|
|
{ |
|
103
|
2 |
|
$url = $this->baseURL . $this->apiKey . "/" . $lat . "," . $long; |
|
104
|
2 |
|
$allRequests = []; |
|
105
|
2 |
|
if ($days <= 0 || $days > 30) { |
|
106
|
|
|
return [ |
|
107
|
1 |
|
"match" => false, |
|
108
|
|
|
"message" => "Unvalid amount of days." |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
1 |
|
for ($i=1; $i < $days + 1; $i++) { |
|
112
|
1 |
|
$time = time() - ($i * 60 * 60 * 24); |
|
113
|
1 |
|
$allRequests[] = $url . "," . $time . $this->options; |
|
114
|
|
|
} |
|
115
|
1 |
|
$formatedResponse = $this->formatResponse($this->curl->getMultiCurl($allRequests)); |
|
116
|
1 |
|
return $formatedResponse; |
|
117
|
|
|
} |
|
118
|
|
|
/** |
|
119
|
|
|
* Format weather response |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function formatResponse(array $weatherResponse) : array |
|
124
|
|
|
{ |
|
125
|
|
|
|
|
126
|
3 |
|
$newFormat = []; |
|
127
|
3 |
|
if (!empty($weatherResponse)) { |
|
128
|
3 |
|
$newFormat["match"] = true; |
|
129
|
3 |
|
foreach ($weatherResponse as $key => $row) { |
|
130
|
3 |
|
$newFormat["data"][] = $row["daily"]["data"][0]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
3 |
|
return $newFormat; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|