Skip to content

Commit d31a996

Browse files
authored
Add ofNullable method to handle nullable inputs (#94)
1 parent 8930e4d commit d31a996

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

src/BigNumber.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use function is_float;
2020
use function is_int;
2121
use function is_nan;
22+
use function is_null;
2223
use function ltrim;
2324
use function preg_match;
2425
use function str_contains;
@@ -98,6 +99,26 @@ final public static function of(BigNumber|int|float|string $value): static
9899
return static::from($value);
99100
}
100101

102+
/**
103+
* Creates a BigNumber of the given value, or returns null if the input is null.
104+
*
105+
* Behaves like of() for non-null values.
106+
*
107+
* @see BigNumber::of()
108+
*
109+
* @throws NumberFormatException If the format of the number is not valid.
110+
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
111+
* @throws RoundingNecessaryException If the value cannot be converted to an instance of the subclass without rounding.
112+
*/
113+
public static function ofNullable(BigNumber|int|float|string|null $value): ?static
114+
{
115+
if (is_null($value)) {
116+
return null;
117+
}
118+
119+
return static::of($value);
120+
}
121+
101122
/**
102123
* Returns the minimum of the given values.
103124
*

tests/BigDecimalTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public function testOf(int|float|string $value, string $unscaledValue, int $scal
4141
self::assertBigDecimalInternalValues($unscaledValue, $scale, BigDecimal::of($value));
4242
}
4343

44+
/**
45+
* @param int|float|string $value The value to convert to a BigDecimal.
46+
* @param string $unscaledValue The expected unscaled value.
47+
* @param int $scale The expected scale.
48+
*/
49+
#[DataProvider('providerOf')]
50+
public function testOfNullableWithValidInputBehavesLikeOf(int|float|string $value, string $unscaledValue, int $scale): void
51+
{
52+
self::assertBigDecimalInternalValues($unscaledValue, $scale, BigDecimal::ofNullable($value));
53+
}
54+
55+
public function testOfNullableWithNullInput(): void
56+
{
57+
self::assertNull(BigDecimal::ofNullable(null));
58+
}
59+
4460
public static function providerOf(): array
4561
{
4662
return [

tests/BigIntegerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public function testOf(int|float|string $value, string $expected): void
5050
self::assertBigIntegerEquals($expected, BigInteger::of($value));
5151
}
5252

53+
/**
54+
* @param int|float|string $value The value to convert to a BigInteger.
55+
* @param string $expected The expected string value of the result.
56+
*/
57+
#[DataProvider('providerOf')]
58+
public function testOfNullableWithValidInputBehavesLikeOf(mixed $value, string $expected): void
59+
{
60+
self::assertBigIntegerEquals($expected, BigInteger::ofNullable($value));
61+
}
62+
63+
public function testOfNullableWithNullInput(): void
64+
{
65+
self::assertNull(BigInteger::ofNullable(null));
66+
}
67+
5368
public static function providerOf(): array
5469
{
5570
return [

tests/BigRationalTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ public function testOf(string $numerator, string $denominator, string $string):
7171
self::assertBigRationalInternalValues($numerator, $denominator, $rational);
7272
}
7373

74+
/**
75+
* @param string $numerator The expected numerator.
76+
* @param string $denominator The expected denominator.
77+
* @param string $string The string to parse.
78+
*/
79+
#[DataProvider('providerOf')]
80+
public function testOfNullableWithValidInputBehavesLikeOf(string $numerator, string $denominator, string $string): void
81+
{
82+
$rational = BigRational::ofNullable($string);
83+
self::assertBigRationalInternalValues($numerator, $denominator, $rational);
84+
}
85+
86+
public function testOfNullableWithNullInput(): void
87+
{
88+
self::assertNull(BigRational::ofNullable(null));
89+
}
90+
7491
public static function providerOf(): array
7592
{
7693
return [

0 commit comments

Comments
 (0)