1 | <?php |
||
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 | 2 | } |
|
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 | public function __toString() |
||
70 | { |
||
71 | 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 | 5 | public function getUnit() |
|
93 | |||
94 | /** |
||
95 | * Get the value. |
||
96 | * |
||
97 | * @return float The value. |
||
98 | */ |
||
99 | 6 | public function getValue() |
|
103 | |||
104 | /** |
||
105 | * Get the value's description. |
||
106 | * |
||
107 | * @return string The value's description. |
||
108 | */ |
||
109 | 2 | public function getDescription() |
|
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 | public function getFormatted() |
||
129 | } |
||
130 |