1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class representing a quantity with associated unit. |
10
|
|
|
* The amount is stored as a @see DecimalValue object. |
11
|
|
|
* |
12
|
|
|
* @see QuantityValue for quantities with a known uncertainty interval. |
13
|
|
|
* For simple numeric amounts use @see NumberValue. |
14
|
|
|
* |
15
|
|
|
* @note UnboundedQuantityValue and QuantityValue both use the value type ID "quantity". |
16
|
|
|
* The fact that we use subclassing to model the bounded vs the unbounded case should be |
17
|
|
|
* considered an implementation detail. |
18
|
|
|
* |
19
|
|
|
* @since 0.1 |
20
|
|
|
* |
21
|
|
|
* @license GPL-2.0+ |
22
|
|
|
* @author Daniel Kinzler |
23
|
|
|
*/ |
24
|
|
|
class UnboundedQuantityValue extends DataValueObject { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The quantity's amount |
28
|
|
|
* |
29
|
|
|
* @var DecimalValue |
30
|
|
|
*/ |
31
|
|
|
protected $amount; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The quantity's unit identifier (use "1" for unitless quantities). |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $unit; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param DecimalValue $amount |
42
|
|
|
* @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
43
|
|
|
* |
44
|
|
|
* @throws IllegalValueException |
45
|
|
|
*/ |
46
|
|
|
public function __construct( DecimalValue $amount, $unit ) { |
47
|
|
|
if ( !is_string( $unit ) ) { |
48
|
|
|
throw new IllegalValueException( '$unit needs to be a string, not ' . gettype( $unit ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ( $unit === '' ) { |
52
|
|
|
throw new IllegalValueException( '$unit can not be an empty string (use "1" for unit-less quantities)' ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->amount = $amount; |
56
|
|
|
$this->unit = $unit; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a QuantityValue representing the given amount. |
61
|
|
|
* |
62
|
|
|
* This is a convenience wrapper around the constructor that accepts native values |
63
|
|
|
* instead of DecimalValue objects. |
64
|
|
|
* |
65
|
|
|
* @note: if the amount or a bound is given as a string, the string must conform |
66
|
|
|
* to the rules defined by @see DecimalValue. |
67
|
|
|
* |
68
|
|
|
* @since 0.1 |
69
|
|
|
* |
70
|
|
|
* @param string|int|float|DecimalValue $number |
71
|
|
|
* @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
72
|
|
|
* |
73
|
|
|
* @return UnboundedQuantityValue |
74
|
|
|
* @throws IllegalValueException |
75
|
|
|
*/ |
76
|
|
|
public static function newFromNumber( $number, $unit = '1' ) { |
77
|
|
|
$number = self::asDecimalValue( 'amount', $number ); |
78
|
|
|
|
79
|
|
|
return new self( $number, $unit ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @see newFromNumber |
84
|
|
|
* |
85
|
|
|
* @deprecated since 0.1, use newFromNumber instead |
86
|
|
|
* |
87
|
|
|
* @param string|int|float|DecimalValue $number |
88
|
|
|
* @param string $unit |
89
|
|
|
* @param string|int|float|DecimalValue|null $upperBound |
|
|
|
|
90
|
|
|
* @param string|int|float|DecimalValue|null $lowerBound |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return UnboundedQuantityValue |
93
|
|
|
*/ |
94
|
|
|
public static function newFromDecimal( $number, $unit = '1' ) { |
95
|
|
|
return self::newFromNumber( $number, $unit ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Converts $number to a DecimalValue if possible and necessary. |
100
|
|
|
* |
101
|
|
|
* @note: if the $number is given as a string, it must conform to the rules |
102
|
|
|
* defined by @see DecimalValue. |
103
|
|
|
* |
104
|
|
|
* @param string $name The variable name to use in exception messages |
105
|
|
|
* @param string|int|float|DecimalValue|null $number |
106
|
|
|
* @param DecimalValue|null $default |
107
|
|
|
* |
108
|
|
|
* @throws IllegalValueException |
109
|
|
|
* @throws InvalidArgumentException |
110
|
|
|
* @return DecimalValue |
111
|
|
|
*/ |
112
|
|
|
protected static function asDecimalValue( $name, $number, DecimalValue $default = null ) { |
113
|
|
|
if ( !is_string( $name ) ) { |
114
|
|
|
throw new InvalidArgumentException( '$name must be a string' ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( $number === null ) { |
118
|
|
|
if ( $default === null ) { |
119
|
|
|
throw new InvalidArgumentException( '$' . $name . ' must not be null' ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$number = $default; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( $number instanceof DecimalValue ) { |
|
|
|
|
126
|
|
|
// nothing to do |
127
|
|
|
} elseif ( is_int( $number ) || is_float( $number ) || is_string( $number ) ) { |
128
|
|
|
$number = new DecimalValue( $number ); |
129
|
|
|
} else { |
130
|
|
|
throw new IllegalValueException( '$' . $name . ' must be a string, int, or float' ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $number; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see Serializable::serialize |
138
|
|
|
* |
139
|
|
|
* @since 0.1 |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function serialize() { |
144
|
|
|
return serialize( array_values( $this->getArrayValue() ) ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @see Serializable::unserialize |
149
|
|
|
* |
150
|
|
|
* @since 0.1 |
151
|
|
|
* |
152
|
|
|
* @param string $data |
153
|
|
|
*/ |
154
|
|
|
public function unserialize( $data ) { |
155
|
|
|
list( $amount, $unit ) = unserialize( $data ); |
156
|
|
|
$amount = DecimalValue::newFromArray( $amount ); |
157
|
|
|
$this->__construct( $amount, $unit ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @see DataValue::getType |
162
|
|
|
* |
163
|
|
|
* @since 0.1 |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public static function getType() { |
168
|
|
|
return 'quantity'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @see DataValue::getSortKey |
173
|
|
|
* |
174
|
|
|
* @since 0.1 |
175
|
|
|
* |
176
|
|
|
* @return float |
177
|
|
|
*/ |
178
|
|
|
public function getSortKey() { |
179
|
|
|
return $this->amount->getValueFloat(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns the quantity object. |
184
|
|
|
* |
185
|
|
|
*@see DataValue::getValue |
186
|
|
|
* |
187
|
|
|
* @since 0.1 |
188
|
|
|
* |
189
|
|
|
* @return UnboundedQuantityValue |
190
|
|
|
*/ |
191
|
|
|
public function getValue() { |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the amount represented by this quantity. |
197
|
|
|
* |
198
|
|
|
* @since 0.1 |
199
|
|
|
* |
200
|
|
|
* @return DecimalValue |
201
|
|
|
*/ |
202
|
|
|
public function getAmount() { |
203
|
|
|
return $this->amount; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns the unit held by this quantity. |
208
|
|
|
* Unit-less quantities should use "1" as their unit. |
209
|
|
|
* |
210
|
|
|
* @since 0.1 |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getUnit() { |
215
|
|
|
return $this->unit; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns a transformed value derived from this QuantityValue by applying |
220
|
|
|
* the given transformation to the amount and the upper and lower bounds. |
221
|
|
|
* The resulting amount and bounds are rounded to the significant number of |
222
|
|
|
* digits. Note that for exact quantities (with at least one bound equal to |
223
|
|
|
* the amount), no rounding is applied (since they are considered to have |
224
|
|
|
* infinite precision). |
225
|
|
|
* |
226
|
|
|
* The transformation is provided as a callback, which must implement a |
227
|
|
|
* monotonously increasing, fully differentiable function mapping a DecimalValue |
228
|
|
|
* to a DecimalValue. Typically, it will be a linear transformation applying a |
229
|
|
|
* factor and an offset. |
230
|
|
|
* |
231
|
|
|
* @param string $newUnit The unit of the transformed quantity. |
232
|
|
|
* |
233
|
|
|
* @param callable $transformation A callback that implements the desired transformation. |
234
|
|
|
* The transformation will be called three times, once for the amount, once |
235
|
|
|
* for the lower bound, and once for the upper bound. It must return a DecimalValue. |
236
|
|
|
* The first parameter passed to $transformation is the DecimalValue to transform |
237
|
|
|
* In addition, any extra parameters passed to transform() will be passed through |
238
|
|
|
* to the transformation callback. |
239
|
|
|
* |
240
|
|
|
* @param mixed ... Any extra parameters will be passed to the $transformation function. |
241
|
|
|
* |
242
|
|
|
* @throws InvalidArgumentException |
243
|
|
|
* @return UnboundedQuantityValue |
244
|
|
|
*/ |
245
|
|
|
public function transform( $newUnit, $transformation ) { |
246
|
|
|
if ( !is_callable( $transformation ) ) { |
247
|
|
|
throw new InvalidArgumentException( '$transformation must be callable.' ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if ( !is_string( $newUnit ) ) { |
251
|
|
|
throw new InvalidArgumentException( '$newUnit must be a string. Use "1" as the unit for unit-less quantities.' ); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if ( $newUnit === '' ) { |
255
|
|
|
throw new InvalidArgumentException( '$newUnit must not be empty. Use "1" as the unit for unit-less quantities.' ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$oldUnit = $this->unit; |
259
|
|
|
|
260
|
|
|
if ( $newUnit === null ) { |
261
|
|
|
$newUnit = $oldUnit; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Apply transformation by calling the $transform callback. |
265
|
|
|
// The first argument for the callback is the DataValue to transform. In addition, |
266
|
|
|
// any extra arguments given for transform() are passed through. |
267
|
|
|
$args = func_get_args(); |
268
|
|
|
array_shift( $args ); |
269
|
|
|
|
270
|
|
|
$args[0] = $this->amount; |
271
|
|
|
$amount = call_user_func_array( $transformation, $args ); |
272
|
|
|
|
273
|
|
|
// use a preliminary QuantityValue to determine the significant number of digits |
274
|
|
|
return new self( $amount, $newUnit ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function __toString() { |
278
|
|
|
return $this->amount->getValue() |
279
|
|
|
. ( $this->unit === '1' ? '' : $this->unit ); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @see DataValue::getArrayValue |
284
|
|
|
* |
285
|
|
|
* @since 0.1 |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function getArrayValue() { |
290
|
|
|
return array( |
291
|
|
|
'amount' => $this->amount->getArrayValue(), |
292
|
|
|
'unit' => $this->unit, |
293
|
|
|
); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Constructs a new instance of the DataValue from the provided data. |
298
|
|
|
* This can round-trip with @see getArrayValue |
299
|
|
|
* |
300
|
|
|
* @since 0.1 |
301
|
|
|
* |
302
|
|
|
* @param mixed $data |
303
|
|
|
* |
304
|
|
|
* @return UnboundedQuantityValue |
305
|
|
|
* @throws IllegalValueException |
306
|
|
|
*/ |
307
|
|
|
public static function newFromArray( $data ) { |
308
|
|
|
self::requireArrayFields( $data, array( 'amount', 'unit' ) ); |
309
|
|
|
|
310
|
|
|
return new static( |
311
|
|
|
DecimalValue::newFromArray( $data['amount'] ), |
312
|
|
|
$data['unit'] |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @see Comparable::equals |
318
|
|
|
* |
319
|
|
|
* @since 0.1 |
320
|
|
|
* |
321
|
|
|
* @param mixed $target |
322
|
|
|
* |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
public function equals( $target ) { |
326
|
|
|
if ( $this === $target ) { |
327
|
|
|
return true; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $target instanceof self |
331
|
|
|
&& $this->toArray() === $target->toArray(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.