|
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; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Quantity with Unit. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractQuantity implements QuantityInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var UnitInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $unit; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var float|int|string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $quantity; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var UnitInterface $unit |
|
32
|
|
|
* @var float|int|string $quantity |
|
33
|
|
|
*/ |
|
34
|
6 |
|
private function __construct(UnitInterface $unit, $quantity) |
|
35
|
|
|
{ |
|
36
|
6 |
|
$this->unit = $unit; |
|
37
|
6 |
|
$this->quantity = $quantity; |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates quantity with same unit. |
|
42
|
|
|
* Optimized to return this if same quantity. |
|
43
|
|
|
* @var float|int|string $quantity |
|
44
|
|
|
*/ |
|
45
|
3 |
|
final protected function create($quantity) |
|
46
|
|
|
{ |
|
47
|
3 |
|
return $this->quantity === $quantity ? $this : new static($this->unit, $quantity); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private function getCalculator() |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->unit->getCalculator(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
2 |
|
final public function getQuantity() |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->quantity; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
1 |
|
final public function getUnit() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->unit; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
2 |
|
final public function compare(QuantityInterface $other) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$arg = $other->convert($this->unit); |
|
80
|
|
|
|
|
81
|
2 |
|
return $this->getCalculator()->compare($this->quantity, $arg->quantity); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
1 |
|
final public function equals(QuantityInterface $other) |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->compare($other) === 0; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
1 |
|
final public function isConvertible(UnitInterface $unit) |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->unit->isConvertible($unit); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
3 |
|
final public function convert(UnitInterface $unit) |
|
104
|
|
|
{ |
|
105
|
3 |
|
$res = $this->unit->convert($unit, $this->quantity); |
|
106
|
|
|
|
|
107
|
3 |
|
return $this->create($res); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
final public function add(QuantityInterface $addend) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$arg = $addend->convert($this->unit); |
|
116
|
|
|
$res = $this->getCalculator()->add($this->quantity, $arg); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
return $this->create($res); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
View Code Duplication |
final public function subtract(QuantityInterface $subtrahend) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
$arg = $subtrahend->convert($this->unit); |
|
127
|
|
|
$res = $this->getCalculator()->subtract($this->quantity, $arg); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
return $this->create($res); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
final public function multiply($multiplier) |
|
136
|
|
|
{ |
|
137
|
|
|
$res = $this->getCalculator()->multiply($this->quantity, $multiplier); |
|
138
|
|
|
|
|
139
|
|
|
return $this->create($res); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
final public function divide($divisor) |
|
146
|
|
|
{ |
|
147
|
|
|
$res = $this->getCalculator()->divide($this->quantity, $divisor); |
|
148
|
|
|
|
|
149
|
|
|
return $this->create($res); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
*/ |
|
155
|
|
|
final public function ceil() |
|
156
|
|
|
{ |
|
157
|
|
|
$res = $this->getCalculator()->ceil($this->quantity); |
|
158
|
|
|
|
|
159
|
|
|
return $this->create($res); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
final public function floor() |
|
166
|
|
|
{ |
|
167
|
|
|
$res = $this->getCalculator()->floor($this->quantity); |
|
168
|
|
|
|
|
169
|
|
|
return $this->create($res); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
|
|
final public function round($roundingMode) |
|
176
|
|
|
{ |
|
177
|
|
|
$res = $this->getCalculator()->round($this->quantity, $roundingMode); |
|
178
|
|
|
|
|
179
|
|
|
return $this->create($res); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritdoc} |
|
184
|
|
|
*/ |
|
185
|
|
|
final public function absolute() |
|
186
|
|
|
{ |
|
187
|
|
|
$res = $this->getCalculator()->absolute($this->quantity); |
|
188
|
|
|
|
|
189
|
|
|
return $this->create($res); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
6 |
|
final public static function __callStatic($unit, $args) |
|
193
|
|
|
{ |
|
194
|
6 |
|
return new static(static::findUnit($unit), $args[0]); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns unit for given unit name. |
|
199
|
|
|
* The only function to change in child classes. |
|
200
|
|
|
* XXX Should be defined as abstract but `abstract static` is not supported in PHP5. |
|
201
|
|
|
* @param string $name |
|
|
|
|
|
|
202
|
|
|
* @return UnitInterface |
|
203
|
|
|
* @throws InvalidConfigException |
|
204
|
|
|
*/ |
|
205
|
|
|
protected static function findUnit($unit) |
|
206
|
|
|
{ |
|
207
|
|
|
throw new InvalidConfigException('getUnit method must be redefined'); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: