Completed
Pull Request — master (#66)
by Daniel
06:14 queued 03:05
created

QuantityValue::getSignificantFigures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 BoundedQuantityValue for quantities with a known uncertainty interval.
13
 * For simple numeric amounts use @see NumberValue.
14
 *
15
 * @note BoundedQuantityValue and plain 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 QuantityValue 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, $unused = 'nothing' ) {
47
		if ( $unused !== 'nothing' ) {
48
			throw new LogicException( 'Constructor called with old signature. '
49
				. 'Perhaps you want to construct a BoundedQuantityValue instead.' );
50
		}
51
52
		if ( !is_string( $unit ) ) {
53
			throw new IllegalValueException( '$unit needs to be a string, not ' . gettype( $unit ) );
54
		}
55
56
		if ( $unit === '' ) {
57
			throw new IllegalValueException( '$unit can not be an empty string (use "1" for unit-less quantities)' );
58
		}
59
60
		$this->amount = $amount;
61
		$this->unit = $unit;
62
	}
63
64
	/**
65
	 * Returns a QuantityValue representing the given amount.
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 $number
76
	 * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities.
77
	 *
78
	 * @return self
79
	 * @throws IllegalValueException
80
	 */
81
	public static function newFromNumber( $number, $unit = '1' ) {
82
		$number = self::asDecimalValue( 'amount', $number );
83
84
		return new self( $number, $unit );
85
	}
86
87
	/**
88
	 * @see newFromNumber
89
	 *
90
	 * @deprecated since 0.1, use newFromNumber instead
91
	 *
92
	 * @param string|int|float|DecimalValue $number
93
	 * @param string $unit
94
	 * @param string|int|float|DecimalValue|null $upperBound
0 ignored issues
show
Bug introduced by
There is no parameter named $upperBound. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
	 * @param string|int|float|DecimalValue|null $lowerBound
0 ignored issues
show
Bug introduced by
There is no parameter named $lowerBound. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
	 *
97
	 * @return self
98
	 */
99
	public static function newFromDecimal( $number, $unit = '1' ) {
100
		return self::newFromNumber( $number, $unit );
101
	}
102
103
	/**
104
	 * Converts $number to a DecimalValue if possible and necessary.
105
	 *
106
	 * @note: if the $number is given as a string, it must conform to the rules
107
	 *        defined by @see DecimalValue.
108
	 *
109
	 * @param string $name The variable name to use in exception messages
110
	 * @param string|int|float|DecimalValue|null $number
111
	 * @param DecimalValue|null $default
112
	 *
113
	 * @throws IllegalValueException
114
	 * @throws InvalidArgumentException
115
	 * @return DecimalValue
116
	 */
117
	protected static function asDecimalValue( $name, $number, DecimalValue $default = null ) {
118
		if ( !is_string( $name ) ) {
119
			throw new InvalidArgumentException( '$name must be a string' );
120
		}
121
122
		if ( $number === null ) {
123
			if ( $default === null ) {
124
				throw new InvalidArgumentException( '$' . $name . ' must not be null' );
125
			}
126
127
			$number = $default;
128
		}
129
130
		if ( $number instanceof DecimalValue ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
131
			// nothing to do
132
		} elseif ( is_int( $number ) || is_float( $number ) || is_string( $number ) ) {
133
			$number = new DecimalValue( $number );
134
		} else {
135
			throw new IllegalValueException( '$' . $name . '  must be a string, int, or float' );
136
		}
137
138
		return $number;
139
	}
140
141
	/**
142
	 * @see Serializable::serialize
143
	 *
144
	 * @since 0.1
145
	 *
146
	 * @return string
147
	 */
148
	public function serialize() {
149
		return serialize( array_values( $this->getArrayValue() ) );
150
	}
151
152
	/**
153
	 * @see Serializable::unserialize
154
	 *
155
	 * @since 0.1
156
	 *
157
	 * @param string $data
158
	 */
159
	public function unserialize( $data ) {
160
		list( $amount, $unit ) = unserialize( $data );
161
		$amount = DecimalValue::newFromArray( $amount );
162
		$this->__construct( $amount, $unit );
163
	}
164
165
	/**
166
	 * @see DataValue::getType
167
	 *
168
	 * @since 0.1
169
	 *
170
	 * @return string
171
	 */
172
	public static function getType() {
173
		return 'quantity';
174
	}
175
176
	/**
177
	 * @see DataValue::getSortKey
178
	 *
179
	 * @since 0.1
180
	 *
181
	 * @return float
182
	 */
183
	public function getSortKey() {
184
		return $this->amount->getValueFloat();
185
	}
186
187
	/**
188
	 * Returns the quantity object.
189
	 * @see DataValue::getValue
190
	 *
191
	 * @since 0.1
192
	 *
193
	 * @return self
194
	 */
