From 213d096edc50b489260d99ef8036eab6e2b7d37a Mon Sep 17 00:00:00 2001 From: PostScription <68855126+PostScipton@users.noreply.github.com> Date: Fri, 30 Apr 2021 14:56:00 +0300 Subject: [PATCH 1/4] Add methods `add()`, `subtract()` and `rebase()` --- src/Exceptions/NotNumericException.php | 24 ++ src/Exceptions/ValueErrorException.php | 4 +- src/Money.php | 67 ++++- src/MoneyInterface.php | 35 +++ src/MoneySettings.php | 4 +- tests/Feature/ManipulatingMoneyNumberTest.php | 278 ++++++++++++++++++ 6 files changed, 406 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/NotNumericException.php create mode 100644 tests/Feature/ManipulatingMoneyNumberTest.php diff --git a/src/Exceptions/NotNumericException.php b/src/Exceptions/NotNumericException.php new file mode 100644 index 0000000..50ba797 --- /dev/null +++ b/src/Exceptions/NotNumericException.php @@ -0,0 +1,24 @@ +settings->getOrigin() === MoneySettings::ORIGIN_INT - ? (float)($this->number / $this->getDivisor()) - : $this->number; + ? (float)($this->getPureNumber() / $this->getDivisor()) + : $this->getPureNumber(); $money = number_format( $amount, @@ -67,6 +69,67 @@ public function getNumber(): string return $money; } + public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $this->number += $this->numberIntoCorrectOrigin($number, $origin); + return $this; + } + + public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $number = $this->numberIntoCorrectOrigin($number, $origin); + + // If less than 0, then result must be 0 + if ($this->getPureNumber() - $number < 0) { + $number = $this->getPureNumber(); + } + + $this->number -= $number; + return $this; + } + + public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $this->number = $this->numberIntoCorrectOrigin($number, $origin); + return $this; + } + + private function numberIntoCorrectOrigin($number, int $origin = MoneySettings::ORIGIN_INT) + { + // If origins are not the same + if ($this->settings->getOrigin() !== $origin) { + return $this->settings->getOrigin() === MoneySettings::ORIGIN_INT + ? floor($number * $this->getDivisor()) // $origin is float + : $number / $this->getDivisor(); // $origin is int + } + + return $number; + } + public function convertOfflineInto(Currency $currency, float $coeff): Money { $new_amount = $this->getPureNumber() * $coeff; diff --git a/src/MoneyInterface.php b/src/MoneyInterface.php index 4f41842..ce75c16 100644 --- a/src/MoneyInterface.php +++ b/src/MoneyInterface.php @@ -118,6 +118,41 @@ public function getNumber(): string; */ public function getPureNumber(): float; + /** + * Adds a number to the money. It's like

+ * `$100 + $50 = $150`

+ * @param $number

