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\ConverterInterface; |
14
|
|
|
use Xynnn\Unicorn\Exception\UnsupportedUnitException; |
15
|
|
|
use Xynnn\Unicorn\Model\ConvertibleValue; |
16
|
|
|
use Xynnn\Unicorn\Model\Unit; |
17
|
|
|
|
18
|
|
|
abstract class AbstractConverter implements ConverterInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const MAX_DECIMALS = 999; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Unit[] List of convertible units |
25
|
|
|
*/ |
26
|
|
|
protected $units = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract public function getName(): string; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ConvertibleValue $from |
35
|
|
|
* @param Unit $to |
36
|
|
|
* @return ConvertibleValue |
37
|
|
|
*/ |
38
|
68 |
|
public function convert(ConvertibleValue $from, Unit $to): ConvertibleValue |
39
|
|
|
{ |
40
|
68 |
|
if (!$from instanceof ConvertibleValue || !is_string($from->getValue()) || !$from->getUnit() instanceof Unit) { |
41
|
|
|
throw new \InvalidArgumentException('The given ConvertibleValue is not valid for conversion.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
68 |
|
$this->validate([$from->getUnit(), $to]); |
45
|
66 |
|
$this->normalize($from); |
46
|
66 |
|
$this->convertTo($from, $to); |
47
|
|
|
|
48
|
66 |
|
return $from; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $units |
53
|
|
|
*/ |
54
|
68 |
|
protected function validate(array $units) |
55
|
|
|
{ |
56
|
68 |
|
foreach ($units as $unit) { |
57
|
|
|
// make sure the unit is equal to an instance in the converters units array |
58
|
68 |
|
if (!in_array($unit, $this->units)) { |
59
|
68 |
|
throw new UnsupportedUnitException($unit); |
60
|
|
|
} |
61
|
|
|
} |
62
|
66 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ConvertibleValue $cv |
66
|
|
|
*/ |
67
|
61 |
|
protected function normalize(ConvertibleValue $cv) |
68
|
|
|
{ |
69
|
61 |
|
$cv->setValue(bcdiv($cv->getValue(), $cv->getUnit()->getFactor(), self::MAX_DECIMALS)); |
70
|
61 |
|
$cv->setUnit($this->getBaseUnit()); |
71
|
61 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ConvertibleValue $from The convertible to be converted |
75
|
|
|
* @param Unit $to Unit to which is to be converted |
76
|
|
|
*/ |
77
|
61 |
|
protected function convertTo(ConvertibleValue $from, Unit $to) |
78
|
|
|
{ |
79
|
61 |
|
$from->setValue(bcmul($from->getValue(), $to->getFactor(), self::MAX_DECIMALS)); |
80
|
61 |
|
$from->setUnit($to); |
81
|
61 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getUnits(): array |
87
|
|
|
{ |
88
|
|
|
return $this->units; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |