Completed
Push — master ( bf308c...9563f6 )
by Bryan
13:17 queued 06:12
created

Unit::getFormatted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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 1
    public function __construct(float $value = 0.0, string $unit = null, string $description = null)
37
    {
38 1
        $this->value = $value;
39 1
        $this->unit = $unit;
40 1
        $this->description = $description;
41 1
    }
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 3
    public function getUnit(): ?string
61
    {
62 3
        if ($this->unit == 'celsius' || $this->unit == 'metric') {
63 1
            return "C";
64 2
        } elseif ($this->unit == 'fahrenheit') {
65 1
            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 1
    public function getValue(): float
77
    {
78 1
        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
    public function getFormatted(): string
97
    {
98
        if (!empty($this->getUnit())) {
99
            return (string)$this->getValue() . " " . $this->getUnit();
100
        } else {
101
            return (string)$this->getValue();
102
        }
103
    }
104
}
105