From 3b44fd38a894adc9b3845ac56ff0ab05a518f497 Mon Sep 17 00:00:00 2001 From: Alexis POUPELIN Date: Tue, 14 Nov 2023 16:44:49 +0100 Subject: [PATCH 1/3] Added intval and ceil to not loose precision during conversion --- src/Model/ShippingMethod.php | 7 +++++-- test/ClientTest.php | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Model/ShippingMethod.php b/src/Model/ShippingMethod.php index 8bb3ba7..9425cc7 100644 --- a/src/Model/ShippingMethod.php +++ b/src/Model/ShippingMethod.php @@ -11,11 +11,14 @@ public static function fromData(array $data): self $prices[$country['iso_2']] = (int)($country['price'] * 100); } + $min_weight = intval(ceil($data['min_weight'] * 1000)); + $max_weight = intval(ceil($data['max_weight'] * 1000)); + return new self( (int)$data['id'], (string)$data['name'], - (int)($data['min_weight'] * 1000), - (int)($data['max_weight'] * 1000), + (int) $min_weight, + (int) $max_weight, (string)$data['carrier'], $prices, $data['service_point_input'] !== 'none', diff --git a/test/ClientTest.php b/test/ClientTest.php index e24a1eb..23954aa 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -69,7 +69,7 @@ public function testGetShippingMethods(): void return new Response( 200, [], - '{"shipping_methods": [{"service_point_input": "none","max_weight": "1.000","name": "Low weight shipment","carrier": "carrier_code","countries": [{"iso_2": "BE","iso_3": "BEL","id": 1,"price": 3.50,"name": "Belgium"},{"iso_2": "NL","iso_3": "NLD","id": 2,"price": 4.20,"name": "Netherlands"}],"min_weight": "0.001","id": 1,"price": 0}]}' + '{"shipping_methods": [{"service_point_input": "none","min_weight": "0.001","max_weight": "1.001","name": "Low weight shipment","carrier": "carrier_code","countries": [{"iso_2": "BE","iso_3": "BEL","id": 1,"price": 3.50,"name": "Belgium"},{"iso_2": "NL","iso_3": "NLD","id": 2,"price": 4.20,"name": "Netherlands"}],"min_weight": "0.001","id": 1,"price": 0}]}' ); }); @@ -78,7 +78,7 @@ public function testGetShippingMethods(): void $this->assertCount(1, $shippingMethods); $this->assertEquals(1, $shippingMethods[0]->getId()); $this->assertEquals(1, $shippingMethods[0]->getMinimumWeight()); - $this->assertEquals(1000, $shippingMethods[0]->getMaximumWeight()); + $this->assertEquals(1001, $shippingMethods[0]->getMaximumWeight()); $this->assertEquals('carrier_code', $shippingMethods[0]->getCarrier()); $this->assertEquals(['BE' => 350, 'NL' => 420], $shippingMethods[0]->getPrices()); $this->assertEquals(420, $shippingMethods[0]->getPriceForCountry('NL')); From b3b78cb949b73af20f0c939d7f585dc2a95c00f7 Mon Sep 17 00:00:00 2001 From: Alexis POUPELIN Date: Mon, 20 Nov 2023 10:45:31 +0100 Subject: [PATCH 2/3] Moved conversion into parameters (as suggested) --- src/Model/ShippingMethod.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Model/ShippingMethod.php b/src/Model/ShippingMethod.php index 9425cc7..0ef3340 100644 --- a/src/Model/ShippingMethod.php +++ b/src/Model/ShippingMethod.php @@ -11,14 +11,11 @@ public static function fromData(array $data): self $prices[$country['iso_2']] = (int)($country['price'] * 100); } - $min_weight = intval(ceil($data['min_weight'] * 1000)); - $max_weight = intval(ceil($data['max_weight'] * 1000)); - return new self( (int)$data['id'], (string)$data['name'], - (int) $min_weight, - (int) $max_weight, + (int) ceil($data['min_weight'] * 1000.0), + (int) ceil($data['max_weight'] * 1000.0), (string)$data['carrier'], $prices, $data['service_point_input'] !== 'none', From 3ef7240da87b7a6f591d289005dc2f6876de37d0 Mon Sep 17 00:00:00 2001 From: Villermen Date: Tue, 21 Nov 2023 12:41:12 +0100 Subject: [PATCH 3/3] Use round() for correcting float math imprecision --- src/Model/ParcelItem.php | 2 +- src/Model/ShippingMethod.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Model/ParcelItem.php b/src/Model/ParcelItem.php index e89e243..debd610 100644 --- a/src/Model/ParcelItem.php +++ b/src/Model/ParcelItem.php @@ -12,7 +12,7 @@ public static function fromData(array $data): self return new self( (string)$data['description'], (int)$data['quantity'], - round(((float)$data['weight']) * 1000), + (int)round(((float)$data['weight']) * 1000), (float)$data['value'], isset($data['hs_code']) ? (string)$data['hs_code'] : null, isset($data['origin_country']) ? (string)$data['origin_country'] : null, diff --git a/src/Model/ShippingMethod.php b/src/Model/ShippingMethod.php index 0ef3340..fd2d39b 100644 --- a/src/Model/ShippingMethod.php +++ b/src/Model/ShippingMethod.php @@ -14,8 +14,8 @@ public static function fromData(array $data): self return new self( (int)$data['id'], (string)$data['name'], - (int) ceil($data['min_weight'] * 1000.0), - (int) ceil($data['max_weight'] * 1000.0), + (int)round($data['min_weight'] * 1000.0), + (int)round($data['max_weight'] * 1000.0), (string)$data['carrier'], $prices, $data['service_point_input'] !== 'none',