]> git.mxchange.org Git - friendica-addons.git/blob - curweather/vendor/cmfcmf/openweathermap-php-api/Cmfcmf/OpenWeatherMap/Util/Unit.php
added composer.json and needed libs
[friendica-addons.git] / curweather / vendor / cmfcmf / openweathermap-php-api / Cmfcmf / OpenWeatherMap / Util / Unit.php
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     public function __construct($value = 0.0, $unit = "", $description = "")
56     {
57         $this->value = (float)$value;
58         $this->unit = (string)$unit;
59         $this->description = (string)$description;
60     }
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     public function getUnit()
82     {
83         // Units are inconsistent. Only celsius and fahrenheit are not abbreviated. This check fixes that.
84         if ($this->unit == 'celsius') {
85             return "&deg;C";
86         } else if ($this->unit == 'fahrenheit') {
87             return 'F';
88         } else {
89             return $this->unit;
90         }
91     }
92
93     /**
94      * Get the value.
95      *
96      * @return float The value.
97      */
98     public function getValue()
99     {
100         return $this->value;
101     }
102
103     /**
104      * Get the value's description.
105      *
106      * @return string The value's description.
107      */
108     public function getDescription()
109     {
110         return $this->description;
111     }
112
113     /**
114      * Get the value as formatted string with unit.
115      *
116      * @return string The value as formatted string with unit.
117      *
118      * The unit is not included if it is empty.
119      */
120     public function getFormatted()
121     {
122         if ($this->getUnit() != "") {
123             return "{$this->getValue()} {$this->getUnit()}";
124         } else {
125             return "{$this->getValue()}";
126         }
127     }
128 }