Skip to content

Commit

Permalink
Merge pull request #67 from smalyshev/master
Browse files Browse the repository at this point in the history
Make public function to convert numeric string or value to DecimalValue.
  • Loading branch information
Daniel Kinzler committed Jul 9, 2016
2 parents 49a5fba + 2d8bb74 commit 37c7f49
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
24 changes: 4 additions & 20 deletions src/DataValues/DecimalMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ public function __construct( $useBC = null ) {
$this->useBC = $useBC;
}

/**
* @param int|float|string $number
*
* @return DecimalValue
*/
private function makeDecimalValue( $number ) {

if ( is_string( $number ) && $number !== '' ) {
if ( $number[0] !== '-' && $number[0] !== '+' ) {
$number = '+' . $number;
}
}

return new DecimalValue( $number );
}

/**
* Whether this is using the bcmath library.
*
Expand All @@ -83,7 +67,7 @@ public function product( DecimalValue $a, DecimalValue $b ) {
$product = $a->getValueFloat() * $b->getValueFloat();
}

return $this->makeDecimalValue( $product );
return new DecimalValue( $product );
}

/**
Expand All @@ -102,7 +86,7 @@ public function sum( DecimalValue $a, DecimalValue $b ) {
$sum = $a->getValueFloat() + $b->getValueFloat();
}

return $this->makeDecimalValue( $sum );
return new DecimalValue( $sum );
}

/**
Expand All @@ -121,7 +105,7 @@ public function min( DecimalValue $a, DecimalValue $b ) {
$min = $comp > 0 ? $b : $a;
} else {
$min = min( $a->getValueFloat(), $b->getValueFloat() );
$min = $this->makeDecimalValue( $min );
$min = new DecimalValue( $min );
}

return $min;
Expand All @@ -143,7 +127,7 @@ public function max( DecimalValue $a, DecimalValue $b ) {
$max = $comp > 0 ? $a : $b;
} else {
$max = max( $a->getValueFloat(), $b->getValueFloat() );
$max = $this->makeDecimalValue( $max );
$max = new DecimalValue( $max );
}

return $max;
Expand Down
4 changes: 3 additions & 1 deletion src/DataValues/DecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function __construct( $value ) {
}

$value = trim( $value );

if ( $value !== '' && $value[0] !== '-' && $value[0] !== '+' ) {
$value = '+' . $value;
}
if ( strlen( $value ) > 127 ) {
throw new IllegalValueException( 'Value must be at most 127 characters long.' );
}
Expand Down
3 changes: 2 additions & 1 deletion tests/DataValues/DecimalValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function validConstructorArgumentsProvider() {
$argLists[] = array( '+0.0' );
$argLists[] = array( '+0.000' );
$argLists[] = array( '+1.0' . str_repeat( ' ', 124 ) );
$argLists[] = array( '4.2' );
$argLists[] = array( ' 4.2' );

return $argLists;
}
Expand All @@ -51,7 +53,6 @@ public function invalidConstructorArgumentsProvider() {

$argLists[] = array( 'foo' );
$argLists[] = array( '' );
$argLists[] = array( '4.2' );
$argLists[] = array( '++4.2' );
$argLists[] = array( '--4.2' );
$argLists[] = array( '-+4.2' );
Expand Down

0 comments on commit 37c7f49

Please sign in to comment.