-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds implementation for NegativeBigDecimal. (#20)
- Loading branch information
1 parent
9b145b4
commit 79fa180
Showing
7 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Math\Internal\Exceptions; | ||
|
||
use RuntimeException; | ||
use TinyBlocks\Math\Internal\Number; | ||
|
||
final class NonNegativeValue extends RuntimeException | ||
{ | ||
public function __construct(Number $number) | ||
{ | ||
$template = 'Value <%s> is not valid. Must be a negative number less than zero.'; | ||
parent::__construct(message: sprintf($template, $number->value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Math; | ||
|
||
use TinyBlocks\Math\Internal\BigNumberBehavior; | ||
use TinyBlocks\Math\Internal\Exceptions\NonNegativeValue; | ||
use TinyBlocks\Math\Internal\Number; | ||
use TinyBlocks\Math\Internal\Scale; | ||
|
||
class NegativeBigDecimal extends BigNumberBehavior implements BigNumber | ||
{ | ||
protected function __construct(string|float $value, ?int $scale = null) | ||
{ | ||
$scale = Scale::from(value: $scale); | ||
$number = Number::from(value: $value); | ||
|
||
if ($number->isPositiveOrZero()) { | ||
throw new NonNegativeValue(number: $number); | ||
} | ||
|
||
parent::__construct(number: $number, scale: $scale); | ||
} | ||
|
||
public static function fromFloat(float $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): NegativeBigDecimal | ||
{ | ||
return new NegativeBigDecimal(value: $value, scale: $scale); | ||
} | ||
|
||
public static function fromString(string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): NegativeBigDecimal | ||
{ | ||
return new NegativeBigDecimal(value: $value, scale: $scale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Math; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use TinyBlocks\Math\Internal\Exceptions\NonNegativeValue; | ||
|
||
final class NegativeBigDecimalTest extends TestCase | ||
{ | ||
public function testFromFloat(): void | ||
{ | ||
/** @Given a negative float value */ | ||
$value = -1.0; | ||
|
||
/** @When creating a NegativeBigDecimal from the float */ | ||
$actual = NegativeBigDecimal::fromFloat(value: $value); | ||
|
||
/** @Then the created object should be an instance of both BigNumber and NegativeBigDecimal */ | ||
self::assertInstanceOf(BigNumber::class, $actual); | ||
self::assertInstanceOf(NegativeBigDecimal::class, $actual); | ||
} | ||
|
||
public function testFromString(): void | ||
{ | ||
/** @Given a negative string value */ | ||
$value = '-0.3333333333333333333333'; | ||
|
||
/** @When creating a NegativeBigDecimal from the string */ | ||
$actual = NegativeBigDecimal::fromString(value: $value); | ||
|
||
/** @Then the created object should be an instance of both BigNumber and NegativeBigDecimal */ | ||
self::assertInstanceOf(BigNumber::class, $actual); | ||
self::assertInstanceOf(NegativeBigDecimal::class, $actual); | ||
} | ||
|
||
#[DataProvider('dataProviderForTestNonNegativeValue')] | ||
public function testNonNegativeValue(mixed $value): void | ||
{ | ||
/** @Given a non-negative value */ | ||
$template = 'Value <%s> is not valid. Must be a negative number less than zero.'; | ||
|
||
/** @Then a NonNegativeValue exception should be thrown with the correct message */ | ||
$this->expectException(NonNegativeValue::class); | ||
$this->expectExceptionMessage(sprintf($template, $value)); | ||
|
||
/** @When attempting to create a NegativeBigDecimal with a non-negative value */ | ||
NegativeBigDecimal::fromFloat(value: $value); | ||
} | ||
|
||
public function testNonNegativeValueWithNegate(): void | ||
{ | ||
/** @Given a NegativeBigDecimal value */ | ||
$template = 'Value <%s> is not valid. Must be a negative number less than zero.'; | ||
|
||
/** @Then a NonNegativeValue exception should be thrown when the value is negated */ | ||
$this->expectException(NonNegativeValue::class); | ||
$this->expectExceptionMessage(sprintf($template, 10.155)); | ||
|
||
/** @When negating a negative number, it should trigger an exception */ | ||
$negative = NegativeBigDecimal::fromFloat(value: -10.155); | ||
$negative->negate(); | ||
} | ||
|
||
public static function dataProviderForTestNonNegativeValue(): array | ||
{ | ||
return [ | ||
'Zero value' => ['value' => 0], | ||
'Positive integer' => ['value' => 1] | ||
]; | ||
} | ||
} |