1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the unicorn project |
4
|
|
|
* |
5
|
|
|
* (c) Philipp Braeutigam <[email protected]>, Steffen Brand <[email protected]> and contributors |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Xynnn\Unicorn\Converter; |
12
|
|
|
|
13
|
|
|
use Xynnn\Unicorn\Model\Unit; |
14
|
|
|
use Xynnn\Unicorn\Model\ConvertibleValue; |
15
|
|
|
|
16
|
|
|
class TemperatureConverter extends AbstractMathematicalConverter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Unit $celsius Static instance for conversions |
21
|
|
|
*/ |
22
|
|
|
public static $celsius; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Unit $fahrenheit Static instance for conversions |
26
|
|
|
*/ |
27
|
|
|
public static $fahrenheit; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Unit $kelvin Static instance for conversions |
31
|
|
|
*/ |
32
|
|
|
public static $kelvin; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LengthConverter constructor. |
36
|
|
|
*/ |
37
|
5 |
|
public function __construct() |
38
|
|
|
{ |
39
|
5 |
|
$this->units[] = self::$celsius = new Unit('Celsius ', '°C'); |
40
|
5 |
|
$this->units[] = self::$fahrenheit = new Unit('Fahrenheit ', '°F'); |
41
|
5 |
|
$this->units[] = self::$kelvin = new Unit('Kelvin ', 'K'); |
42
|
5 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string Name of the converter |
46
|
|
|
*/ |
47
|
4 |
|
public function getName(): string |
48
|
|
|
{ |
49
|
4 |
|
return 'unicorn.converter.temperature'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ConvertibleValue $cv The Convertible to be normalized |
54
|
|
|
*/ |
55
|
5 |
View Code Duplication |
protected function normalize(ConvertibleValue $cv) |
|
|
|
|
56
|
|
|
{ |
57
|
5 |
|
switch ($cv->getUnit()) { |
58
|
|
|
|
59
|
5 |
|
case self::$fahrenheit: |
60
|
1 |
|
$value = bcdiv(bcmul(bcsub($cv->getValue(), '32', self::MAX_DECIMALS), '5', self::MAX_DECIMALS), '9', self::MAX_DECIMALS); |
61
|
1 |
|
break; |
62
|
|
|
|
63
|
4 |
|
case self::$kelvin: |
64
|
|
|
$value = bcsub($cv->getValue(), '273.15', self::MAX_DECIMALS); |
65
|
|
|
break; |
66
|
|
|
|
67
|
|
|
default: |
68
|
4 |
|
$value = $cv->getValue(); |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
$cv->setValue($value); |
73
|
5 |
|
$cv->setUnit($this->getBaseUnit()); |
74
|
5 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ConvertibleValue $from The convertible to be converted |
78
|
|
|
* @param Unit $to Unit to which is to be converted |
79
|
|
|
*/ |
80
|
5 |
View Code Duplication |
protected function convertTo(ConvertibleValue $from, Unit $to) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
switch ($to) { |
83
|
|
|
|
84
|
5 |
|
case self::$fahrenheit: |
85
|
2 |
|
$value = bcadd(bcdiv(bcmul($from->getValue(), '9', self::MAX_DECIMALS), '5', self::MAX_DECIMALS), '32', self::MAX_DECIMALS); |
86
|
2 |
|
break; |
87
|
|
|
|
88
|
3 |
|
case self::$kelvin: |
89
|
2 |
|
$value = bcadd($from->getValue(), '273.15', self::MAX_DECIMALS); |
90
|
2 |
|
break; |
91
|
|
|
|
92
|
|
|
default: |
93
|
1 |
|
$value = $from->getValue(); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
5 |
|
$from->setValue($value); |
98
|
5 |
|
$from->setUnit($to); |
99
|
5 |
|
} |
100
|
|
|
|
101
|
5 |
|
public function getBaseUnit(): Unit |
102
|
|
|
{ |
103
|
5 |
|
return self::$celsius; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.