Completed
Pull Request — master (#138)
by Christian
01:27
created

Unit   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 124
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __toString() 0 4 1
A getUnit() 0 12 4
A getValue() 0 4 1
A getDescription() 0 4 1
A getPrecision() 0 4 1
A getFormatted() 0 8 2
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
/**
22
 * The unit class representing a unit object.
23
 */
24
class Unit
25
{
26
    /**
27
     * @var float The value.
28
     *
29
     * @internal
30
     */
31
    private $value;
32
33
    /**
34
     * @var string The value's unit.
35
     *
36
     * @internal
37
     */
38
    private $unit;
39
40
    /**
41
     * @var string The value's description.
42
     *
43
     * @internal
44
     */
45
    private $description;
46
47
    /**
48
     * @var float|null The value's measurement precision. Can be null if unknown.
49
     */
50
    private $precision;
51
52
    /**
53
     * Create a new unit object.
54
     *
55 16
     * @param float $value The value.
56
     * @param string $unit The unit of the value.
57 16
     * @param string $description The description of the value.
58 16
     * @param float|null $precision The precision of the value, or null if unknown.
59 16
     *
60 16
     * @internal
61
     */
62
    public function __construct($value = 0.0, $unit = "", $description = "", $precision = null)
63
    {
64
        $this->value = (float)$value;
65
        $this->unit = (string)$unit;
66
        $this->description = (string)$description;
67
        $this->precision = $precision === null ? null : (float)$precision;
68
    }
69 1
70
    /**
71 1
     * Get the value as formatted string with unit.
72
     *
73
     * @return string The value as formatted string with unit.
74
     *
75
     * The unit is not included if it is empty.
76
     */
77
    public function __toString()
78
    {
79
        return $this->getFormatted();
80
    }
81 8
82
    /**
83
     * Get the value's unit.
84
     *
85 8
     * @return string The value's unit.
86 2
     *
87 6
     * This also converts 'celsius' to '°C' and 'fahrenheit' to '°F'.
88 1
     */
89
    public function getUnit()
90 5
    {
91
        // Units are inconsistent. Only celsius and fahrenheit are not abbreviated. This check fixes that.
92
        // Also, the API started to return "metric" as temperature unit recently. Also fix that.
93
        if ($this->unit == 'celsius' || $this->unit == 'metric') {
94
            return "°C";
95
        } elseif ($this->unit == 'fahrenheit') {
96
            return '°F';
97
        } else {
98
            return $this->unit;
99 9
        }
100
    }
101 9
102
    /**
103
     * Get the value.
104
     *
105
     * @return float The value.
106
     */
107
    public function getValue()
108
    {
109 2
        return $this->value;
110
    }
111 2
112
    /**
113
     * Get the value's description.
114
     *
115
     * @return string The value's description.
116
     */
117
    public function getDescription()
118
    {
119
        return $this->description;
120
    }
121 3
122
    /**
123 3
     * Get the value's precision.
124 2
     *
125
     * @return float|null The value's precision. Can be null if unknown.
126 1
     */
127
    public function getPrecision()
128
    {
129
        return $this->precision;
130
    }
131
132
    /**
133
     * Get the value as formatted string with unit.
134
     *
135
     * @return string The value as formatted string with unit.
136
     *
137
     * The unit is not included if it is empty.
138
     */
139
    public function getFormatted()
140
    {
141
        if ($this->getUnit() != "") {
142
            return $this->getValue() . " " . $this->getUnit();
143
        } else {
144
            return (string)$this->getValue();
145
        }
146
    }
147
}
148