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