Skip to content

Commit

Permalink
Implement JsonSerializable in AbstractMoney
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 5, 2022
1 parent 3709e60 commit 0d51018
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## UNRELEASED (0.7.0)

💥 **Breaking changes**

- JSON extension is now required for PHP 7.4 (always available with PHP >= 8.0)

**New features**

- `Money` and `RationalMoney` now implement `JsonSerializable`

## [0.6.0](https://github.com/brick/money/releases/tag/0.6.0) - 2022-08-02

💥 **Breaking changes**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"brick/math": "~0.10.1"
},
"require-dev": {
Expand Down
11 changes: 10 additions & 1 deletion src/AbstractMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
use Brick\Math\RoundingMode;
use Brick\Math\Exception\MathException;
use Brick\Math\Exception\RoundingNecessaryException;
use JsonSerializable;

/**
* Base class for Money and RationalMoney.
*/
abstract class AbstractMoney implements MoneyContainer
abstract class AbstractMoney implements MoneyContainer, JsonSerializable
{
abstract public function getAmount() : BigNumber;

Expand Down Expand Up @@ -240,4 +241,12 @@ final protected function getAmountOf($that)

return $that;
}

final public function jsonSerialize(): array
{
return [
'amount' => (string) $this->getAmount(),
'currency' => (string) $this->getCurrency()
];
}
}
17 changes: 17 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,21 @@ public function testTotalOfDifferentCurrenciesThrowsException() : void
Money::of('1.00', 'USD')
);
}

/**
* @dataProvider providerJsonSerialize
*/
public function testJsonSerialize(Money $money, array $expected): void
{
self::assertSame($expected, $money->jsonSerialize());
self::assertSame(json_encode($expected), json_encode($money));
}

public function providerJsonSerialize(): array
{
return [
[Money::of('3.5', 'EUR'), ['amount' => '3.50', 'currency' => 'EUR']],
[Money::of('3.888923', 'GBP', new CustomContext(8)), ['amount' => '3.88892300', 'currency' => 'GBP']]
];
}
}
17 changes: 17 additions & 0 deletions tests/RationalMoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,21 @@ public function providerTo() : array
[['123456789/256', 'CHF'], new AutoContext(), RoundingMode::UNNECESSARY, 'CHF 482253.08203125']
];
}

/**
* @dataProvider providerJsonSerialize
*/
public function testJsonSerialize(RationalMoney $money, array $expected): void
{
self::assertSame($expected, $money->jsonSerialize());
self::assertSame(json_encode($expected), json_encode($money));
}

public function providerJsonSerialize(): array
{
return [
[RationalMoney::of('3.5', 'EUR'), ['amount' => '35/10', 'currency' => 'EUR']],
[RationalMoney::of('3.888923', 'GBP'), ['amount' => '3888923/1000000', 'currency' => 'GBP']]
];
}
}

0 comments on commit 0d51018

Please sign in to comment.