Completed
Push — master ( 5bd7d5...03ff4c )
by Bryan
50:21 queued 42:15
created

Unit::getFormatted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace Bhhaskin\WeatherBee\Util;
3
4
/**
5
*
6
*/
7
class Unit
8
{
9
    /**
10
    * The value
11
    * @var float
12
    * @internal
13
    */
14
    private $value;
15
16
    /**
17
    * The value's unit
18
    * @var string
19
    * @internal
20
    */
21
    private $unit;
22
23
    /**
24
    * The value's description
25
    * @var string
26
    * @internal
27
    */
28
    private $description;
29
30
    /**
31
    * Create a new unit object
32
    * @param float  $value       The value
33
    * @param string $unit        The value's unit
34
    * @param string $description The value's description
35
    */
36 7
    public function __construct(float $value = 0.0, string $unit = null, string $description = null)
37
    {
38 7
        $this->value = $value;
39 7
        $this->unit = $unit;
40 7
        $this->description = $description;
41 7
    }
42
43
    /**
44
    * Get the value as formatted string with unit.
45
    * Unit will not be included if it is empty
46
    *
47
    * @return string The value formatted string with unit.
48
    */
49 1
    public function __toString(): string
50
    {
51 1
        return $this->getFormatted();
52
    }
53
54
    /**
55
    * Get the value's unit.
56
    * This also converts 'celsius' to 'C' and 'fahrenheit' to 'F'.
57
    *
58
    * @return string The value's unit.
59
    */
60 4
    public function getUnit(): ?string
61
    {
62 4
        if ($this->unit == 'celsius' || $this->unit == 'metric') {
63 1
            return "C";
64 3
        } elseif ($this->unit == 'fahrenheit') {
65 2
            return 'F';
66
        } else {
67 1
            return $this->unit;
68
        }
69
    }
70
71
    /**
72
    * Get the value.
73
    *
74
    * @return float The value
75
    */
76 2
    public function getValue(): float
77
    {
78 2
        return $this->value;
79
    }
80
81
    /**
82
    * Get the value's description.
83
    *
84
    * @return string Value's description.
85
    */
86 1
    public function getDescription(): ?string
87
    {
88 1
        return $this->description;
89
    }
90
91
    /**
92
    * Get the value as a formatted string with unit.
93
    *
94
    * @return string The value as formatted string with unit.
95
    */
96 1
    public function getFormatted(): string
97
    {
98 1
        if (!empty($this->getUnit())) {
99 1
            return (string)$this->getValue() . " " . $this->getUnit();
100
        } else {
101
            return (string)$this->getValue();
102
        }
103
    }
104
}
105