Skip to content

Commit

Permalink
Merge pull request #106 from DataValues/unbreakNewFromArray
Browse files Browse the repository at this point in the history
Better documentation for the DataValue::newFromArray contract
  • Loading branch information
manicki authored Jun 26, 2017
2 parents 82a070e + 9a7a567 commit 14ee984
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
27 changes: 12 additions & 15 deletions src/DataValues/DecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DecimalValue extends DataValueObject {
* @param string|int|float $value If given as a string, the value must match
* QUANTITY_VALUE_PATTERN. The leading plus sign is optional.
*
* @throws IllegalValueException
* @throws InvalidArgumentException
*/
public function __construct( $value ) {
if ( is_int( $value ) || is_float( $value ) ) {
Expand Down Expand Up @@ -112,8 +112,6 @@ private function convertToDecimal( $number ) {
/**
* Compares this DecimalValue to another DecimalValue.
*
* @since 0.1
*
* @param self $that
*
* @throws LogicException
Expand Down Expand Up @@ -227,8 +225,6 @@ public function getValue() {
/**
* Returns the sign of the amount (+ or -).
*
* @since 0.1
*
* @return string "+" or "-".
*/
public function getSign() {
Expand Down Expand Up @@ -288,8 +284,6 @@ public function computeAbsolute() {
* Returns the integer part of the value, that is, the part before the decimal point,
* without the sign.
*
* @since 0.1
*
* @return string
*/
public function getIntegerPart() {
Expand All @@ -306,8 +300,6 @@ public function getIntegerPart() {
* Returns the fractional part of the value, that is, the part after the decimal point,
* if any.
*
* @since 0.1
*
* @return string
*/
public function getFractionalPart() {
Expand Down Expand Up @@ -342,8 +334,6 @@ public function getTrimmed() {
* Returns the value held by this object, as a float.
* Equivalent to floatval( $this->getvalue() ).
*
* @since 0.1
*
* @return float
*/
public function getValueFloat() {
Expand All @@ -360,13 +350,20 @@ public function getArrayValue() {
}

/**
* Constructs a new instance of the DataValue from the provided data.
* This can round-trip with @see getArrayValue
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
* This is expected to round-trip with @see getArrayValue.
*
* @deprecated since 0.8.3. Static DataValue::newFromArray constructors like this are
* underspecified (not in the DataValue interface), and misleadingly named (should be named
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
*
* @param string|int|float $data
* @param mixed $data Warning! Even if this is expected to be a value as returned by
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
* this. This is not guaranteed to be a string!
*
* @throws InvalidArgumentException if $data is not in the expected format. Subclasses of
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
* @return self
* @throws IllegalValueException
*/
public static function newFromArray( $data ) {
return new static( $data );
Expand Down
19 changes: 13 additions & 6 deletions src/DataValues/UnboundedQuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct( DecimalValue $amount, $unit ) {
* @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities.
*
* @return self
* @throws IllegalValueException
* @throws InvalidArgumentException
*/
public static function newFromNumber( $amount, $unit = '1' ) {
$amount = self::asDecimalValue( 'amount', $amount );
Expand All @@ -82,7 +82,6 @@ public static function newFromNumber( $amount, $unit = '1' ) {
* @param string|int|float|DecimalValue|null $number
* @param DecimalValue|null $default
*
* @throws IllegalValueException
* @throws InvalidArgumentException
* @return DecimalValue
*/
Expand Down Expand Up @@ -248,13 +247,21 @@ public function getArrayValue() {

/**
* Static helper capable of constructing both unbounded and bounded quantity value objects,
* depending on the serialization provided. Required for @see DataValueDeserializer. This can
* round-trip with both @see self::getArrayValue as well as @see QuantityValue::getArrayValue.
* depending on the serialization provided. Required for @see DataValueDeserializer.
* This is expected to round-trip with both @see getArrayValue as well as
* @see QuantityValue::getArrayValue.
*
* @param mixed $data
* @deprecated since 0.8.3. Static DataValue::newFromArray constructors like this are
* underspecified (not in the DataValue interface), and misleadingly named (should be named
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
*
* @param mixed $data Warning! Even if this is expected to be a value as returned by
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
* this. This is not even guaranteed to be an array!
*
* @throws IllegalValueException if $data is not in the expected format. Subclasses of
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
* @return self|QuantityValue Either an unbounded or bounded quantity value object.
* @throws IllegalValueException
*/
public static function newFromArray( $data ) {
self::requireArrayFields( $data, [ 'amount', 'unit' ] );
Expand Down

0 comments on commit 14ee984

Please sign in to comment.