From 3f436c8100487f79484f083f3bafe062071d7083 Mon Sep 17 00:00:00 2001 From: Tim Eulitz Date: Thu, 2 May 2019 16:34:05 +0200 Subject: [PATCH 1/3] Add string casting functionality to Euro class --- src/Euro.php | 7 +++++++ tests/Unit/EuroTest.php | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/Euro.php b/src/Euro.php index 1d1cc66..3a528f9 100644 --- a/src/Euro.php +++ b/src/Euro.php @@ -29,6 +29,13 @@ private function __construct( int $cents ) { $this->cents = $cents; } + /** + * @return string + */ + public function __toString(): string { + return $this->getEuroString(); + } + /** * @param int $cents * @return self diff --git a/tests/Unit/EuroTest.php b/tests/Unit/EuroTest.php index a11cae5..b63e14c 100644 --- a/tests/Unit/EuroTest.php +++ b/tests/Unit/EuroTest.php @@ -91,6 +91,11 @@ public function testGiven1234Cents_getEuroStringReturns12euro34() { $this->assertSame( '12.34', $amount->getEuroString() ); } + public function testGiven1234Cents_stringCastingReturns98euro76() { + $amount = Euro::newFromCents( 9876 ); + $this->assertSame( '98.76', (string) $amount ); + } + public function testOneEuroString_getsTurnedInto100cents() { $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); } From 75535485390e7e9df7bc495594b63502a347308f Mon Sep 17 00:00:00 2001 From: Tim Eulitz Date: Thu, 2 May 2019 17:35:13 +0200 Subject: [PATCH 2/3] Make Euro class JSON serializable --- composer.json | 3 ++- src/Euro.php | 9 ++++++++- tests/Unit/EuroTest.php | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4b41b40..bc1b737 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "homepage": "https://github.com/wmde/Euro", "license": "GPL-2.0-or-later", "require": { - "php": ">=7.1" + "php": ">=7.1", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "~6.2", diff --git a/src/Euro.php b/src/Euro.php index 3a528f9..aef547f 100644 --- a/src/Euro.php +++ b/src/Euro.php @@ -10,7 +10,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -final class Euro { +final class Euro implements \JsonSerializable { private const DECIMAL_COUNT = 2; private const CENTS_PER_EURO = 100; @@ -36,6 +36,13 @@ public function __toString(): string { return $this->getEuroString(); } + /** + * @return string + */ + public function jsonSerialize(): string { + return $this->getEuroString(); + } + /** * @param int $cents * @return self diff --git a/tests/Unit/EuroTest.php b/tests/Unit/EuroTest.php index b63e14c..9557724 100644 --- a/tests/Unit/EuroTest.php +++ b/tests/Unit/EuroTest.php @@ -96,6 +96,11 @@ public function testGiven1234Cents_stringCastingReturns98euro76() { $this->assertSame( '98.76', (string) $amount ); } + public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { + $amount = Euro::newFromCents( 9876 ); + $this->assertSame( '"98.76"', json_encode( $amount ) ); + } + public function testOneEuroString_getsTurnedInto100cents() { $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); } From 3f0a21ea8a4c5c920d74f4cb5ee5de002eb6da64 Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Fri, 3 May 2019 05:20:48 +0200 Subject: [PATCH 3/3] Fix test name typo --- tests/Unit/EuroTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/EuroTest.php b/tests/Unit/EuroTest.php index 9557724..a0c8bfa 100644 --- a/tests/Unit/EuroTest.php +++ b/tests/Unit/EuroTest.php @@ -91,7 +91,7 @@ public function testGiven1234Cents_getEuroStringReturns12euro34() { $this->assertSame( '12.34', $amount->getEuroString() ); } - public function testGiven1234Cents_stringCastingReturns98euro76() { + public function testGiven9876Cents_stringCastingReturns98euro76() { $amount = Euro::newFromCents( 9876 ); $this->assertSame( '98.76', (string) $amount ); }