From e180aea88fbd0c992201fc76fea2f25bbd2529ea Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 15:50:36 +0530 Subject: [PATCH 1/7] Added method to reset points collection This is useful when calculating distances between multiple points grouping by some other fields. --- src/Calculator.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Calculator.php b/src/Calculator.php index 4b6f69b..45ffbba 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -74,6 +74,16 @@ public function removePoint($key = null) throw new \InvalidArgumentException('The point key does not exist.'); } + /** + * Resets the points collection. + * @return \Ballen\Distical\Calculator + */ + public function resetPoints($key = null) + { + $this->points = []; + return $this; + } + /** * Helper method to get distance between two points. * @param LatLong $pointA Point A (eg. Departure point) @@ -113,9 +123,10 @@ private function distanceBetweenPoints(LatLong $pointA, LatLong $pointB) /** * Calculates the distance between each of the points. + * @param bool $shouldResetPoints Clears points collection after calculating distance if true * @return double Distance in kilometres. */ - private function calculate() + private function calculate(bool $shouldResetPoints = false) { if (count($this->points) < 2) { throw new \RuntimeException('There must be two or more points (co-ordinates) before a calculation can be performed.'); @@ -127,15 +138,21 @@ private function calculate() } $previous = $point; } + + if($shouldResetPoints === true) { + $this->resetPoints(); + } + return $total; } /** * Returns the total distance between the two lat/lng points. + * @param bool $shouldResetPoints Resets points collection after calculating distance if true * @return Distance */ - public function get() + public function get(bool $shouldResetPoints = false) { - return new Distance($this->calculate()); + return new Distance($this->calculate($shouldResetPoints)); } } From 3dc13f30d57e6ebf585a956da54f9a9b31e190f6 Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:14:15 +0530 Subject: [PATCH 2/7] Added Examples --- examples/index.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/index.php b/examples/index.php index 3034e8a..abf71f4 100644 --- a/examples/index.php +++ b/examples/index.php @@ -46,3 +46,31 @@ // Output the distance in summary: echo "

Distance from Colchester to Ipswich and then straight on to Aylesbury is: " . $distance->asKilometres(). "km (or " . $distance->asMiles() . " miles).

"; + +// Reset the points and add new ones... +$calculator = new Calculator(); +$calculator->addPoint($centralIpswich); +$calculator->addPoint($centralAylesbury); +$calculator->resetPoints(); +$calculator->addPoint($centralAylesbury); +$calculator->addPoint($centralColchester); + +$distance = $calculator->get(); + +// Output the distance in summary: +echo "

Distance from Aylesbury to Colchester is: " . $distance->asKilometres(). "km (or " . $distance->asMiles() . " miles).

"; + +// Reset the points with calculations and calculate new ones... +$calculator = new Calculator(); +$calculator->addPoint($centralIpswich); +$calculator->addPoint($centralAylesbury); +$distance1 = $calculator->get(true); + +$calculator->addPoint($centralAylesbury); +$calculator->addPoint($centralColchester); + +$distance2 = $calculator->get(true); + +// Output the distance in summary: +echo "

Distance from Ipswich to Aylesbury is: " . $distance1->asKilometres(). "km (or " . $distance1->asMiles() . " miles).

"; +echo "

Distance from Aylesbury to Colchester is: " . $distance2->asKilometres(). "km (or " . $distance2->asMiles() . " miles).

