3 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
7 * Please see the LICENSE file distributed with this source code for further
8 * information regarding copyright and licensing.
10 * Please visit the following links to read about the usage policies and the license of
11 * OpenWeatherMap before using this class:
13 * @see http://www.OpenWeatherMap.org
14 * @see http://www.OpenWeatherMap.org/terms
15 * @see http://openweathermap.org/appid
18 namespace Cmfcmf\OpenWeatherMap\Util;
21 * The unit class representing a unit object.
26 * @var float The value.
33 * @var string The value's unit.
40 * @var string The value's description.
47 * Create a new unit object.
49 * @param float $value The value.
50 * @param string $unit The unit of the value.
51 * @param string $description The description of the value.
55 public function __construct($value = 0.0, $unit = "", $description = "")
57 $this->value = (float)$value;
58 $this->unit = (string)$unit;
59 $this->description = (string)$description;
63 * Get the value as formatted string with unit.
65 * @return string The value as formatted string with unit.
67 * The unit is not included if it is empty.
69 public function __toString()
71 return $this->getFormatted();
75 * Get the value's unit.
77 * @return string The value's unit.
79 * This also converts 'celsius' to '°C' and 'fahrenheit' to 'F'.
81 public function getUnit()
83 // Units are inconsistent. Only celsius and fahrenheit are not abbreviated. This check fixes that.
84 if ($this->unit == 'celsius') {
86 } else if ($this->unit == 'fahrenheit') {
96 * @return float The value.
98 public function getValue()
104 * Get the value's description.
106 * @return string The value's description.
108 public function getDescription()
110 return $this->description;
114 * Get the value as formatted string with unit.
116 * @return string The value as formatted string with unit.
118 * The unit is not included if it is empty.
120 public function getFormatted()
122 if ($this->getUnit() != "") {
123 return "{$this->getValue()} {$this->getUnit()}";
125 return "{$this->getValue()}";