1 | <?php |
||
26 | class Unit implements JsonSerializable |
||
27 | { |
||
28 | /** |
||
29 | * @var float The value. |
||
30 | * |
||
31 | * @internal |
||
32 | */ |
||
33 | private $value; |
||
34 | |||
35 | /** |
||
36 | * @var string The value's unit. |
||
37 | * |
||
38 | * @internal |
||
39 | */ |
||
40 | private $unit; |
||
41 | |||
42 | /** |
||
43 | * @var string The value's description. |
||
44 | * |
||
45 | * @internal |
||
46 | */ |
||
47 | private $description; |
||
48 | |||
49 | /** |
||
50 | * @var float|null The value's measurement precision. Can be null if unknown. |
||
51 | */ |
||
52 | private $precision; |
||
53 | |||
54 | /** |
||
55 | 16 | * Create a new unit object. |
|
56 | * |
||
57 | 16 | * @param float $value The value. |
|
58 | 16 | * @param string $unit The unit of the value. |
|
59 | 16 | * @param string $description The description of the value. |
|
60 | 16 | * @param float|null $precision The precision of the value, or null if unknown. |
|
61 | * |
||
62 | * @internal |
||
63 | */ |
||
64 | public function __construct($value = 0.0, $unit = "", $description = "", $precision = null) |
||
71 | 1 | ||
72 | /** |
||
73 | * Get the value as formatted string with unit. |
||
74 | * |
||
75 | * @return string The value as formatted string with unit. |
||
76 | * |
||
77 | * The unit is not included if it is empty. |
||
78 | */ |
||
79 | public function __toString() |
||
83 | |||
84 | /** |
||
85 | 8 | * Get the value's unit. |
|
86 | 2 | * |
|
87 | 6 | * @return string The value's unit. |
|
88 | 1 | * |
|
89 | * This also converts 'celsius' to '°C' and 'fahrenheit' to '°F'. |
||
90 | 5 | */ |
|
91 | public function getUnit() |
||
103 | |||
104 | /** |
||
105 | * Get the value. |
||
106 | * |
||
107 | * @return float The value. |
||
108 | */ |
||
109 | 2 | public function getValue() |
|
113 | |||
114 | /** |
||
115 | * Get the value's description. |
||
116 | * |
||
117 | * @return string The value's description. |
||
118 | */ |
||
119 | public function getDescription() |
||
123 | 3 | ||
124 | 2 | /** |
|
125 | * Get the value's precision. |
||
126 | 1 | * |
|
127 | * @return float|null The value's precision. Can be null if unknown. |
||
128 | */ |
||
129 | public function getPrecision() |
||
133 | |||
134 | /** |
||
135 | * Get the value as formatted string with unit. |
||
136 | * |
||
137 | * @return string The value as formatted string with unit. |
||
138 | * |
||
139 | * The unit is not included if it is empty. |
||
140 | */ |
||
141 | public function getFormatted() |
||
149 | |||
150 | /** |
||
151 | * Get Unit properties when encoding to JSON |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function jsonSerialize() |
||
164 | } |
||
165 |