"; From 400e1da655edf9b113505eff7f40b79fafd96d60 Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:14:28 +0530 Subject: [PATCH 3/7] Added Tests for resetPoints --- tests/DisticalTest.php | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/DisticalTest.php b/tests/DisticalTest.php index cd7fb18..49e5e76 100644 --- a/tests/DisticalTest.php +++ b/tests/DisticalTest.php @@ -79,6 +79,51 @@ public function testRemovePointWithKey() $calculator->get(); } + public function testResetPoints() + { + $calculator = new Calculator(); + $calculator->addPoint($this->latlong1); + $calculator->addPoint($this->latlong2); + $this->assertInstanceOf(Distance::class, $calculator->get()); + + $calculator->resetPoints(); + $calculator->addPoint($this->latlong3); + $this->expectExceptionMessage( + 'There must be two or more points (co-ordinates) before a calculation can be performed.' + ); + $calculator->get(); + + $calculator->addPoint($this->latlong2); + $this->assertEquals(6.273748050460542, $calculator->get()->asKilometres()); + } + + public function testCalculationsAfterResetPoints() + { + $calculator = new Calculator(); + $calculator->addPoint($this->latlong1); + $calculator->addPoint($this->latlong2); + $calculator->resetPoints(); + $calculator->addPoint($this->latlong2); + $calculator->addPoint($this->latlong3); + + $this->assertEquals(6.273748050460542, $calculator->get()->asKilometres()); + $this->assertNotEquals(9.444924713131321, $calculator->get()->asKilometres()); + } + + public function testResetPointsAfterCalculation() + { + $calculator = new Calculator(); + $calculator->addPoint($this->latlong1); + $calculator->addPoint($this->latlong2); + $this->assertInstanceOf(Distance::class, $calculator->get(true)); + + $calculator->addPoint($this->latlong3); + $this->expectExceptionMessage( + 'There must be two or more points (co-ordinates) before a calculation can be performed.' + ); + $calculator->get(); + } + public function testRemovePointWithInvalidKey() { $this->expectExceptionMessage('The point key does not exist.'); From d63faf9a5c5d3802ceac7577b30e7fa4911c6af1 Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:22:46 +0530 Subject: [PATCH 4/7] Changed condition to accept 0 as valid distance --- src/Entities/Distance.php | 4 ++-- tests/DistanceEntityTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Entities/Distance.php b/src/Entities/Distance.php index 97839c2..26427df 100644 --- a/src/Entities/Distance.php +++ b/src/Entities/Distance.php @@ -54,8 +54,8 @@ private function validateDistance($distance) if (!is_numeric($distance)) { throw new \InvalidArgumentException('The distance value must be of a valid type.'); } - if (!$distance > 0) { - throw new \InvalidArgumentException('The distance must be greater than zero!'); + if ($distance < 0) { + throw new \InvalidArgumentException('The distance must be greater than or equals zero!'); } } diff --git a/tests/DistanceEntityTest.php b/tests/DistanceEntityTest.php index 4989cc2..e5cfcd3 100644 --- a/tests/DistanceEntityTest.php +++ b/tests/DistanceEntityTest.php @@ -42,8 +42,8 @@ public function testInvalidEntityCreationWithAsString() public function testInvalidEntityCreationWithZero() { - $this->expectException('InvalidArgumentException', 'The distance must be greater than zero!'); - $test = new Distance(0); + $this->expectException('InvalidArgumentException', 'The distance must be greater than or equals zero!'); + $test = new Distance(-1); } public function testConversionToKilometres() From 38b023671c89985c707fd8950bb4df6559fd764a Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:32:12 +0530 Subject: [PATCH 5/7] Added parameter to check if treat zero as valid distance? --- src/Entities/Distance.php | 11 ++++++++--- tests/DistanceEntityTest.php | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Entities/Distance.php b/src/Entities/Distance.php index 26427df..c59a2e8 100644 --- a/src/Entities/Distance.php +++ b/src/Entities/Distance.php @@ -37,9 +37,9 @@ class Distance * Class constructor * @param mixed $kilometres The distance in kilometres. */ - public function __construct($kilometres = 0) + public function __construct($kilometres = 0, $allowZero = false) { - $this->validateDistance($kilometres); + $this->validateDistance($kilometres, $allowZero); $this->kilometres = $kilometres; } @@ -49,11 +49,16 @@ public function __construct($kilometres = 0) * @throws \InvalidArgumentException * @return void */ - private function validateDistance($distance) + private function validateDistance($distance, $allowZero = false) { if (!is_numeric($distance)) { throw new \InvalidArgumentException('The distance value must be of a valid type.'); } + + if (!$allowZero && $distance === 0) { + throw new \InvalidArgumentException('The distance must be greater than zero!'); + } + if ($distance < 0) { throw new \InvalidArgumentException('The distance must be greater than or equals zero!'); } diff --git a/tests/DistanceEntityTest.php b/tests/DistanceEntityTest.php index e5cfcd3..da54ca4 100644 --- a/tests/DistanceEntityTest.php +++ b/tests/DistanceEntityTest.php @@ -36,14 +36,27 @@ public function testEntityCreation() public function testInvalidEntityCreationWithAsString() { - $this->expectException('InvalidArgumentException', 'The distance value must be of a valid type.'); - $test = new Distance('a random string'); + $this->expectExceptionMessage('The distance value must be of a valid type.'); + new Distance('a random string'); } public function testInvalidEntityCreationWithZero() { - $this->expectException('InvalidArgumentException', 'The distance must be greater than or equals zero!'); - $test = new Distance(-1); + $this->expectExceptionMessage('The distance must be greater than zero!'); + new Distance(0); + } + + public function testInvalidEntityCreationWithNegative() + { + $this->expectExceptionMessage('The distance must be greater than or equals zero!'); + new Distance(-1, true); + } + + public function testEntityCreationWithAllowedZero() + { + $test = new Distance(0, true); + $this->assertInstanceOf(Distance::class, $test); + $this->assertEquals(0, $test->asKilometres()); } public function testConversionToKilometres() From 326deee6f33332537bccd6b93209d9613b9f9958 Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:34:33 +0530 Subject: [PATCH 6/7] Update Calculator.php --- src/Calculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator.php b/src/Calculator.php index 45ffbba..8a4d540 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -78,7 +78,7 @@ public function removePoint($key = null) * Resets the points collection. * @return \Ballen\Distical\Calculator */ - public function resetPoints($key = null) + public function resetPoints() { $this->points = []; return $this; From 258aa335a1d0acfd77482d191eafe2a4a7fda732 Mon Sep 17 00:00:00 2001 From: Jagdish Patel Date: Sun, 29 Oct 2023 16:35:34 +0530 Subject: [PATCH 7/7] Update Calculator.php --- src/Calculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator.php b/src/Calculator.php index 8a4d540..4380b55 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -139,7 +139,7 @@ private function calculate(bool $shouldResetPoints = false) $previous = $point; } - if($shouldResetPoints === true) { + if ($shouldResetPoints === true) { $this->resetPoints(); }