Skip to content

Commit

Permalink
Allows Number and NumberImmutable to take instances of Number and Num…
Browse files Browse the repository at this point in the history
…berImmutable
  • Loading branch information
elliotjreed committed Dec 3, 2023
1 parent 756bdaf commit b62c9d7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ $number->asString(); // '25'
```php
use ElliotJReed\Maths\Number;

$number = new Number(10);
$anotherNumber = new Number($number);

$number->asFloat(); // 10.0
$anotherNumber->asFloat(); // 10.0

$number->add(10)->asFloat(); // 20.0
$anotherNumber->add(5)->asFloat(); // 15.0
```

```php
use ElliotJReed\Maths\Number;

$number = new Number(1.125);

$number->roundToDecimalPlaces(2);
Expand Down
10 changes: 6 additions & 4 deletions src/ElliotJReed/Maths/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ abstract class NumberFormat
protected string $number;

/**
* @param int|float|string $number (Optional) The "base" number. Default: 0
* @param int $precision (Optional) The number of digits after the decimal place in the result. Default: 24
* @param \ElliotJReed\Maths\Number|\ElliotJReed\Maths\NumberImmutable|int|float|string $number (Optional) The "base" number. Default: 0
* @param int $precision (Optional) The number of digits after the decimal place in the result. Default: 24
*
* @throws \ElliotJReed\Maths\Exception\NonNumericValue thrown when number argument is not numeric
*/
public function __construct(int | float | string $number = 0, protected readonly int $precision = 24)
{
public function __construct(
NumberImmutable | Number | int | float | string $number = 0,
protected readonly int $precision = 24
) {
$this->number = $this->castNumberToString($number);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/ElliotJReed/Maths/NumberImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ElliotJReed\Maths\Exception\InvalidExponent;
use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor;
use ElliotJReed\Maths\Exception\NonNumericValue;
use ElliotJReed\Maths\Number;
use ElliotJReed\Maths\NumberImmutable;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -42,6 +43,24 @@ public function testItReturnsNumberWhenNegativeBaseNumberIsInScientificNotation(
$this->assertSame(0, $number->asInteger(\PHP_ROUND_HALF_DOWN));
}

public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumberImmutable(): void
{
$number = new NumberImmutable(new NumberImmutable(10000.29533));

$this->assertSame('10000.30', $number->asString(2));
$this->assertSame('10000.29533', $number->asString());
$this->assertSame(10000.29533, $number->asFloat());
}

public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumber(): void
{
$number = new NumberImmutable(new Number(10000.29533));

$this->assertSame('10000.30', $number->asString(2));
$this->assertSame('10000.29533', $number->asString());
$this->assertSame(10000.29533, $number->asFloat());
}

public function testItReturnsNumberAsStringToDefinedDecimalPlaces(): void
{
$number = new NumberImmutable(10000.29533);
Expand Down
19 changes: 19 additions & 0 deletions tests/ElliotJReed/Maths/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor;
use ElliotJReed\Maths\Exception\NonNumericValue;
use ElliotJReed\Maths\Number;
use ElliotJReed\Maths\NumberImmutable;
use PHPUnit\Framework\TestCase;

final class NumberTest extends TestCase
Expand Down Expand Up @@ -42,6 +43,24 @@ public function testItReturnsNumberWhenNegativeBaseNumberIsInScientificNotation(
$this->assertSame(0, $number->asInteger(\PHP_ROUND_HALF_DOWN));
}

public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumber(): void
{
$number = new Number(new Number(10000.29533));

$this->assertSame('10000.30', $number->asString(2));
$this->assertSame('10000.29533', $number->asString());
$this->assertSame(10000.29533, $number->asFloat());
}

public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumberImmutable(): void
{
$number = new Number(new NumberImmutable(10000.29533));

$this->assertSame('10000.30', $number->asString(2));
$this->assertSame('10000.29533', $number->asString());
$this->assertSame(10000.29533, $number->asFloat());
}

public function testItReturnsNumberAsStringToDefinedDecimalPlaces(): void
{
$number = new Number(10000.29533);
Expand Down

0 comments on commit b62c9d7

Please sign in to comment.