From 2dff3de9e1ef703201e94f71b96f0ddb84c4bd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 6 Jul 2021 13:27:40 +0200 Subject: [PATCH 1/4] Refactor latitude and longitude to decimals --- src/Model/PickupPoint.php | 19 +++++++++++++------ src/Model/PickupPointInterface.php | 14 ++++++++++---- src/Provider/FakerProvider.php | 4 ++-- src/Provider/GlsProvider.php | 4 ++-- src/Provider/PostNordProvider.php | 4 ++-- .../config/doctrine/model/PickupPoint.orm.xml | 14 +++++++------- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/Model/PickupPoint.php b/src/Model/PickupPoint.php index 9969343..8b942f9 100644 --- a/src/Model/PickupPoint.php +++ b/src/Model/PickupPoint.php @@ -5,6 +5,7 @@ namespace Setono\SyliusPickupPointPlugin\Model; use function sprintf; +use Webmozart\Assert\Assert; class PickupPoint implements PickupPointInterface { @@ -22,9 +23,9 @@ class PickupPoint implements PickupPointInterface protected ?string $country = null; - protected ?string $latitude = null; + protected ?float $latitude = null; - protected ?string $longitude = null; + protected ?float $longitude = null; public function getId(): ?int { @@ -91,23 +92,29 @@ public function setCountry(string $country): void $this->country = $country; } - public function getLatitude(): ?string + public function getLatitude(): ?float { return $this->latitude; } - public function setLatitude(?string $latitude): void + public function setLatitude(?float $latitude): void { + Assert::nullOrGreaterThanEq($latitude, -90); + Assert::nullOrLessThanEq($latitude, 90); + $this->latitude = $latitude; } - public function getLongitude(): ?string + public function getLongitude(): ?float { return $this->longitude; } - public function setLongitude(?string $longitude): void + public function setLongitude(?float $longitude): void { + Assert::nullOrGreaterThanEq($longitude, -180); + Assert::nullOrLessThanEq($longitude, 180); + $this->longitude = $longitude; } diff --git a/src/Model/PickupPointInterface.php b/src/Model/PickupPointInterface.php index 0b434b7..c18621d 100644 --- a/src/Model/PickupPointInterface.php +++ b/src/Model/PickupPointInterface.php @@ -35,13 +35,19 @@ public function getCountry(): ?string; public function setCountry(string $country): void; - public function getLatitude(): ?string; + public function getLatitude(): ?float; - public function setLatitude(?string $latitude): void; + /** + * @throws \InvalidArgumentException if the $latitude is invalid + */ + public function setLatitude(?float $latitude): void; - public function getLongitude(): ?string; + public function getLongitude(): ?float; - public function setLongitude(?string $longitude): void; + /** + * @throws \InvalidArgumentException if the $longitude is invalid + */ + public function setLongitude(?float $longitude): void; public function getFullAddress(): string; } diff --git a/src/Provider/FakerProvider.php b/src/Provider/FakerProvider.php index 7ae86ec..8c7e469 100644 --- a/src/Provider/FakerProvider.php +++ b/src/Provider/FakerProvider.php @@ -80,8 +80,8 @@ private function createFakePickupPoint(string $index, ?string $countryCode = nul $pickupPoint->setZipCode((string) $this->faker->numberBetween(11111, 99999)); $pickupPoint->setCity($this->faker->city); $pickupPoint->setCountry($countryCode); - $pickupPoint->setLatitude((string) $this->faker->latitude); - $pickupPoint->setLongitude((string) $this->faker->longitude); + $pickupPoint->setLatitude($this->faker->latitude); + $pickupPoint->setLongitude($this->faker->longitude); return $pickupPoint; } diff --git a/src/Provider/GlsProvider.php b/src/Provider/GlsProvider.php index bbb1661..9f4f4df 100644 --- a/src/Provider/GlsProvider.php +++ b/src/Provider/GlsProvider.php @@ -121,8 +121,8 @@ private function transform(ParcelShop $parcelShop): PickupPointInterface $pickupPoint->setZipCode($parcelShop->getZipCode()); $pickupPoint->setCity($parcelShop->getCity()); $pickupPoint->setCountry($parcelShop->getCountryCode()); - $pickupPoint->setLatitude($parcelShop->getLatitude()); - $pickupPoint->setLongitude($parcelShop->getLongitude()); + $pickupPoint->setLatitude((float) $parcelShop->getLatitude()); + $pickupPoint->setLongitude((float) $parcelShop->getLongitude()); return $pickupPoint; } diff --git a/src/Provider/PostNordProvider.php b/src/Provider/PostNordProvider.php index 81770c2..e7a0979 100644 --- a/src/Provider/PostNordProvider.php +++ b/src/Provider/PostNordProvider.php @@ -138,8 +138,8 @@ private function transform(array $servicePoint): PickupPointInterface $latitude = $longitude = null; if (isset($servicePoint['coordinates'][0])) { - $latitude = (string) $servicePoint['coordinates'][0]['northing']; - $longitude = (string) $servicePoint['coordinates'][0]['easting']; + $latitude = (float) $servicePoint['coordinates'][0]['northing']; + $longitude = (float) $servicePoint['coordinates'][0]['easting']; } /** @var PickupPointInterface|object $pickupPoint */ diff --git a/src/Resources/config/doctrine/model/PickupPoint.orm.xml b/src/Resources/config/doctrine/model/PickupPoint.orm.xml index be7fdcb..5e9d65d 100644 --- a/src/Resources/config/doctrine/model/PickupPoint.orm.xml +++ b/src/Resources/config/doctrine/model/PickupPoint.orm.xml @@ -8,12 +8,12 @@ - - - - - - - + + + + + + + From 4f7c2563054416ea3e647b2bc8accd388cfdac64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 6 Jul 2021 13:34:34 +0200 Subject: [PATCH 2/4] Fix spec --- spec/Provider/DAOProviderSpec.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/Provider/DAOProviderSpec.php b/spec/Provider/DAOProviderSpec.php index 73f3977..45b9461 100644 --- a/spec/Provider/DAOProviderSpec.php +++ b/spec/Provider/DAOProviderSpec.php @@ -105,8 +105,8 @@ private function testPickupPoint($pickupPoint, string $id): void $pickupPoint->getZipCode()->shouldReturn('7100'); $pickupPoint->getCity()->shouldReturn('Vejle'); $pickupPoint->getCountry()->shouldReturn('DK'); - $pickupPoint->getLatitude()->shouldReturn('55.7119'); - $pickupPoint->getLongitude()->shouldReturn('9.539939'); + $pickupPoint->getLatitude()->shouldReturn(55.7119); + $pickupPoint->getLongitude()->shouldReturn(9.539939); } private function pickupPointArray(string $id): array @@ -118,8 +118,8 @@ private function pickupPointArray(string $id): array 'postnr' => '7100', 'bynavn' => 'Vejle', 'udsortering' => 'E', - 'latitude' => '55.7119', - 'longitude' => '9.539939', + 'latitude' => 55.7119, + 'longitude' => 9.539939, 'afstand' => 2.652, 'aabningstider' => [ 'man' => '08:00 - 22:00', From e253f8459000bd94f458e529cb8bf6cb50a88fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 6 Jul 2021 13:44:24 +0200 Subject: [PATCH 3/4] Fix mocks --- tests/Behat/Mocker/GlsProviderMocker.php | 21 ++++++++++--------- tests/Behat/Mocker/PostNordProviderMocker.php | 21 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/Behat/Mocker/GlsProviderMocker.php b/tests/Behat/Mocker/GlsProviderMocker.php index 15fed7d..6ff6cf1 100644 --- a/tests/Behat/Mocker/GlsProviderMocker.php +++ b/tests/Behat/Mocker/GlsProviderMocker.php @@ -33,16 +33,17 @@ public function findPickupPoints(OrderInterface $order): iterable public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface { - return new PickupPoint( - new PickupPointCode(self::PICKUP_POINT_ID, $this->getCode(), 'DK'), - 'Somewhere', - '1 Rainbow str', - '4499', - 'Aalborg', - 'DK', - '23N', - '180E' - ); + $pickupPoint = new PickupPoint(); + $pickupPoint->setCode(new PickupPointCode(self::PICKUP_POINT_ID, $this->getCode(), 'DK')); + $pickupPoint->setName('Somewhere'); + $pickupPoint->setAddress('1 Rainbow str'); + $pickupPoint->setZipCode('4499'); + $pickupPoint->setCity('Aalborg'); + $pickupPoint->setCountry('DK'); + $pickupPoint->setLatitude(57.046707); + $pickupPoint->setLongitude(9.935932); + + return $pickupPoint; } public function findAllPickupPoints(): iterable diff --git a/tests/Behat/Mocker/PostNordProviderMocker.php b/tests/Behat/Mocker/PostNordProviderMocker.php index bd04359..b5ad803 100644 --- a/tests/Behat/Mocker/PostNordProviderMocker.php +++ b/tests/Behat/Mocker/PostNordProviderMocker.php @@ -33,16 +33,17 @@ public function findPickupPoints(OrderInterface $order): iterable public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface { - return new PickupPoint( - new PickupPointCode(self::PICKUP_POINT_ID, $this->getCode(), 'DK'), - 'Somewhere', - '1 Rainbow str', - '1145', - 'Aalborg', - 'DK', - '23N', - '180E' - ); + $pickupPoint = new PickupPoint(); + $pickupPoint->setCode(new PickupPointCode(self::PICKUP_POINT_ID, $this->getCode(), 'DK')); + $pickupPoint->setName('Somewhere'); + $pickupPoint->setAddress('1 Rainbow str'); + $pickupPoint->setZipCode('4499'); + $pickupPoint->setCity('Aalborg'); + $pickupPoint->setCountry('DK'); + $pickupPoint->setLatitude(57.046707); + $pickupPoint->setLongitude(9.935932); + + return $pickupPoint; } public function findAllPickupPoints(): iterable From afcdc7f0479267bd036d77b6c6c3166e61e78265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Wed, 7 Jul 2021 12:10:21 +0200 Subject: [PATCH 4/4] Use range instead --- src/Model/PickupPoint.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Model/PickupPoint.php b/src/Model/PickupPoint.php index 8b942f9..0153e4f 100644 --- a/src/Model/PickupPoint.php +++ b/src/Model/PickupPoint.php @@ -99,8 +99,7 @@ public function getLatitude(): ?float public function setLatitude(?float $latitude): void { - Assert::nullOrGreaterThanEq($latitude, -90); - Assert::nullOrLessThanEq($latitude, 90); + Assert::nullOrRange($latitude, -90, 90); $this->latitude = $latitude; } @@ -112,8 +111,7 @@ public function getLongitude(): ?float public function setLongitude(?float $longitude): void { - Assert::nullOrGreaterThanEq($longitude, -180); - Assert::nullOrLessThanEq($longitude, 180); + Assert::nullOrRange($longitude, -180, 180); $this->longitude = $longitude; }