Completed
Push — OpenWeatherMap-PHP-Api-55 ( daf760 )
by Christian
02:08
created

Unit::getFormatted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
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 16
    }
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 1
    public function __toString()
70
    {
71 1
        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 8
    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 8
        if ($this->unit == 'celsius' || $this->unit == 'metric') {
86 2
            return "&deg;C";
87 6
        } elseif ($this->unit == 'fahrenheit') {
88 1
            return 'F';
89
        } else {
90 5
            return $this->unit;
91
        }
92
    }
93
94
    /**
95
     * Get the value.
96
     *
97
     * @return float The value.
98
     */
99 9
    public function getValue()
100
    {
101 9
        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 3
    public function getFormatted()
122
    {
123 3
        if ($this->getUnit() != "") {
124 2
            return $this->getValue() . " " . $this->getUnit();
125
        } else {
126 1
            return (string)$this->getValue();
127
        }
128
    }
129
}
130