diff --git a/src/Geodesy/Distance/BaseDistance.php b/src/Geodesy/Distance/BaseDistance.php index 0335fe0..5557c13 100644 --- a/src/Geodesy/Distance/BaseDistance.php +++ b/src/Geodesy/Distance/BaseDistance.php @@ -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 { @@ -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) @@ -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); } } \ No newline at end of file diff --git a/src/Geodesy/Distance/HaversineFormula.php b/src/Geodesy/Distance/HaversineFormula.php index 1a93868..e6f6f01 100644 --- a/src/Geodesy/Distance/HaversineFormula.php +++ b/src/Geodesy/Distance/HaversineFormula.php @@ -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 { @@ -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; } /** diff --git a/src/Geodesy/Distance/SphericalCosine.php b/src/Geodesy/Distance/SphericalCosine.php index 519a263..a3b8c0b 100644 --- a/src/Geodesy/Distance/SphericalCosine.php +++ b/src/Geodesy/Distance/SphericalCosine.php @@ -3,8 +3,6 @@ namespace Geodesy\Distance; use Geodesy\Location\LatLong; -use Geodesy\Unit\KiloMetre; -use Geodesy\Unit\Metre; class SphericalCosine extends BaseDistance implements DistanceInterface { @@ -12,7 +10,6 @@ class SphericalCosine extends BaseDistance implements DistanceInterface public function __construct(LatLong $source, LatLong $destination) { parent::__construct($source, $destination); - $this->unit = new Kilometre; } /** diff --git a/src/Geodesy/Distance/VincentyFormula.php b/src/Geodesy/Distance/VincentyFormula.php index e1fc1bc..19de488 100644 --- a/src/Geodesy/Distance/VincentyFormula.php +++ b/src/Geodesy/Distance/VincentyFormula.php @@ -3,8 +3,6 @@ namespace Geodesy\Distance; use Geodesy\Location\LatLong; -use Geodesy\Unit\Metre; -use Geodesy\Unit\KiloMetre; class VincentyFormula extends BaseDistance implements DistanceInterface { @@ -12,7 +10,6 @@ class VincentyFormula extends BaseDistance implements DistanceInterface public function __construct(LatLong $source, LatLong $destination) { parent::__construct($source, $destination); - $this->unit = new KiloMetre; } /** diff --git a/src/Geodesy/Unit/BaseUnit.php b/src/Geodesy/Unit/BaseUnit.php index f71d286..56aa56a 100644 --- a/src/Geodesy/Unit/BaseUnit.php +++ b/src/Geodesy/Unit/BaseUnit.php @@ -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); } \ No newline at end of file diff --git a/src/Geodesy/Unit/KiloMetre.php b/src/Geodesy/Unit/KiloMetre.php index 28e9ea2..65c17bb 100644 --- a/src/Geodesy/Unit/KiloMetre.php +++ b/src/Geodesy/Unit/KiloMetre.php @@ -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; + } } \ No newline at end of file diff --git a/src/Geodesy/Unit/Metre.php b/src/Geodesy/Unit/Metre.php index 73242b7..38c0463 100644 --- a/src/Geodesy/Unit/Metre.php +++ b/src/Geodesy/Unit/Metre.php @@ -6,5 +6,9 @@ class Metre extends BaseUnit implements UnitInterface { + public function convert($value) + { + return $value * 1000; + } } \ No newline at end of file diff --git a/src/Geodesy/Unit/Mile.php b/src/Geodesy/Unit/Mile.php new file mode 100644 index 0000000..8cd7edb --- /dev/null +++ b/src/Geodesy/Unit/Mile.php @@ -0,0 +1,14 @@ +