1
0
mirror of https://github.com/danog/math.git synced 2024-11-27 04:14:40 +01:00

BigNumber::of() now throws NumberFormatException instead of InvalidArgumentException

This allows to catch all of() exceptions with the parent, ArithmeticException.
This commit is contained in:
Benjamin Morel 2015-06-22 22:13:19 +02:00
parent d6aa1007a9
commit 27bc623b70
6 changed files with 77 additions and 56 deletions

View File

@ -48,6 +48,8 @@ final class BigDecimal extends BigNumber implements \Serializable
* @param int $scale The scale of the number.
*
* @return BigDecimal
*
* @throws \InvalidArgumentException
*/
public static function ofUnscaledValue($value, $scale = 0)
{

View File

@ -3,6 +3,7 @@
namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
/**
@ -42,32 +43,26 @@ abstract class BigNumber
*
* @return static
*
* @throws \InvalidArgumentException If the number is not valid.
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
* @throws NumberFormatException If the format of the number is not valid.
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
* @throws RoundingNecessaryException If the value represents a valid number, but this number cannot be converted
* to the subclass this method has been called on, without rounding.
*/
public static function of($value)
{
if ($value instanceof BigNumber) {
try {
switch (static::class) {
case BigInteger::class:
return $value->toBigInteger();
switch (static::class) {
case BigInteger::class:
return $value->toBigInteger();
case BigDecimal::class:
return $value->toBigDecimal();
case BigDecimal::class:
return $value->toBigDecimal();
case BigRational::class:
return $value->toBigRational();
case BigRational::class:
return $value->toBigRational();
default:
return $value;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
default:
return $value;
}
}
@ -87,7 +82,7 @@ abstract class BigNumber
$value = (string) $value;
if (preg_match(BigNumber::REGEXP, $value, $matches) !== 1) {
throw new \InvalidArgumentException('The given value does not represent a valid number.');
throw new NumberFormatException('The given value does not represent a valid number.');
}
if (isset($matches['denominator'])) {
@ -100,21 +95,15 @@ abstract class BigNumber
$result = new BigRational(new BigInteger($numerator), new BigInteger($denominator), false);
try {
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
case BigDecimal::class:
return $result->toBigDecimal();
case BigDecimal::class:
return $result->toBigDecimal();
default:
return $result;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
default:
return $result;
}
} elseif (isset($matches['fractional']) || isset($matches['exponent'])) {
$fractional = isset($matches['fractional']) ? $matches['fractional'] : '';
@ -133,21 +122,15 @@ abstract class BigNumber
$result = new BigDecimal($unscaledValue, $scale);
try {
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
case BigRational::class:
return $result->toBigRational();
case BigRational::class:
return $result->toBigRational();
default:
return $result;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
default:
return $result;
}
} else {
$integral = BigNumber::cleanUp($matches['integral']);

View File

@ -0,0 +1,11 @@
<?php
namespace Brick\Math\Exception;
/**
* Exception thrown when attempting to create a number from a string with an invalid format.
*/
class NumberFormatException extends ArithmeticException
{
}

View File

@ -163,7 +163,7 @@ class BigDecimalTest extends AbstractTestCase
/**
* @dataProvider providerOfInvalidValueThrowsException
* @expectedException \InvalidArgumentException
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string $value
*/

View File

@ -87,12 +87,12 @@ class BigIntegerTest extends AbstractTestCase
}
/**
* @dataProvider providerOfInvalidValueThrowsException
* @expectedException \InvalidArgumentException
* @dataProvider providerOfInvalidFormatThrowsException
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string|number $value
*/
public function testOfInvalidValueThrowsException($value)
public function testOfInvalidFormatThrowsException($value)
{
BigInteger::of($value);
}
@ -100,11 +100,9 @@ class BigIntegerTest extends AbstractTestCase
/**
* @return array
*/
public function providerOfInvalidValueThrowsException()
public function providerOfInvalidFormatThrowsException()
{
return [
[1.1],
[''],
['a'],
[' 1'],
@ -113,7 +111,34 @@ class BigIntegerTest extends AbstractTestCase
['+'],
['-'],
['+a'],
['-a']
['-a'],
['a0'],
['0a'],
['1.a'],
['a.1'],
];
}
/**
* @dataProvider providerOfNonConvertibleValueThrowsException
* @expectedException \Brick\Math\Exception\RoundingNecessaryException
*
* @param float|string $value
*/
public function testOfNonConvertibleValueThrowsException($value)
{
BigInteger::of($value);
}
/**
* @return array
*/
public function providerOfNonConvertibleValueThrowsException()
{
return [
[1.1],
['1e-1'],
['7/9'],
];
}

View File

@ -86,12 +86,12 @@ class BigRationalTest extends AbstractTestCase
}
/**
* @dataProvider providerParseInvalidString
* @expectedException \InvalidArgumentException
* @dataProvider providerOfInvalidString
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string $string An invalid string representation.
*/
public function testParseInvalidString($string)
public function testOfInvalidString($string)
{
BigRational::of($string);
}
@ -99,7 +99,7 @@ class BigRationalTest extends AbstractTestCase
/**
* @return array
*/
public function providerParseInvalidString()
public function providerOfInvalidString()
{
return [
['123/-456'],