Completed
Push — BoundedQuantityValue ( 1533af )
by Daniel
02:40
created

QuantityValue::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace DataValues;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Class representing a quantity with associated unit.
9
 * The amount is stored as a @see DecimalValue object.
10
 *
11
 * For quantities with a known uncertainty interval, see BoundedQuantityValue.
12
 *
13
 * For simple numeric amounts use @see NumberValue.
14
 *
15
 * @since 0.1
16
 *
17
 * @license GPL-2.0+
18
 * @author Daniel Kinzler
19
 */
20
class QuantityValue extends DataValueObject {
21
22
	/**
23
	 * The quantity's amount
24
	 *
25
	 * @var DecimalValue
26
	 */
27
	protected $amount;
28
29
	/**
30
	 * The quantity's unit identifier (use "1" for unitless quantities).
31
	 *
32
	 * @var string
33
	 */
34
	protected $unit;
35
36
	/**
37
	 * Constructs a new QuantityValue object, representing the given value.
38
	 *
39
	 * @since 0.1
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 self
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
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...
90
	 * @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...
91
	 *
92
	 * @return self
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 ) {
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...
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
		$this->__construct( $amount, $unit );
157
	}
158
159
	/**
160
	 * @see DataValue::getType
161
	 *
162
	 * @since 0.1
163
	 *
164
	 * @return string
165
	 */
166
	public static function getType() {
167
		return 'quantity';
168
	}
169
170
	/**
171
	 * @see DataValue::getSortKey
172
	 *
173
	 * @since 0.1
174
	 *
175
	 * @return float
176
	 */
177
	public function getSortKey() {
178
		return $this->amount->getValueFloat();
179
	}
180
181
	/**
182
	 * Returns the quantity object.
183
	 * @see DataValue::getValue
184
	 *
185
	 * @since 0.1
186
	 *
187
	 * @return self
188
	 */
189
	public function getValue() {
190
		return $this;
191
	}
192
193
	/**
194
	 * Returns the amount represented by this quantity.
195
	 *
196
	 * @since 0.1
197
	 *
198
	 * @return DecimalValue
199
	 */
200
	public function getAmount() {
201
		return $this->amount;
202
	}
203
204
	/**
205
	 * Returns the unit held by this quantity.
206
	 * Unit-less quantities should use "1" as their unit.
207
	 *
208
	 * @since 0.1
209
	 *
210
	 * @return string
211
	 */
212
	public function getUnit() {
213
		return $this->unit;
214
	}
215
216
	/**
217
	 * Returns a transformed value derived from this QuantityValue by applying
218
	 * the given transformation to the amount and the upper and lower bounds.
219
	 * The resulting amount and bounds are rounded to the significant number of
220
	 * digits. Note that for exact quantities (with at least one bound equal to
221
	 * the amount), no rounding is applied (since they are considered to have
222
	 * infinite precision).
223
	 *
224
	 * The transformation is provided as a callback, which must implement a
225
	 * monotonously increasing, fully differentiable function mapping a DecimalValue
226
	 * to a DecimalValue. Typically, it will be a linear transformation applying a
227
	 * factor and an offset.
228
	 *
229
	 * @param string $newUnit The unit of the transformed quantity.
230
	 *
231
	 * @param callable $transformation A callback that implements the desired transformation.
232
	 *        The transformation will be called three times, once for the amount, once
233
	 *        for the lower bound, and once for the upper bound. It must return a DecimalValue.
234
	 *        The first parameter passed to $transformation is the DecimalValue to transform
235
	 *        In addition, any extra parameters passed to transform() will be passed through
236
	 *        to the transformation callback.
237
	 *
238
	 * @param mixed ... Any extra parameters will be passed to the $transformation function.
239
	 *
240
	 * @throws InvalidArgumentException
241
	 * @return self
242
	 */
243
	public function transform( $newUnit, $transformation ) {
244
		if ( !is_callable( $transformation ) ) {
245
			throw new InvalidArgumentException( '$transformation must be callable.' );
246
		}
247
248
		if ( !is_string( $newUnit ) ) {
249
			throw new InvalidArgumentException( '$newUnit must be a string. Use "1" as the unit for unit-less quantities.' );
250
		}
251
252
		if ( $newUnit === '' ) {
253
			throw new InvalidArgumentException( '$newUnit must not be empty. Use "1" as the unit for unit-less quantities.' );
254
		}
255
256
		$oldUnit = $this->unit;
257
258
		if ( $newUnit === null ) {
259
			$newUnit = $oldUnit;
260
		}
261
262
		// Apply transformation by calling the $transform callback.
263
		// The first argument for the callback is the DataValue to transform. In addition,
264
		// any extra arguments given for transform() are passed through.
265
		$args = func_get_args();
266
		array_shift( $args );
267
268
		$args[0] = $this->amount;
269
		$amount = call_user_func_array( $transformation, $args );
270
271
		// use a preliminary QuantityValue to determine the significant number of digits
272
		return new self( $amount, $newUnit );
273
	}
274
275
	public function __toString() {
276
		return $this->amount->getValue()
277
			. ( $this->unit === '1' ? '' : $this->unit );
278
	}
279
280
	/**
281
	 * @see DataValue::getArrayValue
282
	 *
283
	 * @since 0.1
284
	 *
285
	 * @return array
286
	 */
287
	public function getArrayValue() {
288
		return array(
289
			'amount' => $this->amount->getArrayValue(),
290
			'unit' => $this->unit,
291
		);
292
	}
293
294
	/**
295
	 * Constructs a new instance of the DataValue from the provided data.
296
	 * This can round-trip with @see getArrayValue
297
	 *
298
	 * @since 0.1
299
	 *
300
	 * @param mixed $data
301
	 *
302
	 * @return self
303
	 * @throws IllegalValueException
304
	 */
305
	public static function newFromArray( $data ) {
306
		self::requireArrayFields( $data, array( 'amount', 'unit' ) );
307
308
		return new static(
309
			DecimalValue::newFromArray( $data['amount'] ),
310
			$data['unit']
311
		);
312
	}
313
314
	/**
315
	 * @see Comparable::equals
316
	 *
317
	 * @since 0.1
318
	 *
319
	 * @param mixed $target
320
	 *
321
	 * @return bool
322
	 */
323
	public function equals( $target ) {
324
		if ( $this === $target ) {
325
			return true;
326
		}
327
328
		return $target instanceof self
329
			&& $this->toArray() === $target->toArray();
330
	}
331
332
}
333