1 | <?php |
||
18 | class QuantityValue extends DataValueObject { |
||
19 | |||
20 | /** |
||
21 | * The quantity's amount |
||
22 | * |
||
23 | * @var DecimalValue |
||
24 | */ |
||
25 | private $amount; |
||
26 | |||
27 | /** |
||
28 | * The quantity's unit identifier (use "1" for unitless quantities). |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $unit; |
||
33 | |||
34 | /** |
||
35 | * The quantity's upper bound |
||
36 | * |
||
37 | * @var DecimalValue |
||
38 | */ |
||
39 | private $upperBound; |
||
40 | |||
41 | /** |
||
42 | * The quantity's lower bound |
||
43 | * |
||
44 | * @var DecimalValue |
||
45 | */ |
||
46 | private $lowerBound; |
||
47 | |||
48 | /** |
||
49 | * Constructs a new QuantityValue object, representing the given value. |
||
50 | * |
||
51 | * @since 0.1 |
||
52 | * |
||
53 | * @param DecimalValue $amount |
||
54 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
55 | * @param DecimalValue $upperBound The upper bound of the quantity, inclusive. |
||
56 | * @param DecimalValue $lowerBound The lower bound of the quantity, inclusive. |
||
57 | * |
||
58 | * @throws IllegalValueException |
||
59 | */ |
||
60 | public function __construct( DecimalValue $amount, $unit, DecimalValue $upperBound, DecimalValue $lowerBound ) { |
||
78 | |||
79 | /** |
||
80 | * Returns a QuantityValue representing the given amount. |
||
81 | * If no upper or lower bound is given, the amount is assumed to be absolutely exact, |
||
82 | * that is, the amount itself will be used as the upper and lower bound. |
||
83 | * |
||
84 | * This is a convenience wrapper around the constructor that accepts native values |
||
85 | * instead of DecimalValue objects. |
||
86 | * |
||
87 | * @note: if the amount or a bound is given as a string, the string must conform |
||
88 | * to the rules defined by @see DecimalValue. |
||
89 | * |
||
90 | * @since 0.1 |
||
91 | * |
||
92 | * @param string|int|float|DecimalValue $amount |
||
93 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
94 | * @param string|int|float|DecimalValue|null $upperBound |
||
95 | * @param string|int|float|DecimalValue|null $lowerBound |
||
96 | * |
||
97 | * @return self |
||
98 | * @throws IllegalValueException |
||
99 | */ |
||
100 | public static function newFromNumber( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
107 | |||
108 | /** |
||
109 | * Converts $number to a DecimalValue if possible and necessary. |
||
110 | * |
||
111 | * @note: if the $number is given as a string, it must conform to the rules |
||
112 | * defined by @see DecimalValue. |
||
113 | * |
||
114 | * @param string $name The variable name to use in exception messages |
||
115 | * @param string|int|float|DecimalValue|null $number |
||
116 | * @param DecimalValue|null $default |
||
117 | * |
||
118 | * @throws IllegalValueException |
||
119 | * @throws InvalidArgumentException |
||
120 | * @return DecimalValue |
||
121 | */ |
||
122 | private static function asDecimalValue( $name, $number, DecimalValue $default = null ) { |
||
145 | |||
146 | /** |
||
147 | * @see Serializable::serialize |
||
148 | * |
||
149 | * @since 0.1 |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function serialize() { |
||
161 | |||
162 | /** |
||
163 | * @see Serializable::unserialize |
||
164 | * |
||
165 | * @since 0.1 |
||
166 | * |
||
167 | * @param string $data |
||
168 | */ |
||
169 | public function unserialize( $data ) { |
||
173 | |||
174 | /** |
||
175 | * @see DataValue::getType |
||
176 | * |
||
177 | * @since 0.1 |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public static function getType() { |
||
184 | |||
185 | /** |
||
186 | * @see DataValue::getSortKey |
||
187 | * |
||
188 | * @since 0.1 |
||
189 | * |
||
190 | * @return float |
||
191 | */ |
||
192 | public function getSortKey() { |
||
195 | |||
196 | /** |
||
197 | * Returns the quantity object. |
||
198 | * @see DataValue::getValue |
||
199 | * |
||
200 | * @since 0.1 |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | public function getValue() { |
||
207 | |||
208 | /** |
||
209 | * Returns the amount represented by this quantity. |
||
210 | * |
||
211 | * @since 0.1 |
||
212 | * |
||
213 | * @return DecimalValue |
||
214 | */ |
||
215 | public function getAmount() { |
||
218 | |||
219 | /** |
||
220 | * Returns this quantity's upper bound. |
||
221 | * |
||
222 | * @since 0.1 |
||
223 | * |
||
224 | * @return DecimalValue |
||
225 | */ |
||
226 | public function getUpperBound() { |
||
229 | |||
230 | /** |
||
231 | * Returns this quantity's lower bound. |
||
232 | * |
||
233 | * @since 0.1 |
||
234 | * |
||
235 | * @return DecimalValue |
||
236 | */ |
||
237 | public function getLowerBound() { |
||
240 | |||
241 | /** |
||
242 | * Returns the size of the uncertainty interval. |
||
243 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
244 | * |
||
245 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
246 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
247 | * normal distribution that is required to cover the 95th percentile. |
||
248 | * |
||
249 | * @since 0.1 |
||
250 | * |
||
251 | * @return float |
||
252 | */ |
||
253 | public function getUncertainty() { |
||
256 | |||
257 | /** |
||
258 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
259 | * to the raw amount for a rough representation of the uncertainty interval, |
||
260 | * as in "amount +/- offset". |
||
261 | * |
||
262 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
263 | * |
||
264 | * @since 0.1 |
||
265 | * |
||
266 | * @return DecimalValue |
||
267 | */ |
||
268 | public function getUncertaintyMargin() { |
||
277 | |||
278 | /** |
||
279 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
280 | * last significant digit in the amount-string. The value returned by this |
||
281 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
282 | * |
||
283 | * @example: if two digits after the decimal point are significant, this |
||
284 | * returns -2. |
||
285 | * |
||
286 | * @example: if the last two digits before the decimal point are insignificant, |
||
287 | * this returns 2. |
||
288 | * |
||
289 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
290 | * and can be misleading. |
||
291 | * |
||
292 | * @since 0.1 |
||
293 | * |
||
294 | * @return int |
||
295 | */ |
||
296 | public function getOrderOfUncertainty() { |
||
318 | |||
319 | /** |
||
320 | * Returns the unit held by this quantity. |
||
321 | * Unit-less quantities should use "1" as their unit. |
||
322 | * |
||
323 | * @since 0.1 |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getUnit() { |
||
330 | |||
331 | /** |
||
332 | * Returns a transformed value derived from this QuantityValue by applying |
||
333 | * the given transformation to the amount and the upper and lower bounds. |
||
334 | * The resulting amount and bounds are rounded to the significant number of |
||
335 | * digits. Note that for exact quantities (with at least one bound equal to |
||
336 | * the amount), no rounding is applied (since they are considered to have |
||
337 | * infinite precision). |
||
338 | * |
||
339 | * The transformation is provided as a callback, which must implement a |
||
340 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
341 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
342 | * factor and an offset. |
||
343 | * |
||
344 | * @param string $newUnit The unit of the transformed quantity. |
||
345 | * |
||
346 | * @param callable $transformation A callback that implements the desired transformation. |
||
347 | * The transformation will be called three times, once for the amount, once |
||
348 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
349 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
350 | * In addition, any extra parameters passed to transform() will be passed through |
||
351 | * to the transformation callback. |
||
352 | * |
||
353 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
354 | * |
||
355 | * @throws InvalidArgumentException |
||
356 | * @return self |
||
357 | */ |
||
358 | public function transform( $newUnit, $transformation ) { |
||
395 | |||
396 | public function __toString() { |
||
403 | |||
404 | /** |
||
405 | * @see DataValue::getArrayValue |
||
406 | * |
||
407 | * @since 0.1 |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | public function getArrayValue() { |
||
419 | |||
420 | /** |
||
421 | * Constructs a new instance of the DataValue from the provided data. |
||
422 | * This can round-trip with @see getArrayValue |
||
423 | * |
||
424 | * @since 0.1 |
||
425 | * |
||
426 | * @param mixed $data |
||
427 | * |
||
428 | * @return self |
||
429 | * @throws IllegalValueException |
||
430 | */ |
||
431 | public static function newFromArray( $data ) { |
||
441 | |||
442 | /** |
||
443 | * @see Comparable::equals |
||
444 | * |
||
445 | * @since 0.1 |
||
446 | * |
||
447 | * @param mixed $target |
||
448 | * |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function equals( $target ) { |
||
459 | |||
460 | } |
||
461 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: