Skip to content

Commit

Permalink
Added Mile and International Nautical Mile
Browse files Browse the repository at this point in the history
  • Loading branch information
jtejido committed Apr 14, 2018
1 parent 9abe7a5 commit a88b989
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
16 changes: 5 additions & 11 deletions src/Geodesy/Distance/BaseDistance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Geodesy\Location\LatLong;
use Geodesy\Unit\UnitInterface;
use Geodesy\Unit\Metre;
use Geodesy\Unit\Mile;
use Geodesy\Unit\KiloMetre;

abstract class BaseDistance
{
Expand All @@ -22,7 +24,7 @@ public function __construct(LatLong $source, LatLong $destination)
$this->source = $source;
$this->destination = $destination;
$this->radius = self::R;
$this->unit = null;
$this->unit = new KiloMetre;
}

public function setUnit(UnitInterface $unit)
Expand All @@ -37,22 +39,14 @@ public function getUnit()

public function getDistance()
{
$dist = $this->distance();
if($this->getUnit() instanceof Metre){
$dist = $dist * 1000;
}
$this->unit->setValue($dist);
return $this->unit->getValue();
return $this->getUnit()->convert($this->distance());
}

abstract public function distance();

public function isInRange($range)
{
if($this->getUnit() instanceof Metre){
$range = $range * 1000;
}
return $this->getDistance() <= $range;
return $this->getDistance() <= $this->getUnit()->convert($range);
}

}
4 changes: 0 additions & 4 deletions src/Geodesy/Distance/HaversineFormula.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Geodesy\Distance;

use Geodesy\Location\LatLong;
use Geodesy\Unit\KiloMetre;
use Geodesy\Unit\Metre;


class HaversineFormula extends BaseDistance implements DistanceInterface
{
Expand All @@ -14,7 +11,6 @@ class HaversineFormula extends BaseDistance implements DistanceInterface
public function __construct(LatLong $source, LatLong $destination)
{
parent::__construct($source, $destination);
$this->unit = new Kilometre;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Geodesy/Distance/SphericalCosine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace Geodesy\Distance;

use Geodesy\Location\LatLong;
use Geodesy\Unit\KiloMetre;
use Geodesy\Unit\Metre;

class SphericalCosine extends BaseDistance implements DistanceInterface
{

public function __construct(LatLong $source, LatLong $destination)
{
parent::__construct($source, $destination);
$this->unit = new Kilometre;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Geodesy/Distance/VincentyFormula.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace Geodesy\Distance;

use Geodesy\Location\LatLong;
use Geodesy\Unit\Metre;
use Geodesy\Unit\KiloMetre;

class VincentyFormula extends BaseDistance implements DistanceInterface
{

public function __construct(LatLong $source, LatLong $destination)
{
parent::__construct($source, $destination);
$this->unit = new KiloMetre;
}

/**
Expand Down
20 changes: 2 additions & 18 deletions src/Geodesy/Unit/BaseUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,9 @@
namespace Geodesy\Unit;


class BaseUnit
abstract class BaseUnit
{

protected $value;

public function __construct()
{
$this->value = null;
}

public function setValue($value)
{
$this->value = $value;
}

public function getValue()
{
return $this->value;
}

abstract public function convert($value);

}
5 changes: 5 additions & 0 deletions src/Geodesy/Unit/KiloMetre.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
class KiloMetre extends BaseUnit implements UnitInterface
{

// This is our base unit, so just return it
public function convert($value)
{
return $value;
}

}
4 changes: 4 additions & 0 deletions src/Geodesy/Unit/Metre.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
class Metre extends BaseUnit implements UnitInterface
{

public function convert($value)
{
return $value * 1000;
}

}
14 changes: 14 additions & 0 deletions src/Geodesy/Unit/Mile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Geodesy\Unit;


class Mile extends BaseUnit implements UnitInterface
{

public function convert($value)
{
return $value * 0.62137;
}

}
14 changes: 14 additions & 0 deletions src/Geodesy/Unit/NauticalMile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Geodesy\Unit;


class NauticalMile extends BaseUnit implements UnitInterface
{

public function convert($value)
{
return $value * 0.539957;
}

}

0 comments on commit a88b989

Please sign in to comment.