From 1af9312b156517692099d7df083c5948ecf4875f Mon Sep 17 00:00:00 2001 From: Josh Wells Date: Wed, 13 May 2020 14:07:55 +0100 Subject: [PATCH] Implemented JsonSerializable --- src/Decimal.php | 11 ++++++++++- tests/Decimal/DecimalJsonSerialize.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/Decimal/DecimalJsonSerialize.php diff --git a/src/Decimal.php b/src/Decimal.php index 2a374ae..5eb568e 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -3,6 +3,7 @@ namespace Litipk\BigNumbers; +use JsonSerializable; use Litipk\BigNumbers\DecimalConstants as DecimalConstants; use Litipk\BigNumbers\Errors\InfiniteInputError; @@ -14,7 +15,7 @@ * * @author Andreu Correa Casablanca */ -class Decimal +class Decimal implements JsonSerializable { const DEFAULT_SCALE = 16; const CLASSIC_DECIMAL_NUMBER_REGEXP = '/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/'; @@ -1129,6 +1130,14 @@ public function __toString(): string return $this->value; } + /** + * @return string + */ + public function jsonSerialize(): string + { + return $this->value; + } + /* * */ diff --git a/tests/Decimal/DecimalJsonSerialize.php b/tests/Decimal/DecimalJsonSerialize.php new file mode 100644 index 0000000..e7747b3 --- /dev/null +++ b/tests/Decimal/DecimalJsonSerialize.php @@ -0,0 +1,19 @@ +assertEquals($value, Decimal::fromString($value)->jsonSerialize()); + } + + public function testJsonSerializable() + { + $value = '123.456'; + $this->assertEquals(json_encode($value), json_encode(Decimal::fromString($value))); + } +}