From 812b41917d22e133f76372bd2a409e93fa2e04a0 Mon Sep 17 00:00:00 2001 From: Vladimir Berdnik Date: Mon, 4 Mar 2019 15:33:35 +0600 Subject: [PATCH] Improve pretty-print formatting of negative amount of money (#4) * Improve pretty-print formatting of negative amount of money --- .php_cs | 1 + CHANGELOG.md | 5 +++++ src/Money.php | 6 +++++- tests/Exchange/RateTest.php | 11 ++++++++--- tests/MoneyTest.php | 7 +++++++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.php_cs b/.php_cs index 06f962b..32691a8 100644 --- a/.php_cs +++ b/.php_cs @@ -72,6 +72,7 @@ $config = PhpCsFixer\Config::create() 'standardize_not_equals' => true, 'strict_comparison' => true, 'strict_param' => true, + 'ternary_operator_spaces' => true, 'ternary_to_null_coalescing' => true, 'trailing_comma_in_multiline_array' => true, 'trim_array_spaces' => true, diff --git a/CHANGELOG.md b/CHANGELOG.md index e2554bd..6525dca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,11 @@ Security - in case of vulnerabilities. _TBD_ +## 1.0.1 (2019-03-05) + +### Fixed ++ Fixed pretty-print formatting of negative amount of money + ## 1.0.0 (2018-12-08) Initial Release diff --git a/src/Money.php b/src/Money.php index e331af6..3b76c22 100644 --- a/src/Money.php +++ b/src/Money.php @@ -165,7 +165,11 @@ public function getFormattedAmount(string $decimalPoint = '.', string $thousands */ public function getPrettyPrint(string $decimalPoint = '.', string $thousandsSeparator = ','): string { - return $this->getCurrency()->getSign() . $this->getFormattedAmount($decimalPoint, $thousandsSeparator); + $currencySign = $this->getCurrency()->getSign(); + $formattedAmount = ltrim($this->getFormattedAmount($decimalPoint, $thousandsSeparator), '-'); + $amountSign = $this->isNegative() ? '-' : ''; + + return "{$amountSign}{$currencySign}{$formattedAmount}"; } /** diff --git a/tests/Exchange/RateTest.php b/tests/Exchange/RateTest.php index 2217346..ef24c91 100644 --- a/tests/Exchange/RateTest.php +++ b/tests/Exchange/RateTest.php @@ -15,13 +15,13 @@ public function testExceptionIsRaisedForInvalidConstructorArgument(): void { $cp = new CurrencyPair(new Currency('USD'), new Currency('EUR')); $this->expectException(InvalidArgumentException::class); - new Rate($cp, new DateTime(), 0); + new Rate($cp, $this->getRateDate(), 0); } public function testCanBeConstructed() { $cp = new CurrencyPair(new Currency('USD'), new Currency('EUR')); - $rate = new Rate($cp, new DateTime(), 0.92); + $rate = new Rate($cp, $this->getRateDate(), 0.92); $this->assertInstanceOf(Rate::class, $rate); @@ -36,7 +36,7 @@ public function testCanBeConstructed() public function testCanBeSerialized(Rate $rate): void { $this->assertSame( - '{"baseCurrency":"USD","quoteCurrency":"EUR","date":"' . date('c') . '","ratio":0.92}', + '{"baseCurrency":"USD","quoteCurrency":"EUR","date":"' . $this->getRateDate()->format('c') . '","ratio":0.92}', json_encode($rate) ); } @@ -112,4 +112,9 @@ public function testCanConvertMoneyWithMarkup(Rate $rate): void $this->assertSame(966, $newMoney->getAmount()); $this->assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode()); } + + private function getRateDate(): DateTime + { + return new DateTime('2019-03-05 13:32'); + } } diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index 67210fd..b048cc7 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -113,6 +113,13 @@ public function testFormattedAmountCanBeRetrieved(): void $this->assertSame('€1,200.34', $m->getPrettyPrint('.', ',')); } + public function testFormattedNegativeAmountCanBeRetrieved(): void + { + $m = new Money(-120034, new Currency('EUR')); + $this->assertSame('-1 200.34', $m->getFormattedAmount('.', ' ')); + $this->assertSame('-€1,200.34', $m->getPrettyPrint('.', ',')); + } + /** * @depends testObjectCanBeConstructedFromIntegerValueAndCurrencyObject *