|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawaby88\OpenWeatherMap\Services\Support; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class CurrentWeather. |
|
7
|
|
|
*/ |
|
8
|
|
|
class CurrentWeather |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Weather object |
|
12
|
|
|
*/ |
|
13
|
|
|
public $weather; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Location object |
|
16
|
|
|
*/ |
|
17
|
|
|
public $location; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Sun object |
|
20
|
|
|
*/ |
|
21
|
|
|
public $sun; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Temperature object |
|
24
|
|
|
*/ |
|
25
|
|
|
public $temperature; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* CurrentWeather constructor. |
|
29
|
|
|
* Generate Weather, Location, Sun, and Temperature objects from given data. |
|
30
|
|
|
* @param $data |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($data) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->weather = $this->initWeather($data); |
|
35
|
|
|
$this->location = $this->initLocation($data); |
|
36
|
|
|
$this->sun = $this->initSun($data); |
|
37
|
|
|
$this->temperature = $this->initTemperature($data); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $data |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
private function initWeather($data): Weather |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($this->weather)) { |
|
47
|
|
|
return $this->weather; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$rain = []; |
|
51
|
|
|
$snow = []; |
|
52
|
|
|
if (isset($data->rain)) { |
|
53
|
|
|
$rainArray = (array) $data->rain; |
|
54
|
|
|
$rain[array_key_first($rainArray)] = reset($rainArray); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (isset($data->snow)) { |
|
58
|
|
|
$snowArray = (array) $data->snow; |
|
59
|
|
|
$snow[array_key_first($snowArray)] = reset($snowArray); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new Weather( |
|
63
|
|
|
$data->weather[0]->main, $data->weather[0]->description, $data->weather[0]->icon, $data->wind->speed, $data->wind->deg, $data->clouds->all ?? $data->clouds->today, $rain, $snow |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $data |
|
69
|
|
|
* @return Temperature |
|
70
|
|
|
*/ |
|
71
|
|
|
private function initTemperature($data): Temperature |
|
72
|
|
|
{ |
|
73
|
|
|
return isset($this->temperature) |
|
74
|
|
|
? $this->temperature |
|
75
|
|
|
: new Temperature($data->main->temp, $data->main->feels_like, $data->main->temp_min, $data->main->temp_max, $data->main->pressure, $data->main->humidity); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $data |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
private function initLocation($data): Location |
|
83
|
|
|
{ |
|
84
|
|
|
if (isset($this->location)) { |
|
85
|
|
|
return $this->location; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$lat = 0; |
|
89
|
|
|
$lon = 0; |
|
90
|
|
|
$country = isset($data->sys) |
|
91
|
|
|
? $data->sys->country |
|
92
|
|
|
: 'undefined'; |
|
93
|
|
|
if (isset($data->coord)) { |
|
94
|
|
|
$lat = $data->coord->lat ?? $data->coord->Lat; |
|
95
|
|
|
$lon = $data->coord->lon ?? $data->coord->Lon; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return new Location($data->name, $country, $lat, $lon); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $data |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
private function initSun($data): Sun |
|
106
|
|
|
{ |
|
107
|
|
|
return isset($this->sun) |
|
108
|
|
|
? $this->sun |
|
109
|
|
|
: new Sun($data->sys->sunrise ?? 0, $data->sys->sunset ?? 0, $data->timezone ?? 0); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|