|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* |
|
8
|
|
|
* Please see the LICENSE file distributed with this source code for further |
|
9
|
|
|
* information regarding copyright and licensing. |
|
10
|
|
|
* |
|
11
|
|
|
* Please visit the following links to read about the usage policies and the license of |
|
12
|
|
|
* OpenWeatherMap data before using this library: |
|
13
|
|
|
* |
|
14
|
|
|
* @see https://OpenWeatherMap.org/price |
|
15
|
|
|
* @see https://OpenWeatherMap.org/terms |
|
16
|
|
|
* @see https://OpenWeatherMap.org/appid |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Cmfcmf\OpenWeatherMap\Util; |
|
20
|
|
|
|
|
21
|
|
|
use JsonSerializable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The unit class representing a unit object. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Unit implements JsonSerializable |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var float The value. |
|
30
|
|
|
* |
|
31
|
|
|
* @internal |
|
32
|
|
|
*/ |
|
33
|
|
|
private $value; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string The value's unit. |
|
37
|
|
|
* |
|
38
|
|
|
* @internal |
|
39
|
|
|
*/ |
|
40
|
|
|
private $unit; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string The value's description. |
|
44
|
|
|
* |
|
45
|
|
|
* @internal |
|
46
|
|
|
*/ |
|
47
|
|
|
private $description; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var float|null The value's measurement precision. Can be null if unknown. |
|
51
|
|
|
*/ |
|
52
|
|
|
private $precision; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
16 |
|
* Create a new unit object. |
|
56
|
|
|
* |
|
57
|
16 |
|
* @param float $value The value. |
|
58
|
16 |
|
* @param string $unit The unit of the value. |
|
59
|
16 |
|
* @param string $description The description of the value. |
|
60
|
16 |
|
* @param float|null $precision The precision of the value, or null if unknown. |
|
61
|
|
|
* |
|
62
|
|
|
* @internal |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($value = 0.0, $unit = "", $description = "", $precision = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->value = (float)$value; |
|
67
|
|
|
$this->unit = (string)$unit; |
|
68
|
|
|
$this->description = (string)$description; |
|
69
|
1 |
|
$this->precision = $precision === null ? null : (float)$precision; |
|
70
|
|
|
} |
|
71
|
1 |
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the value as formatted string with unit. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string The value as formatted string with unit. |
|
76
|
|
|
* |
|
77
|
|
|
* The unit is not included if it is empty. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __toString() |
|
80
|
|
|
{ |
|
81
|
8 |
|
return $this->getFormatted(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
8 |
|
* Get the value's unit. |
|
86
|
2 |
|
* |
|
87
|
6 |
|
* @return string The value's unit. |
|
88
|
1 |
|
* |
|
89
|
|
|
* This also converts 'celsius' to '°C' and 'fahrenheit' to '°F'. |
|
90
|
5 |
|
*/ |
|
91
|
|
|
public function getUnit() |
|
92
|
|
|
{ |
|
93
|
|
|
// Units are inconsistent. Only celsius and fahrenheit are not abbreviated. This check fixes that. |
|
94
|
|
|
// Also, the API started to return "metric" as temperature unit recently. Also fix that. |
|
95
|
|
|
if ($this->unit == 'celsius' || $this->unit == 'metric') { |
|
96
|
|
|
return "°C"; |
|
97
|
|
|
} elseif ($this->unit == 'fahrenheit') { |
|
98
|
|
|
return '°F'; |
|
99
|
9 |
|
} else { |
|
100
|
|
|
return $this->unit; |
|
101
|
9 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the value. |
|
106
|
|
|
* |
|
107
|
|
|
* @return float The value. |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function getValue() |
|
110
|
|
|
{ |
|
111
|
2 |
|
return $this->value; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the value's description. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string The value's description. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getDescription() |
|
120
|
|
|
{ |
|
121
|
3 |
|
return $this->description; |
|
122
|
|
|
} |
|
123
|
3 |
|
|
|
124
|
2 |
|
/** |
|
125
|
|
|
* Get the value's precision. |
|
126
|
1 |
|
* |
|
127
|
|
|
* @return float|null The value's precision. Can be null if unknown. |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getPrecision() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->precision; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the value as formatted string with unit. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string The value as formatted string with unit. |
|
138
|
|
|
* |
|
139
|
|
|
* The unit is not included if it is empty. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getFormatted() |
|
142
|
|
|
{ |
|
143
|
|
|
if ($this->getUnit() != "") { |
|
144
|
|
|
return $this->getValue() . " " . $this->getUnit(); |
|
145
|
|
|
} else { |
|
146
|
|
|
return (string)$this->getValue(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get Unit properties when encoding to JSON |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
public function jsonSerialize() |
|
156
|
|
|
{ |
|
157
|
|
|
return [ |
|
158
|
|
|
'value' => $this->getValue(), |
|
159
|
|
|
'unit' => $this->getUnit(), |
|
160
|
|
|
'description' => $this->getDescription(), |
|
161
|
|
|
'precision' => $this->getPrecision() |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|