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 |
32
|
|
|
* @var float|int|string $quantity |
33
|
|
|
*/ |
34
|
8 |
|
private function __construct(UnitInterface $unit, $quantity) |
35
|
|
|
{ |
36
|
8 |
|
$this->unit = $unit; |
37
|
8 |
|
$this->quantity = $quantity; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates quantity with same unit. |
42
|
|
|
* Optimized to return this if same quantity. |
43
|
|
|
* @var float|int|string |
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 isPositive() |
96
|
|
|
{ |
97
|
1 |
|
return $this->quantity > 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
final public function isNegative() |
104
|
|
|
{ |
105
|
1 |
|
return $this->quantity < 0; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
final public function isConvertible(UnitInterface $unit) |
112
|
|
|
{ |
113
|
1 |
|
return $this->unit->isConvertible($unit); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
3 |
|
final public function convert(UnitInterface $unit) |
120
|
|
|
{ |
121
|
3 |
|
$res = $this->unit->convert($unit, $this->quantity); |
122
|
|
|
|
123
|
3 |
|
return $this->create($res); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
final public function add(QuantityInterface $addend) |
130
|
|
|
{ |
131
|
|
|
$arg = $addend->convert($this->unit); |
132
|
|
|
$res = $this->getCalculator()->add($this->quantity, $arg); |
|
|
|
|
133
|
|
|
|
134
|
|
|
return $this->create($res); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
final public function subtract(QuantityInterface $subtrahend) |
141
|
|
|
{ |
142
|
|
|
$arg = $subtrahend->convert($this->unit); |
143
|
|
|
$res = $this->getCalculator()->subtract($this->quantity, $arg); |
|
|
|
|
144
|
|
|
|
145
|
|
|
return $this->create($res); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
final public function multiply($multiplier) |
152
|
|
|
{ |
153
|
|
|
$res = $this->getCalculator()->multiply($this->quantity, $multiplier); |
154
|
|
|
|
155
|
|
|
return $this->create($res); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
final public function divide($divisor) |
162
|
|
|
{ |
163
|
|
|
$res = $this->getCalculator()->divide($this->quantity, $divisor); |
164
|
|
|
|
165
|
|
|
return $this->create($res); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
final public function ceil() |
172
|
|
|
{ |
173
|
|
|
$res = $this->getCalculator()->ceil($this->quantity); |
174
|
|
|
|
175
|
|
|
return $this->create($res); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
final public function floor() |
182
|
|
|
{ |
183
|
|
|
$res = $this->getCalculator()->floor($this->quantity); |
184
|
|
|
|
185
|
|
|
return $this->create($res); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
final public function round($roundingMode) |
192
|
|
|
{ |
193
|
|
|
$res = $this->getCalculator()->round($this->quantity, $roundingMode); |
194
|
|
|
|
195
|
|
|
return $this->create($res); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
final public function absolute() |
202
|
|
|
{ |
203
|
|
|
$res = $this->getCalculator()->absolute($this->quantity); |
204
|
|
|
|
205
|
|
|
return $this->create($res); |
206
|
|
|
} |
207
|
|
|
|
208
|
8 |
|
final public static function __callStatic($unit, $args) |
209
|
|
|
{ |
210
|
8 |
|
return new static(static::findUnit($unit), $args[0]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns unit for given unit name. |
215
|
|
|
* The only function to change in child classes. |
216
|
|
|
* XXX Should be defined as abstract but `abstract static` is not supported in PHP5. |
217
|
|
|
* @param string $name |
|
|
|
|
218
|
|
|
* @throws InvalidConfigException |
219
|
|
|
* @return UnitInterface |
220
|
|
|
*/ |
221
|
|
|
protected static function findUnit($unit) |
222
|
|
|
{ |
223
|
|
|
throw new InvalidConfigException('getUnit method must be redefined'); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
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: