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. * @param int $scale The scale of the number.
* *
* @return BigDecimal * @return BigDecimal
*
* @throws \InvalidArgumentException
*/ */
public static function ofUnscaledValue($value, $scale = 0) public static function ofUnscaledValue($value, $scale = 0)
{ {

View File

@ -3,6 +3,7 @@
namespace Brick\Math; namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException; use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException; use Brick\Math\Exception\RoundingNecessaryException;
/** /**
@ -42,32 +43,26 @@ abstract class BigNumber
* *
* @return static * @return static
* *
* @throws \InvalidArgumentException If the number is not valid. * @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 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 * @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. * to the subclass this method has been called on, without rounding.
*/ */
public static function of($value) public static function of($value)
{ {
if ($value instanceof BigNumber) { if ($value instanceof BigNumber) {
try { switch (static::class) {
switch (static::class) { case BigInteger::class:
case BigInteger::class: return $value->toBigInteger();
return $value->toBigInteger();
case BigDecimal::class: case BigDecimal::class:
return $value->toBigDecimal(); return $value->toBigDecimal();
case BigRational::class: case BigRational::class:
return $value->toBigRational(); return $value->toBigRational();
default: default:
return $value; 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.');
} }
} }
@ -87,7 +82,7 @@ abstract class BigNumber
$value = (string) $value; $value = (string) $value;
if (preg_match(BigNumber::REGEXP, $value, $matches) !== 1) { 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'])) { if (isset($matches['denominator'])) {
@ -100,21 +95,15 @@ abstract class BigNumber
$result = new BigRational(new BigInteger($numerator), new BigInteger($denominator), false); $result = new BigRational(new BigInteger($numerator), new BigInteger($denominator), false);
try { switch (static::class) {
switch (static::class) { case BigInteger::class:
case BigInteger::class: return $result->toBigInteger();
return $result->toBigInteger();
case BigDecimal::class: case BigDecimal::class:
return $result->toBigDecimal(); return $result->toBigDecimal();
default: default:
return $result; 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.');
} }
} elseif (isset($matches['fractional']) || isset($matches['exponent'])) { } elseif (isset($matches['fractional']) || isset($matches['exponent'])) {
$fractional = isset($matches['fractional']) ? $matches['fractional'] : ''; $fractional = isset($matches['fractional']) ? $matches['fractional'] : '';
@ -133,21 +122,15 @@ abstract class BigNumber
$result = new BigDecimal($unscaledValue, $scale); $result = new BigDecimal($unscaledValue, $scale);
try { switch (static::class) {
switch (static::class) { case BigInteger::class:
case BigInteger::class: return $result->toBigInteger();
return $result->toBigInteger();
case BigRational::class: case BigRational::class:
return $result->toBigRational(); return $result->toBigRational();
default: default:
return $result; 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.');
} }
} else { } else {
$integral = BigNumber::cleanUp($matches['integral']); $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 * @dataProvider providerOfInvalidValueThrowsException
* @expectedException \InvalidArgumentException * @expectedException \Brick\Math\Exception\NumberFormatException
* *
* @param string $value * @param string $value
*/ */

View File

@ -87,12 +87,12 @@ class BigIntegerTest extends AbstractTestCase
} }
/** /**
* @dataProvider providerOfInvalidValueThrowsException * @dataProvider providerOfInvalidFormatThrowsException
* @expectedException \InvalidArgumentException * @expectedException \Brick\Math\Exception\NumberFormatException
* *
* @param string|number $value * @param string|number $value
*/ */
public function testOfInvalidValueThrowsException($value) public function testOfInvalidFormatThrowsException($value)
{ {
BigInteger::of($value); BigInteger::of($value);
} }
@ -100,11 +100,9 @@ class BigIntegerTest extends AbstractTestCase
/** /**
* @return array * @return array
*/ */
public function providerOfInvalidValueThrowsException() public function providerOfInvalidFormatThrowsException()
{ {
return [ return [
[1.1],
[''], [''],
['a'], ['a'],
[' 1'], [' 1'],
@ -113,7 +111,34 @@ class BigIntegerTest extends AbstractTestCase
['+'], ['+'],
['-'], ['-'],
['+a'], ['+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 * @dataProvider providerOfInvalidString
* @expectedException \InvalidArgumentException * @expectedException \Brick\Math\Exception\NumberFormatException
* *
* @param string $string An invalid string representation. * @param string $string An invalid string representation.
*/ */
public function testParseInvalidString($string) public function testOfInvalidString($string)
{ {
BigRational::of($string); BigRational::of($string);
} }
@ -99,7 +99,7 @@ class BigRationalTest extends AbstractTestCase
/** /**
* @return array * @return array
*/ */
public function providerParseInvalidString() public function providerOfInvalidString()
{ {
return [ return [
['123/-456'], ['123/-456'],