1 | <?php |
||
23 | class QuantityValue extends UnboundedQuantityValue { |
||
24 | |||
25 | /** |
||
26 | * The quantity's upper bound |
||
27 | * |
||
28 | * @var DecimalValue |
||
29 | */ |
||
30 | private $upperBound; |
||
31 | |||
32 | /** |
||
33 | * The quantity's lower bound |
||
34 | * |
||
35 | * @var DecimalValue |
||
36 | */ |
||
37 | private $lowerBound; |
||
38 | |||
39 | /** |
||
40 | * @param DecimalValue $amount |
||
41 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
42 | * @param DecimalValue $upperBound The upper bound of the quantity, inclusive. |
||
43 | * @param DecimalValue $lowerBound The lower bound of the quantity, inclusive. |
||
44 | * |
||
45 | * @throws IllegalValueException |
||
46 | */ |
||
47 | public function __construct( DecimalValue $amount, $unit, DecimalValue $upperBound, DecimalValue $lowerBound ) { |
||
61 | |||
62 | /** |
||
63 | * Returns a QuantityValue representing the given amount. |
||
64 | * If no upper or lower bound is given, the amount is assumed to be absolutely exact, |
||
65 | * that is, the amount itself will be used as the upper and lower bound. |
||
66 | * |
||
67 | * This is a convenience wrapper around the constructor that accepts native values |
||
68 | * instead of DecimalValue objects. |
||
69 | * |
||
70 | * @note: if the amount or a bound is given as a string, the string must conform |
||
71 | * to the rules defined by @see DecimalValue. |
||
72 | * |
||
73 | * @since 0.1 |
||
74 | * |
||
75 | * @param string|int|float|DecimalValue $amount |
||
76 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
77 | * @param string|int|float|DecimalValue|null $upperBound |
||
78 | * @param string|int|float|DecimalValue|null $lowerBound |
||
79 | * |
||
80 | * @return self |
||
81 | * @throws IllegalValueException |
||
82 | */ |
||
83 | public static function newFromNumber( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
90 | |||
91 | /** |
||
92 | * @see newFromNumber |
||
93 | * |
||
94 | * @deprecated since 0.1, use newFromNumber instead |
||
95 | * |
||
96 | * @param string|int|float|DecimalValue $amount |
||
97 | * @param string $unit |
||
98 | * @param string|int|float|DecimalValue|null $upperBound |
||
99 | * @param string|int|float|DecimalValue|null $lowerBound |
||
100 | * |
||
101 | * @return self |
||
102 | */ |
||
103 | public static function newFromDecimal( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
106 | |||
107 | /** |
||
108 | * @see Serializable::serialize |
||
109 | * |
||
110 | * @since 0.1 |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function serialize() { |
||
122 | |||
123 | /** |
||
124 | * @see Serializable::unserialize |
||
125 | * |
||
126 | * @since 0.1 |
||
127 | * |
||
128 | * @param string $data |
||
129 | */ |
||
130 | public function unserialize( $data ) { |
||
134 | |||
135 | /** |
||
136 | * Returns this quantity's upper bound. |
||
137 | * |
||
138 | * @since 0.1 |
||
139 | * |
||
140 | * @return DecimalValue |
||
141 | */ |
||
142 | public function getUpperBound() { |
||
145 | |||
146 | /** |
||
147 | * Returns this quantity's lower bound. |
||
148 | * |
||
149 | * @since 0.1 |
||
150 | * |
||
151 | * @return DecimalValue |
||
152 | */ |
||
153 | public function getLowerBound() { |
||
156 | |||
157 | /** |
||
158 | * Returns the size of the uncertainty interval. |
||
159 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
160 | * |
||
161 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
162 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
163 | * normal distribution that is required to cover the 95th percentile. |
||
164 | * |
||
165 | * @since 0.1 |
||
166 | * |
||
167 | * @return float |
||
168 | */ |
||
169 | public function getUncertainty() { |
||
172 | |||
173 | /** |
||
174 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
175 | * to the raw amount for a rough representation of the uncertainty interval, |
||
176 | * as in "amount +/- offset". |
||
177 | * |
||
178 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
179 | * |
||
180 | * @todo Should be factored out into a separate QuantityMath class. |
||
181 | * |
||
182 | * @since 0.1 |
||
183 | * |
||
184 | * @return DecimalValue |
||
185 | */ |
||
186 | public function getUncertaintyMargin() { |
||
195 | |||
196 | /** |
||
197 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
198 | * last significant digit in the amount-string. The value returned by this |
||
199 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
200 | * |
||
201 | * @example: if two digits after the decimal point are significant, this |
||
202 | * returns -2. |
||
203 | * |
||
204 | * @example: if the last two digits before the decimal point are insignificant, |
||
205 | * this returns 2. |
||
206 | * |
||
207 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
208 | * and can be misleading. |
||
209 | * |
||
210 | * @todo Should be factored out into a separate QuantityMath class. |
||
211 | * |
||
212 | * @since 0.1 |
||
213 | * |
||
214 | * @return int |
||
215 | */ |
||
216 | public function getOrderOfUncertainty() { |
||
238 | |||
239 | /** |
||
240 | * Returns the number of significant figures in the amount-string, |
||
241 | * counting the decimal point, but not counting the leading sign. |
||
242 | * |
||
243 | * Note that this calculation assumes a symmetric uncertainty interval, and can be misleading |
||
244 | * |
||
245 | * @since 0.1 |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | public function getSignificantFigures() { |
||
260 | |||
261 | /** |
||
262 | * Returns the unit held by this quantity. |
||
263 | * Unit-less quantities should use "1" as their unit. |
||
264 | * |
||
265 | * @since 0.1 |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getUnit() { |
||
272 | |||
273 | /** |
||
274 | * Returns a transformed value derived from this QuantityValue by applying |
||
275 | * the given transformation to the amount and the upper and lower bounds. |
||
276 | * The resulting amount and bounds are rounded to the significant number of |
||
277 | * digits. Note that for exact quantities (with at least one bound equal to |
||
278 | * the amount), no rounding is applied (since they are considered to have |
||
279 | * infinite precision). |
||
280 | * |
||
281 | * The transformation is provided as a callback, which must implement a |
||
282 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
283 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
284 | * factor and an offset. |
||
285 | * |
||
286 | * @param string $newUnit The unit of the transformed quantity. |
||
287 | * |
||
288 | * @param callable $transformation A callback that implements the desired transformation. |
||
289 | * The transformation will be called three times, once for the amount, once |
||
290 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
291 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
292 | * In addition, any extra parameters passed to transform() will be passed through |
||
293 | * to the transformation callback. |
||
294 | * |
||
295 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
296 | * |
||
297 | * @todo share more code with UnboundQuantityValue::transform. |
||
298 | * @todo Should be factored out into a separate QuantityMath class. |
||
299 | * |
||
300 | * @throws InvalidArgumentException |
||
301 | * @return self |
||
302 | */ |
||
303 | public function transform( $newUnit, $transformation ) { |
||
350 | |||
351 | public function __toString() { |
||
358 | |||
359 | /** |
||
360 | * @see DataValue::getArrayValue |
||
361 | * |
||
362 | * @since 0.1 |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | public function getArrayValue() { |
||
374 | |||
375 | /** |
||
376 | * Constructs a new instance of the DataValue from the provided data. |
||
377 | * This can round-trip with @see getArrayValue |
||
378 | * |
||
379 | * @note: if the upperBound or lowerBound field is missing from $data, |
||
380 | * this returns an UnboundedQuantityValue, not a QuantityValue! |
||
381 | * |
||
382 | * @since 0.1 |
||
383 | * |
||
384 | * @param mixed $data |
||
385 | * |
||
386 | * @return UnboundedQuantityValue |
||
387 | * @throws IllegalValueException |
||
388 | */ |
||
389 | public static function newFromArray( $data ) { |
||
404 | |||
405 | } |
||
406 |
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: