1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Units of Measure Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-units |
6
|
|
|
* @package php-units |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\units\tree; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\units\CalculatorInterface; |
14
|
|
|
use hiqdev\php\units\calculators\PhpCalculator; |
15
|
|
|
use hiqdev\php\units\exceptions\InvalidArgumentException; |
16
|
|
|
use hiqdev\php\units\exceptions\NotConvertibleException; |
17
|
|
|
use hiqdev\php\units\UnitInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Root unit in tree. |
21
|
|
|
* |
22
|
|
|
* @author Andrii Vasyliev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class RootUnit implements UnitInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TreeConverter |
28
|
|
|
*/ |
29
|
|
|
protected $tree; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
10 |
|
public function getName() |
40
|
|
|
{ |
41
|
10 |
|
return $this->name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @return UnitInterface */ |
45
|
7 |
|
public function getParent() |
46
|
|
|
{ |
47
|
7 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @return UnitInterface */ |
51
|
10 |
|
public function getRoot() |
52
|
|
|
{ |
53
|
10 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
public function getFactor() |
57
|
|
|
{ |
58
|
8 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
7 |
|
public function getMeasure() |
62
|
|
|
{ |
63
|
7 |
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param TreeConverter $tree |
68
|
|
|
* @param string $name |
69
|
|
|
*/ |
70
|
5 |
|
public function __construct(TreeConverter $tree, $name) |
71
|
|
|
{ |
72
|
5 |
|
$this->tree = $tree; |
73
|
5 |
|
$this->name = $name; |
74
|
5 |
|
} |
75
|
|
|
|
76
|
|
|
protected $calculator; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns calculator for this unit. |
80
|
|
|
* Units can have different calculators. |
81
|
|
|
* @return CalculatorInterface |
82
|
|
|
*/ |
83
|
7 |
|
public function getCalculator() |
84
|
|
|
{ |
85
|
7 |
|
if ($this->calculator === null) { |
86
|
3 |
|
$this->calculator = new PhpCalculator(); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
return $this->calculator; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
9 |
|
public function equals(UnitInterface $other) |
96
|
|
|
{ |
97
|
9 |
|
return $this->getName() === $other->getName() || $this->isAlias($other); |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
public function isAlias(UnitInterface $other) |
101
|
|
|
{ |
102
|
9 |
|
$node = $this->getNode($other); |
103
|
|
|
|
104
|
9 |
|
return $this === $node->getCanon() |
105
|
9 |
|
|| $node === $this->getCanon() |
106
|
|
|
|| ( |
107
|
9 |
|
$this->getRoot() === $this->getNode($other)->getRoot() |
108
|
9 |
|
&& $this->getFactor() === $this->getNode($other)->getFactor() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
public function getCanon() |
113
|
|
|
{ |
114
|
8 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
7 |
|
public function assertConvertible(UnitInterface $other) |
121
|
|
|
{ |
122
|
7 |
|
if (!$this->isConvertible($other)) { |
123
|
|
|
throw new InvalidArgumentException('not convertible unit: ' . $other->getName()); |
124
|
|
|
} |
125
|
7 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
9 |
|
final public function isConvertible(UnitInterface $other) |
131
|
|
|
{ |
132
|
9 |
|
return $this->getMeasure() === $other->getMeasure(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
7 |
|
public function convert(UnitInterface $other, $quantity) |
139
|
|
|
{ |
140
|
7 |
|
$this->assertConvertible($other); |
141
|
|
|
|
142
|
7 |
|
$other = $this->getNode($other); |
143
|
|
|
|
144
|
7 |
|
if ($this->equals($other)) { |
145
|
4 |
|
return $quantity; |
146
|
|
|
} |
147
|
|
|
|
148
|
7 |
|
if ($other->equals($this->getParent())) { |
149
|
7 |
|
return $this->multiplyByFactor($quantity); |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
if ($this->equals($other->getParent())) { |
|
|
|
|
153
|
7 |
|
return $other->divideByFactor($quantity); |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
return $this->getRoot()->convert($other, $this->convert($this->getRoot(), $quantity)); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
7 |
View Code Duplication |
public function multiplyByFactor($quantity) |
|
|
|
|
160
|
|
|
{ |
161
|
7 |
|
if (empty($this->factor)) { |
|
|
|
|
162
|
|
|
throw new NotConvertibleException('method not yet implement: ' . $this->getName()); |
163
|
|
|
} |
164
|
|
|
|
165
|
7 |
|
return $this->getCalculator()->multiply($quantity, $this->factor); |
166
|
|
|
} |
167
|
|
|
|
168
|
7 |
View Code Duplication |
public function divideByFactor($quantity) |
|
|
|
|
169
|
|
|
{ |
170
|
7 |
|
if (empty($this->factor)) { |
171
|
|
|
throw new NotConvertibleException('method not yet implement: ' . $this->getName()); |
172
|
|
|
} |
173
|
|
|
|
174
|
7 |
|
return $this->getCalculator()->divide($quantity, $this->factor); |
175
|
|
|
} |
176
|
|
|
|
177
|
9 |
|
public function getNode(UnitInterface $unit) |
178
|
|
|
{ |
179
|
9 |
|
return $this->tree->getNode($unit); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: