Skip to content

Commit

Permalink
Merge pull request #4 from Jagdish-J-P/patch-1
Browse files Browse the repository at this point in the history
Added method to reset points collection
  • Loading branch information
allebb authored Nov 8, 2023
2 parents 58b40b2 + 258aa33 commit 480142a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
28 changes: 28 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,31 @@

// Output the distance in summary:
echo "<p>Distance from Colchester to Ipswich and then straight on to Aylesbury is: " . $distance->asKilometres(). "km (or " . $distance->asMiles() . " miles).</p>";

// 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 "<p>Distance from Aylesbury to Colchester is: " . $distance->asKilometres(). "km (or " . $distance->asMiles() . " miles).</p>";

// 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 "<p>Distance from Ipswich to Aylesbury is: " . $distance1->asKilometres(). "km (or " . $distance1->asMiles() . " miles).</p>";
echo "<p>Distance from Aylesbury to Colchester is: " . $distance2->asKilometres(). "km (or " . $distance2->asMiles() . " miles).</p>";
23 changes: 20 additions & 3 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
$this->points = [];
return $this;
}

/**
* Helper method to get distance between two points.
* @param LatLong $pointA Point A (eg. Departure point)
Expand Down Expand Up @@ -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.');
Expand All @@ -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));
}
}
13 changes: 9 additions & 4 deletions src/Entities/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -49,14 +49,19 @@ 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 (!$distance > 0) {

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!');
}
}

/**
Expand Down
21 changes: 17 additions & 4 deletions tests/DistanceEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 zero!');
$test = new Distance(0);
$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()
Expand Down
45 changes: 45 additions & 0 deletions tests/DisticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down

0 comments on commit 480142a

Please sign in to comment.