Skip to content

Commit 9b145b4

Browse files
authored
Release/3.1.0 (#19)
1 parent 800c0b8 commit 9b145b4

File tree

8 files changed

+119
-90
lines changed

8 files changed

+119
-90
lines changed

src/BigNumber.php

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
use TinyBlocks\Math\Internal\Exceptions\DivisionByZero;
88

9+
/**
10+
* The BigNumber interface represents arbitrary-precision arithmetic, allowing
11+
* precise mathematical operations on large or small numbers.
12+
*/
913
interface BigNumber
1014
{
1115
public const AUTOMATIC_SCALE = null;
@@ -18,22 +22,6 @@ interface BigNumber
1822
*/
1923
public function add(BigNumber $addend): BigNumber;
2024

21-
/**
22-
* Subtracts another BigNumber (subtrahend) from the current BigNumber (minuend).
23-
*
24-
* @param BigNumber $subtrahend The BigNumber to be subtracted from the current BigNumber.
25-
* @return BigNumber A new BigNumber representing the difference between the two numbers.
26-
*/
27-
public function subtract(BigNumber $subtrahend): BigNumber;
28-
29-
/**
30-
* Multiplies the current BigNumber (multiplicand) with another BigNumber (multiplier).
31-
*
32-
* @param BigNumber $multiplier The BigNumber to multiply the current BigNumber by.
33-
* @return BigNumber A new BigNumber representing the product of the two numbers.
34-
*/
35-
public function multiply(BigNumber $multiplier): BigNumber;
36-
3725
/**
3826
* Divides the current BigNumber (dividend) by another BigNumber (divisor).
3927
*
@@ -44,62 +32,64 @@ public function multiply(BigNumber $multiplier): BigNumber;
4432
public function divide(BigNumber $divisor): BigNumber;
4533

4634
/**
47-
* Returns a new BigNumber after applying the specified rounding mode.
35+
* Retrieves the scale of the current BigNumber.
4836
*
49-
* @param RoundingMode $mode The rounding mode to apply.
50-
* @return BigNumber A new BigNumber rounded according to the specified mode.
37+
* @return int|null The scale of the current BigNumber, or null if no scale is applied.
5138
*/
52-
public function withRounding(RoundingMode $mode): BigNumber;
39+
public function getScale(): ?int;
5340

5441
/**
55-
* Returns a new BigNumber with the specified scale.
42+
* Compares the current BigNumber with another BigNumber to determine if it is greater.
5643
*
57-
* @param int $scale The scale to apply to the BigNumber.
58-
* @return BigNumber A new BigNumber with the specified scale.
44+
* @param BigNumber $other The BigNumber to compare with.
45+
* @return bool True if the current BigNumber is greater than the other BigNumber, otherwise false.
5946
*/
60-
public function withScale(int $scale): BigNumber;
47+
public function isGreaterThan(BigNumber $other): bool;
6148

6249
/**
63-
* Returns a new BigNumber with the negated value of the current BigNumber.
50+
* Compares the current BigNumber with another BigNumber to determine if it is greater than or equal.
6451
*
65-
* @return BigNumber A new BigNumber representing the negated value of the current number.
52+
* @param BigNumber $other The BigNumber to compare with.
53+
* @return bool True if the current BigNumber is greater than or equal to the other BigNumber, otherwise false.
6654
*/
67-
public function negate(): BigNumber;
55+
public function isGreaterThanOrEqual(BigNumber $other): bool;
6856

6957
/**
70-
* Retrieves the scale of the current BigNumber.
58+
* Determines if the current BigNumber is negative.
7159
*
72-
* @return int|null The scale of the current BigNumber, or null if no scale is applied.
60+
* @return bool True if the BigNumber is negative, otherwise false.
7361
*/
74-
public function getScale(): ?int;
62+
public function isNegative(): bool;
7563

7664
/**
77-
* Determines if the current BigNumber is equal to zero.
65+
* Determines if the current BigNumber is either negative or zero.
7866
*
79-
* @return bool True if the BigNumber is zero, otherwise false.
67+
* @return bool True if the BigNumber is negative or zero, otherwise false.
8068
*/
81-
public function isZero(): bool;
69+
public function isNegativeOrZero(): bool;
8270

8371
/**
84-
* Determines if the current BigNumber is negative.
72+
* Compares the current BigNumber with another BigNumber to determine if it is less.
8573
*
86-
* @return bool True if the BigNumber is negative, otherwise false.
74+
* @param BigNumber $other The BigNumber to compare with.
75+
* @return bool True if the current BigNumber is less than the other BigNumber, otherwise false.
8776
*/
88-
public function isNegative(): bool;
77+
public function isLessThan(BigNumber $other): bool;
8978

9079
/**
91-
* Determines if the current BigNumber is positive.
80+
* Compares the current BigNumber with another BigNumber to determine if it is less than or equal.
9281
*
93-
* @return bool True if the BigNumber is positive, otherwise false.
82+
* @param BigNumber $other The BigNumber to compare with.
83+
* @return bool True if the current BigNumber is less than or equal to the other BigNumber, otherwise false.
9484
*/
95-
public function isPositive(): bool;
85+
public function isLessThanOrEqual(BigNumber $other): bool;
9686

9787
/**
98-
* Determines if the current BigNumber is either negative or zero.
88+
* Determines if the current BigNumber is positive.
9989
*
100-
* @return bool True if the BigNumber is negative or zero, otherwise false.
90+
* @return bool True if the BigNumber is positive, otherwise false.
10191
*/
102-
public function isNegativeOrZero(): bool;
92+
public function isPositive(): bool;
10393

10494
/**
10595
* Determines if the current BigNumber is either positive or zero.
@@ -109,36 +99,34 @@ public function isNegativeOrZero(): bool;
10999
public function isPositiveOrZero(): bool;
110100

111101
/**
112-
* Compares the current BigNumber with another BigNumber to determine if it is less.
102+
* Determines if the current BigNumber is equal to zero.
113103
*
114-
* @param BigNumber $other The BigNumber to compare with.
115-
* @return bool True if the current BigNumber is less than the other BigNumber, otherwise false.
104+
* @return bool True if the BigNumber is zero, otherwise false.
116105
*/
117-
public function isLessThan(BigNumber $other): bool;
106+
public function isZero(): bool;
118107

119108
/**
120-
* Compares the current BigNumber with another BigNumber to determine if it is greater.
109+
* Multiplies the current BigNumber (multiplicand) with another BigNumber (multiplier).
121110
*
122-
* @param BigNumber $other The BigNumber to compare with.
123-
* @return bool True if the current BigNumber is greater than the other BigNumber, otherwise false.
111+
* @param BigNumber $multiplier The BigNumber to multiply the current BigNumber by.
112+
* @return BigNumber A new BigNumber representing the product of the two numbers.
124113
*/
125-
public function isGreaterThan(BigNumber $other): bool;
114+
public function multiply(BigNumber $multiplier): BigNumber;
126115

127116
/**
128-
* Compares the current BigNumber with another BigNumber to determine if it is less than or equal.
117+
* Returns a new BigNumber with the negated value of the current BigNumber.
129118
*
130-
* @param BigNumber $other The BigNumber to compare with.
131-
* @return bool True if the current BigNumber is less than or equal to the other BigNumber, otherwise false.
119+
* @return BigNumber A new BigNumber representing the negated value of the current number.
132120
*/
133-
public function isLessThanOrEqual(BigNumber $other): bool;
121+
public function negate(): BigNumber;
134122

135123
/**
136-
* Compares the current BigNumber with another BigNumber to determine if it is greater than or equal.
124+
* Subtracts another BigNumber (subtrahend) from the current BigNumber (minuend).
137125
*
138-
* @param BigNumber $other The BigNumber to compare with.
139-
* @return bool True if the current BigNumber is greater than or equal to the other BigNumber, otherwise false.
126+
* @param BigNumber $subtrahend The BigNumber to be subtracted from the current BigNumber.
127+
* @return BigNumber A new BigNumber representing the difference between the two numbers.
140128
*/
141-
public function isGreaterThanOrEqual(BigNumber $other): bool;
129+
public function subtract(BigNumber $subtrahend): BigNumber;
142130

143131
/**
144132
* Converts the current BigNumber to a floating-point number.
@@ -154,4 +142,20 @@ public function toFloat(): float;
154142
* @return string The string representation of the BigNumber.
155143
*/
156144
public function toString(): string;
145+
146+
/**
147+
* Returns a new BigNumber after applying the specified rounding mode.
148+
*
149+
* @param RoundingMode $mode The rounding mode to apply.
150+
* @return BigNumber A new BigNumber rounded according to the specified mode.
151+
*/
152+
public function withRounding(RoundingMode $mode): BigNumber;
153+
154+
/**
155+
* Returns a new BigNumber with the specified scale.
156+
*
157+
* @param int $scale The scale to apply to the BigNumber.
158+
* @return BigNumber A new BigNumber with the specified scale.
159+
*/
160+
public function withScale(int $scale): BigNumber;
157161
}

src/Internal/Exceptions/InvalidNumber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
final class InvalidNumber extends RuntimeException
1010
{
11-
public function __construct(mixed $value)
11+
public function __construct(string $value)
1212
{
1313
$template = 'The value <%s> is not a valid number.';
1414
parent::__construct(message: sprintf($template, $value));

src/Internal/Exceptions/NonPositiveNumber.php

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Math\Internal\Exceptions;
6+
7+
use RuntimeException;
8+
use TinyBlocks\Math\Internal\Number;
9+
10+
final class NonPositiveValue extends RuntimeException
11+
{
12+
public function __construct(Number $number)
13+
{
14+
$template = 'Value <%s> is not valid. Must be a positive number greater than zero.';
15+
parent::__construct(message: sprintf($template, $number->value));
16+
}
17+
}

src/Internal/Number.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use TinyBlocks\Math\Internal\Exceptions\InvalidNumber;
88

9-
class Number
9+
final class Number
1010
{
1111
private const ZERO = 0.0;
1212
private const SIGN = '?<sign>[\-\+]';

src/PositiveBigDecimal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace TinyBlocks\Math;
66

77
use TinyBlocks\Math\Internal\BigNumberBehavior;
8-
use TinyBlocks\Math\Internal\Exceptions\NonPositiveNumber;
8+
use TinyBlocks\Math\Internal\Exceptions\NonPositiveValue;
99
use TinyBlocks\Math\Internal\Number;
1010
use TinyBlocks\Math\Internal\Scale;
1111

@@ -17,7 +17,7 @@ protected function __construct(string|float $value, ?int $scale = null)
1717
$number = Number::from(value: $value);
1818

1919
if ($number->isNegativeOrZero()) {
20-
throw new NonPositiveNumber(number: $number);
20+
throw new NonPositiveValue(number: $number);
2121
}
2222

2323
parent::__construct(number: $number, scale: $scale);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Math\Models;
6+
7+
use TinyBlocks\Math\PositiveBigDecimal;
8+
9+
final class CustomPositiveBigDecimal extends PositiveBigDecimal
10+
{
11+
}

tests/PositiveBigDecimalTest.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\TestCase;
9-
use TinyBlocks\Math\Internal\Exceptions\NonPositiveNumber;
9+
use TinyBlocks\Math\Internal\Exceptions\NonPositiveValue;
10+
use TinyBlocks\Math\Models\CustomPositiveBigDecimal;
1011

1112
final class PositiveBigDecimalTest extends TestCase
1213
{
@@ -36,35 +37,48 @@ public function testFromString(): void
3637
self::assertInstanceOf(PositiveBigDecimal::class, $actual);
3738
}
3839

39-
#[DataProvider('dataProviderForTestNonPositiveNumber')]
40-
public function testNonPositiveNumber(mixed $value): void
40+
#[DataProvider('dataProviderForTestNonPositiveValue')]
41+
public function testNonPositiveValue(mixed $value): void
4142
{
4243
/** @Given a non-positive value */
43-
$template = 'The <%.2f> value must be positive.';
44+
$template = 'Value <%s> is not valid. Must be a positive number greater than zero.';
4445

45-
/** @Then a NonPositiveNumber exception should be thrown with the correct message */
46-
$this->expectException(NonPositiveNumber::class);
46+
/** @Then a NonPositiveValue exception should be thrown with the correct message */
47+
$this->expectException(NonPositiveValue::class);
4748
$this->expectExceptionMessage(sprintf($template, $value));
4849

4950
/** @When attempting to create a PositiveBigDecimal with a non-positive value */
5051
PositiveBigDecimal::fromFloat(value: $value);
5152
}
5253

53-
public function testNonPositiveNumberWithNegate(): void
54+
public function testNonPositiveValueWithNegate(): void
5455
{
5556
/** @Given a PositiveBigDecimal value */
56-
$template = 'The <%.2f> value must be positive.';
57+
$template = 'Value <%s> is not valid. Must be a positive number greater than zero.';
5758

58-
/** @Then a NonPositiveNumber exception should be thrown when the value is negated */
59-
$this->expectException(NonPositiveNumber::class);
59+
/** @Then a NonPositiveValue exception should be thrown when the value is negated */
60+
$this->expectException(NonPositiveValue::class);
6061
$this->expectExceptionMessage(sprintf($template, -1.00));
6162

6263
/** @When negating a positive number, it should trigger an exception */
6364
$positive = PositiveBigDecimal::fromFloat(value: 10.155);
6465
$positive->negate();
6566
}
6667

67-
public static function dataProviderForTestNonPositiveNumber(): array
68+
public function testNonPositiveValueWithCustomClass(): void
69+
{
70+
/** @Given a non-positive value */
71+
$template = 'Value <%s> is not valid. Must be a positive number greater than zero.';
72+
73+
/** @Then a NonPositiveValue exception should be thrown with the correct message */
74+
$this->expectException(NonPositiveValue::class);
75+
$this->expectExceptionMessage(sprintf($template, -1.00));
76+
77+
/** @When attempting to create a CustomPositiveBigDecimal with a non-positive value */
78+
CustomPositiveBigDecimal::fromFloat(value: -1.00);
79+
}
80+
81+
public static function dataProviderForTestNonPositiveValue(): array
6882
{
6983
return [
7084
'Zero value' => ['value' => 0],

0 commit comments

Comments
 (0)