From 9eb7335ea4719f957159ea29dab00a3ec4ac3158 Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 12 Mar 2020 21:33:58 -0400 Subject: [PATCH 1/5] added interface JsonSerializable to Util\Unit class --- Cmfcmf/OpenWeatherMap/Util/Unit.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Cmfcmf/OpenWeatherMap/Util/Unit.php b/Cmfcmf/OpenWeatherMap/Util/Unit.php index 9ad7a76..cd0653a 100644 --- a/Cmfcmf/OpenWeatherMap/Util/Unit.php +++ b/Cmfcmf/OpenWeatherMap/Util/Unit.php @@ -18,10 +18,12 @@ namespace Cmfcmf\OpenWeatherMap\Util; +use JsonSerializable; + /** * The unit class representing a unit object. */ -class Unit +class Unit implements JsonSerializable { /** * @var float The value. @@ -144,4 +146,19 @@ public function getFormatted() return (string)$this->getValue(); } } + + /** + * Get Unit properties when encoding to JSON + * @return array + */ + public function jsonSerialize () + { + return [ + 'value' => $this->getValue(), + 'unit' => $this->getUnit(), + 'description' => $this->getDescription(), + 'precision' => $this->getPrecision(), + ]; + } + } From 18994045585fcd9dda347d4d1f995b4c88318a8e Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 12 Mar 2020 21:37:18 -0400 Subject: [PATCH 2/5] updated docblock of jsonSerialize() --- Cmfcmf/OpenWeatherMap/Util/Unit.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cmfcmf/OpenWeatherMap/Util/Unit.php b/Cmfcmf/OpenWeatherMap/Util/Unit.php index cd0653a..e589f47 100644 --- a/Cmfcmf/OpenWeatherMap/Util/Unit.php +++ b/Cmfcmf/OpenWeatherMap/Util/Unit.php @@ -149,9 +149,10 @@ public function getFormatted() /** * Get Unit properties when encoding to JSON + * * @return array */ - public function jsonSerialize () + public function jsonSerialize() { return [ 'value' => $this->getValue(), From e94c0b4cef29c0d6f46600fda00bba14b115dd29 Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 12 Mar 2020 22:25:04 -0400 Subject: [PATCH 3/5] coding style fix --- Cmfcmf/OpenWeatherMap/Util/Unit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cmfcmf/OpenWeatherMap/Util/Unit.php b/Cmfcmf/OpenWeatherMap/Util/Unit.php index e589f47..a894dbf 100644 --- a/Cmfcmf/OpenWeatherMap/Util/Unit.php +++ b/Cmfcmf/OpenWeatherMap/Util/Unit.php @@ -158,7 +158,7 @@ public function jsonSerialize() 'value' => $this->getValue(), 'unit' => $this->getUnit(), 'description' => $this->getDescription(), - 'precision' => $this->getPrecision(), + 'precision' => $this->getPrecision() ]; } From d41b42a204adb2f3af5d2ee111eb7900cbb1322c Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 12 Mar 2020 22:25:04 -0400 Subject: [PATCH 4/5] coding style fix --- Cmfcmf/OpenWeatherMap/Util/Unit.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Cmfcmf/OpenWeatherMap/Util/Unit.php b/Cmfcmf/OpenWeatherMap/Util/Unit.php index e589f47..577db62 100644 --- a/Cmfcmf/OpenWeatherMap/Util/Unit.php +++ b/Cmfcmf/OpenWeatherMap/Util/Unit.php @@ -158,8 +158,7 @@ public function jsonSerialize() 'value' => $this->getValue(), 'unit' => $this->getUnit(), 'description' => $this->getDescription(), - 'precision' => $this->getPrecision(), + 'precision' => $this->getPrecision() ]; } - } From f9e2bae0e28eeef5ffa850be727051fd5e8c89f8 Mon Sep 17 00:00:00 2001 From: Christian Flach Date: Fri, 13 Mar 2020 16:48:27 +0000 Subject: [PATCH 5/5] Add test --- tests/Util/UnitTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Util/UnitTest.php b/tests/Util/UnitTest.php index 358ffdd..c03d09f 100644 --- a/tests/Util/UnitTest.php +++ b/tests/Util/UnitTest.php @@ -167,4 +167,10 @@ public function testToString() $this->assertEquals($this->unit->getFormatted(), $this->unit); } + + public function testToJSON() + { + $unit = new Unit(42.5, "°C", "hot", "2.5"); + $this->assertEquals(json_encode($unit), '{"value":42.5,"unit":"\u00b0C","description":"hot","precision":2.5}'); + } }