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