1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawaby88\OpenWeatherMap\Services\Support; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Weather. |
7
|
|
|
*/ |
8
|
|
|
class Weather |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string[] Directions. |
12
|
|
|
*/ |
13
|
|
|
private $directions = [ |
14
|
|
|
'N', |
15
|
|
|
'NNE', |
16
|
|
|
'NE', |
17
|
|
|
'ENE', |
18
|
|
|
'E', |
19
|
|
|
'ESE', |
20
|
|
|
'SE', |
21
|
|
|
'SSE', |
22
|
|
|
'S', |
23
|
|
|
'SSW', |
24
|
|
|
'SW', |
25
|
|
|
'WSW', |
26
|
|
|
'W', |
27
|
|
|
'WNW', |
28
|
|
|
'NW', |
29
|
|
|
'NNW', |
30
|
|
|
'N', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string Weather condition title. |
35
|
|
|
*/ |
36
|
|
|
public $condition; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string Weather condition description. |
40
|
|
|
*/ |
41
|
|
|
public $description; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Weather icon. |
45
|
|
|
*/ |
46
|
|
|
public $icon; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string Icon url. |
50
|
|
|
*/ |
51
|
|
|
public $iconUrl; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var float Wind speed percentage. |
55
|
|
|
*/ |
56
|
|
|
public $windSpeed; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var float Wind degree. |
60
|
|
|
*/ |
61
|
|
|
public $windDeg; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string Wind direction. |
65
|
|
|
*/ |
66
|
|
|
public $windDir; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var int clouds. |
70
|
|
|
*/ |
71
|
|
|
public $clouds; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array Precipitation volume. |
75
|
|
|
*/ |
76
|
|
|
public $precipitationVolume; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array Snow volume. |
80
|
|
|
*/ |
81
|
|
|
public $snowVolume; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Weather constructor. |
85
|
|
|
* @param string $condition Weather condition title. |
86
|
|
|
* @param string $description Weather condition description. |
87
|
|
|
* @param string $icon Weather icon. |
88
|
|
|
* @param float $windSpeed Wind speed percentage. |
89
|
|
|
* @param float $windDeg Wind degree. |
90
|
|
|
* @param int $clouds clouds. |
91
|
|
|
* @param array $precipitationVolume Precipitation volume. |
92
|
|
|
* @param array $snowVolume Snow volume. |
93
|
|
|
*/ |
94
|
|
|
public function __construct(string $condition, string $description, string $icon, float $windSpeed, float $windDeg, int $clouds, array $precipitationVolume, array $snowVolume) |
95
|
|
|
{ |
96
|
|
|
$this->condition = $condition; |
97
|
|
|
$this->description = $description; |
98
|
|
|
$this->icon = $icon; |
99
|
|
|
$this->iconUrl = $this->getIconUrl(); |
100
|
|
|
$this->windSpeed = $windSpeed; |
101
|
|
|
$this->windDeg = $windDeg; |
102
|
|
|
$this->windDir = $this->windDegToWindDirection(); |
103
|
|
|
$this->clouds = $clouds; |
104
|
|
|
$this->precipitationVolume = $precipitationVolume; |
105
|
|
|
$this->snowVolume = $snowVolume; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generate icon url. |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getIconUrl(): string |
113
|
|
|
{ |
114
|
|
|
return sprintf(config('open-weather.api_icon_url'), $this->icon); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Convert wind degrees to wind direction. |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function windDegToWindDirection(): string |
122
|
|
|
{ |
123
|
|
|
return $this->directions[round($this->windDeg / 22.5)]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Encoding to json. |
128
|
|
|
* @return false|string |
129
|
|
|
*/ |
130
|
|
|
public function toJson() |
131
|
|
|
{ |
132
|
|
|
return json_encode( |
133
|
|
|
[ |
134
|
|
|
'condition' => $this->condition, |
135
|
|
|
'description' => $this->description, |
136
|
|
|
'icon' => $this->icon, |
137
|
|
|
'iconUrl' => $this->iconUrl, |
138
|
|
|
'windSpeed' => $this->windSpeed, |
139
|
|
|
'windDeg' => $this->windDeg, |
140
|
|
|
'windDir' => $this->windDir, |
141
|
|
|
'clouds' => $this->clouds, |
142
|
|
|
'precipitationVolume' => $this->precipitationVolume, |
143
|
|
|
'snowVolume' => $this->snowVolume, |
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|