+ * A number that will be added

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money; + + /** + * Subtracts a number from the money. It's like

+ * `$150 - $50 = $100`

+ * @param $number

+ * A number that will be subtracted

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money; + + /** + * Rebases the money on a number + * @param $number

+ * A number to which the money will be rebased

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money; + /** * Converts money into another currency using coefficient between currencies *

USD -> RUB = 75.79 / 1

diff --git a/src/MoneySettings.php b/src/MoneySettings.php index 808110f..12a6eec 100644 --- a/src/MoneySettings.php +++ b/src/MoneySettings.php @@ -10,7 +10,7 @@ class MoneySettings implements MoneySettingsInterface { public const ORIGIN_INT = 0; public const ORIGIN_FLOAT = 1; - private const ORIGIN = [ + public const ORIGINS = [ self::ORIGIN_INT, self::ORIGIN_FLOAT, ]; @@ -89,7 +89,7 @@ public function setCurrency(Currency $currency): MoneySettings public function setOrigin(int $origin): MoneySettings { - if (!in_array($origin, self::ORIGIN)) { + if (!in_array($origin, self::ORIGINS)) { throw new UndefinedOriginException(__METHOD__, 1, '$origin'); } diff --git a/tests/Feature/ManipulatingMoneyNumberTest.php b/tests/Feature/ManipulatingMoneyNumberTest.php new file mode 100644 index 0000000..3cbd2a8 --- /dev/null +++ b/tests/Feature/ManipulatingMoneyNumberTest.php @@ -0,0 +1,278 @@ +setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1000, $settings); + + $this->assertEquals('$ 150', $money->add(500)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntAddFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1000, $settings); + + $this->assertEquals('$ 150', $money->add(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatAddFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(100, $settings); + + $this->assertEquals('$ 150', $money->add(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatAddInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(100, $settings); + + $this->assertEquals('$ 150', $money->add(500)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function AddNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->add('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function AddOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->add(500, 1234); + } + + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1500, $settings); + + $this->assertEquals('$ 100', $money->subtract(500)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1500, $settings); + + $this->assertEquals('$ 100', $money->subtract(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(150, $settings); + + $this->assertEquals('$ 100', $money->subtract(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(150, $settings); + + $this->assertEquals('$ 100', $money->subtract(500)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function SubtractNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->subtract('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function SubtractOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->subtract(500, 1234); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractInt_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(500, $settings); + + $this->assertEquals('$ 0', $money->subtract(1000)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractFloat_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(500, $settings); + + $this->assertEquals('$ 0', $money->subtract(100, MoneySettings::ORIGIN_FLOAT)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractFloat_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(50, $settings); + + $this->assertEquals('$ 0', $money->subtract(100, MoneySettings::ORIGIN_FLOAT)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractInt_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(50, $settings); + + $this->assertEquals('$ 0', $money->subtract(1000)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function RebaseInt() + { + $money = new Money(1500); + + $this->assertEquals('$ 100', $money->rebase(1000)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function RebaseFloat() + { + $money = new Money(1500); + + $this->assertEquals('$ 100', $money->rebase(100, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function RebaseNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->rebase('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function RebaseOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->rebase(500, 1234); + } +} \ No newline at end of file From beac9a0675a96eebc1266498f9ad4e69e09a872e Mon Sep 17 00:00:00 2001 From: PostScription <68855126+PostScipton@users.noreply.github.com> Date: Fri, 30 Apr 2021 14:56:00 +0300 Subject: [PATCH 2/4] Add methods add(), subtract() and rebase() --- src/Exceptions/NotNumericException.php | 24 ++ src/Exceptions/ValueErrorException.php | 4 +- src/Money.php | 67 ++++- src/MoneyInterface.php | 35 +++ src/MoneySettings.php | 4 +- tests/Feature/ManipulatingMoneyNumberTest.php | 278 ++++++++++++++++++ 6 files changed, 406 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/NotNumericException.php create mode 100644 tests/Feature/ManipulatingMoneyNumberTest.php diff --git a/src/Exceptions/NotNumericException.php b/src/Exceptions/NotNumericException.php new file mode 100644 index 0000000..50ba797 --- /dev/null +++ b/src/Exceptions/NotNumericException.php @@ -0,0 +1,24 @@ +settings->getOrigin() === MoneySettings::ORIGIN_INT - ? (float)($this->number / $this->getDivisor()) - : $this->number; + ? (float)($this->getPureNumber() / $this->getDivisor()) + : $this->getPureNumber(); $money = number_format( $amount, @@ -67,6 +69,67 @@ public function getNumber(): string return $money; } + public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $this->number += $this->numberIntoCorrectOrigin($number, $origin); + return $this; + } + + public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $number = $this->numberIntoCorrectOrigin($number, $origin); + + // If less than 0, then result must be 0 + if ($this->getPureNumber() - $number < 0) { + $number = $this->getPureNumber(); + } + + $this->number -= $number; + return $this; + } + + public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money + { + // Error handlers + if (!is_numeric($number)) { + throw new NotNumericException(__METHOD__, 1, '$number'); + } + if (!in_array($origin, MoneySettings::ORIGINS)) { + throw new UndefinedOriginException(__METHOD__, 2, '$origin'); + } + + $this->number = $this->numberIntoCorrectOrigin($number, $origin); + return $this; + } + + private function numberIntoCorrectOrigin($number, int $origin = MoneySettings::ORIGIN_INT) + { + // If origins are not the same + if ($this->settings->getOrigin() !== $origin) { + return $this->settings->getOrigin() === MoneySettings::ORIGIN_INT + ? floor($number * $this->getDivisor()) // $origin is float + : $number / $this->getDivisor(); // $origin is int + } + + return $number; + } + public function convertOfflineInto(Currency $currency, float $coeff): Money { $new_amount = $this->getPureNumber() * $coeff; diff --git a/src/MoneyInterface.php b/src/MoneyInterface.php index 4f41842..ce75c16 100644 --- a/src/MoneyInterface.php +++ b/src/MoneyInterface.php @@ -118,6 +118,41 @@ public function getNumber(): string; */ public function getPureNumber(): float; + /** + * Adds a number to the money. It's like

+ * `$100 + $50 = $150`

+ * @param $number

+ * A number that will be added

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money; + + /** + * Subtracts a number from the money. It's like

+ * `$150 - $50 = $100`

+ * @param $number

+ * A number that will be subtracted

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money; + + /** + * Rebases the money on a number + * @param $number

+ * A number to which the money will be rebased

+ * @param int $origin

+ * Origin of the number whether it is integer of float.

+ * Use `Money::ORIGIN_*` to ensure it's correct

+ * @return Money + */ + public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money; + /** * Converts money into another currency using coefficient between currencies *

USD -> RUB = 75.79 / 1

diff --git a/src/MoneySettings.php b/src/MoneySettings.php index 808110f..12a6eec 100644 --- a/src/MoneySettings.php +++ b/src/MoneySettings.php @@ -10,7 +10,7 @@ class MoneySettings implements MoneySettingsInterface { public const ORIGIN_INT = 0; public const ORIGIN_FLOAT = 1; - private const ORIGIN = [ + public const ORIGINS = [ self::ORIGIN_INT, self::ORIGIN_FLOAT, ]; @@ -89,7 +89,7 @@ public function setCurrency(Currency $currency): MoneySettings public function setOrigin(int $origin): MoneySettings { - if (!in_array($origin, self::ORIGIN)) { + if (!in_array($origin, self::ORIGINS)) { throw new UndefinedOriginException(__METHOD__, 1, '$origin'); } diff --git a/tests/Feature/ManipulatingMoneyNumberTest.php b/tests/Feature/ManipulatingMoneyNumberTest.php new file mode 100644 index 0000000..3cbd2a8 --- /dev/null +++ b/tests/Feature/ManipulatingMoneyNumberTest.php @@ -0,0 +1,278 @@ +setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1000, $settings); + + $this->assertEquals('$ 150', $money->add(500)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntAddFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1000, $settings); + + $this->assertEquals('$ 150', $money->add(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatAddFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(100, $settings); + + $this->assertEquals('$ 150', $money->add(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatAddInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(100, $settings); + + $this->assertEquals('$ 150', $money->add(500)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function AddNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->add('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function AddOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->add(500, 1234); + } + + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1500, $settings); + + $this->assertEquals('$ 100', $money->subtract(500)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(1500, $settings); + + $this->assertEquals('$ 100', $money->subtract(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractFloat() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(150, $settings); + + $this->assertEquals('$ 100', $money->subtract(50, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractInt() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(150, $settings); + + $this->assertEquals('$ 100', $money->subtract(500)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function SubtractNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->subtract('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function SubtractOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->subtract(500, 1234); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractInt_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(500, $settings); + + $this->assertEquals('$ 0', $money->subtract(1000)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function IntSubtractFloat_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(500, $settings); + + $this->assertEquals('$ 0', $money->subtract(100, MoneySettings::ORIGIN_FLOAT)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractFloat_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(50, $settings); + + $this->assertEquals('$ 0', $money->subtract(100, MoneySettings::ORIGIN_FLOAT)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function FloatSubtractInt_MoreThanMoneyHas() + { + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_FLOAT); + $money = new Money(50, $settings); + + $this->assertEquals('$ 0', $money->subtract(1000)); + $this->assertEquals(0, $money->getPureNumber()); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function RebaseInt() + { + $money = new Money(1500); + + $this->assertEquals('$ 100', $money->rebase(1000)); + } + + /** @test + * @throws UndefinedOriginException + * @throws NotNumericException + */ + public function RebaseFloat() + { + $money = new Money(1500); + + $this->assertEquals('$ 100', $money->rebase(100, MoneySettings::ORIGIN_FLOAT)); + } + + /** @test + * @throws UndefinedOriginException + */ + public function RebaseNumericError() + { + $this->expectException(NotNumericException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->rebase('asdf'); + } + + /** @test + * @throws NotNumericException + */ + public function RebaseOriginError() + { + $this->expectException(UndefinedOriginException::class); + + $settings = (new MoneySettings()) + ->setOrigin(MoneySettings::ORIGIN_INT); + $money = new Money(100, $settings); + + $money->rebase(500, 1234); + } +} \ No newline at end of file From 5b1890cf14847959486b993edd29a6ed15b09f23 Mon Sep 17 00:00:00 2001 From: PostScription <68855126+PostScipton@users.noreply.github.com> Date: Fri, 30 Apr 2021 16:28:08 +0300 Subject: [PATCH 3/4] Update README.md --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7be637e..79b9176 100644 --- a/README.md +++ b/README.md @@ -433,7 +433,71 @@ $money->getPureNumber(); // 132.76686139139672 --- +##### `add()` + +adds a number to the money + +```php +use PostScripton\Money\Money; + +$money = new Money(1000); // "$ 100" +$money->add(500); // "$ 150" +``` + +```php +use PostScripton\Money\Money; +use PostScripton\Money\MoneySettings; + +$money = new Money(1000); // "$ 100" +$money->add(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 150" +``` + +--- + +##### `subtract()` + +subtracts a number from the money + +```php +use PostScripton\Money\Money; + +$money = new Money(1500); // "$ 150" +$money->subtract(500); // "$ 100" +``` + +```php +use PostScripton\Money\Money; +use PostScripton\Money\MoneySettings; + +$money = new Money(1500); // "$ 150" +$money->subtract(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 100" +``` + +--- + +##### `rebase()` + +a number to which the money will be rebased + +```php +use PostScripton\Money\Money; + +$money = new Money(1500); // "$ 150" +$money->rebase(100); // "$ 10" +``` + +```php +use PostScripton\Money\Money; +use PostScripton\Money\MoneySettings; + +$money = new Money(1500); // "$ 150" +$money->rebase(10.0, MoneySettings::ORIGIN_FLOAT); // "$ 10" +``` + +--- + ##### `convertOfflineInto()` + converts Money object into the chosen currency ```php @@ -477,4 +541,4 @@ use PostScripton\Money\Money; $money = new Money(1234); $money->toString(); // "$ 123.4" -``` +``` \ No newline at end of file From 10a78c8a6efc03114085c2a67b91774945d9ea67 Mon Sep 17 00:00:00 2001 From: Sergey <68855126+PostScripton@users.noreply.github.com> Date: Fri, 30 Apr 2021 16:34:01 +0300 Subject: [PATCH 4/4] v2.1.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f50abb3..9fbc1a2 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "postscripton/laravel-money", "description": "A convenient way to convert numbers from DB or inputs into money strings for humans", - "version": "2.0.0", + "version": "2.1.0", "type": "library", "license": "MIT", "authors": [