-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temperature.php
47 lines (38 loc) · 790 Bytes
/
Temperature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace UnitConvert;
class Temperature
{
private float $celsius;
public static function from_celsius(float $celsius): self
{
return new static ($celsius);
}
public static function from_kelvin(float $k): self
{
// C = K - 273.15
return new static ($k - 273.15);
}
public static function from_fahrenheit(float $f): self
{
// C = 5/9 * (F - 32)
return new static (5 / 9 * ($f - 32));
}
public function __construct(float $celsius)
{
$this->celsius = $celsius;
}
public function to_celsius(): float
{
return $this->celsius;
}
public function to_fahrenheit(): float
{
// F = C * 9/5 + 32 = C * 1.8 + 32
return $this->celsius * 1.8 + 32;
}
public function to_kelvin(): float
{
return $this->celsius + 273.15;
}
}