195
	public function getValue() {
196
		return $this;
197
	}
198
199
	/**
200
	 * Returns the amount represented by this quantity.
201
	 *
202
	 * @since 0.1
203
	 *
204
	 * @return DecimalValue
205
	 */
206
	public function getAmount() {
207
		return $this->amount;
208
	}
209
210
	/**
211
	 * Returns the unit held by this quantity.
212
	 * Unit-less quantities should use "1" as their unit.
213
	 *
214
	 * @since 0.1
215
	 *
216
	 * @return string
217
	 */
218
	public function getUnit() {
219
		return $this->unit;
220
	}
221
222
	/**
223
	 * Returns a transformed value derived from this QuantityValue by applying
224
	 * the given transformation to the amount and the upper and lower bounds.
225
	 * The resulting amount and bounds are rounded to the significant number of
226
	 * digits. Note that for exact quantities (with at least one bound equal to
227
	 * the amount), no rounding is applied (since they are considered to have
228
	 * infinite precision).
229
	 *
230
	 * The transformation is provided as a callback, which must implement a
231
	 * monotonously increasing, fully differentiable function mapping a DecimalValue
232
	 * to a DecimalValue. Typically, it will be a linear transformation applying a
233
	 * factor and an offset.
234
	 *
235
	 * @param string $newUnit The unit of the transformed quantity.
236
	 *
237
	 * @param callable $transformation A callback that implements the desired transformation.
238
	 *        The transformation will be called three times, once for the amount, once
239
	 *        for the lower bound, and once for the upper bound. It must return a DecimalValue.
240
	 *        The first parameter passed to $transformation is the DecimalValue to transform
241
	 *        In addition, any extra parameters passed to transform() will be passed through
242
	 *        to the transformation callback.
243
	 *
244
	 * @param mixed ... Any extra parameters will be passed to the $transformation function.
245
	 *
246
	 * @throws InvalidArgumentException
247
	 * @return self
248
	 */
249
	public function transform( $newUnit, $transformation ) {
250
		if ( !is_callable( $transformation ) ) {
251
			throw new InvalidArgumentException( '$transformation must be callable.' );
252
		}
253
254
		if ( !is_string( $newUnit ) ) {
255
			throw new InvalidArgumentException( '$newUnit must be a string. Use "1" as the unit for unit-less quantities.' );
256
		}
257
258
		if ( $newUnit === '' ) {
259
			throw new InvalidArgumentException( '$newUnit must not be empty. Use "1" as the unit for unit-less quantities.' );
260
		}
261
262
		$oldUnit = $this->unit;
263
264
		if ( $newUnit === null ) {
265
			$newUnit = $oldUnit;
266
		}
267
268
		// Apply transformation by calling the $transform callback.
269
		// The first argument for the callback is the DataValue to transform. In addition,
270
		// any extra arguments given for transform() are passed through.
271
		$args = func_get_args();
272
		array_shift( $args );
273
274
		$args[0] = $this->amount;
275
		$amount = call_user_func_array( $transformation, $args );
276
277
		// use a preliminary QuantityValue to determine the significant number of digits
278
		return new self( $amount, $newUnit );
279
	}
280
281
	public function __toString() {
282
		return $this->amount->getValue()
283
			. ( $this->unit === '1' ? '' : $this->unit );
284
	}
285
286
	/**
287
	 * @see DataValue::getArrayValue
288
	 *
289
	 * @since 0.1
290
	 *
291
	 * @return array
292
	 */
293
	public function getArrayValue() {
294
		return array(
295
			'amount' => $this->amount->getArrayValue(),
296
			'unit' => $this->unit,
297
		);
298
	}
299
300
	/**
301
	 * Constructs a new instance of the DataValue from the provided data.
302
	 * This can round-trip with @see getArrayValue
303
	 *
304
	 * @since 0.1
305
	 *
306
	 * @param mixed $data
307
	 *
308
	 * @return self
309
	 * @throws IllegalValueException
310
	 */
311
	public static function newFromArray( $data ) {
312
		self::requireArrayFields( $data, array( 'amount', 'unit' ) );
313
314
		return new static(
315
			DecimalValue::newFromArray( $data['amount'] ),
316
			$data['unit']
317
		);
318
	}
319
320
	/**
321
	 * @see Comparable::equals
322
	 *
323
	 * @since 0.1
324
	 *
325
	 * @param mixed $target
326
	 *
327
	 * @return bool
328
	 */
329
	public function equals( $target ) {
330
		if ( $this === $target ) {
331
			return true;
332
		}
333
334
		return $target instanceof self
335
			&& $this->toArray() === $target->toArray();
336
	}
337
338
}
339