diff --git a/src/Distance.php b/src/Distance.php index fa8869f..e3365bd 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -3,7 +3,6 @@ namespace TeamChallengeApps\Distance; use Illuminate\Support\Arr; -use App\Exceptions\DistanceException; use Exception; class Distance @@ -19,7 +18,7 @@ public function __construct($value, $unit = 'meters', $config = null) { $this->setDistance($value, $unit); - if ( ! is_null($config) ) { + if (!is_null($config)) { $this->config = $config; } } @@ -103,9 +102,9 @@ public function isEmpty() public function percentageOf(Distance $distance, $overflow = true) { - $percentage = ( $this->asBase() / $distance->asBase() ); + $percentage = ($this->asBase() / $distance->asBase()); - if ( $overflow ) { + if ($overflow) { return round($percentage * 100); } @@ -122,27 +121,27 @@ public function percentageOf(Distance $distance, $overflow = true) public function lt(Distance $distance) { - return ( $this->asBase() < $distance->asBase() ); + return $this->asBase() < $distance->asBase(); } public function lte(Distance $distance) { - return ( $this->asBase() <= $distance->asBase() ); + return $this->asBase() <= $distance->asBase(); } public function gt(Distance $distance) { - return ( $this->asBase() > $distance->asBase() ); + return $this->asBase() > $distance->asBase(); } public function gte(Distance $distance) { - return ( $this->asBase() >= $distance->asBase() ); + return $this->asBase() >= $distance->asBase(); } public function isUnit($unit) { - return ( $this->unit == $unit ); + return $this->unit == $unit; } public function isMeters() @@ -182,7 +181,7 @@ public function units() public function getDecimals() { - $key = 'units.' . $this->unit . '.decimals'; + $key = 'units.'.$this->unit.'.decimals'; return $this->config($key, 2); } @@ -190,12 +189,12 @@ public function getDecimals() public function getMeasurement($unit = null) { $unit = $unit ? $unit : $this->unit; - $key = 'units.' . $unit . '.unit'; + $key = 'units.'.$unit.'.unit'; $measurement = $this->config($key); - if ( ! $measurement ) { - throw new Exception('Measurement ' . $unit . ' not found'); + if (!$measurement) { + throw new Exception('Measurement '.$unit.' not found'); } return $measurement; @@ -223,7 +222,7 @@ public function config($key = null, $fallback = null) protected function loadConfig() { - if ( ! function_exists('config') ) { + if (!function_exists('config')) { throw new Exception('Unable to auto load config'); } @@ -254,7 +253,7 @@ public function increment(Distance $distance) public function toBase() { - if ( $this->isMeters() ) { + if ($this->isMeters()) { return $this; } @@ -268,7 +267,7 @@ public function base() public function asBase() { - if ( $this->isMeters() ) { + if ($this->isMeters()) { return $this->value; } @@ -280,7 +279,7 @@ public function asUnit($unit) $from = $this->getMeasurement($this->unit); $to = $this->getMeasurement($unit); - return ( $this->value * $to * (1 / $from) ); + return $this->value * $to * (1 / $from); } public function convertTo($unit) @@ -321,7 +320,7 @@ public function all() { $units = new DistanceCollection(); - foreach ( $this->units() as $unit ) { + foreach ($this->units() as $unit) { $units->put($unit, $this->convertTo($unit)); } @@ -335,7 +334,7 @@ public function toArray() public function toRoundedArray() { - return $this->all()->map(function($unit){ + return $this->all()->map(function ($unit) { return $unit->round(); }); } @@ -359,10 +358,10 @@ public function __get($property) { if (in_array($property, $this->units())) { $unit = $property; + return $this->convertTo($unit)->distance; } - - return null; - } + return; + } } diff --git a/src/DistanceCollection.php b/src/DistanceCollection.php index c77c613..b5479cf 100644 --- a/src/DistanceCollection.php +++ b/src/DistanceCollection.php @@ -4,8 +4,6 @@ use Illuminate\Support\Collection; -class DistanceCollection extends Collection { - - - -} \ No newline at end of file +class DistanceCollection extends Collection +{ +} diff --git a/src/DistanceServiceProvider.php b/src/DistanceServiceProvider.php index 8c119d7..d3517da 100644 --- a/src/DistanceServiceProvider.php +++ b/src/DistanceServiceProvider.php @@ -7,7 +7,7 @@ class DistanceServiceProvider extends ServiceProvider { /** - * {@inheritDoc} + * {@inheritdoc} */ public function register() { @@ -16,8 +16,6 @@ public function register() /** * Setup the package config. - * - * @return void */ protected function setupConfig() { @@ -29,5 +27,4 @@ protected function setupConfig() $config => config_path('distance.php'), ], 'config'); } - -} \ No newline at end of file +} diff --git a/src/config/config.php b/src/config/config.php index d154676..37d4832 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -2,14 +2,14 @@ return [ - 'units' => [ + 'units' => [ - 'meters' => [ + 'meters' => [ 'suffix' => 'm', 'unit' => 1.00, 'decimals' => 2, ], - 'kilometers' => [ + 'kilometers' => [ 'format' => 'km', 'unit' => 0.001, 'decimals' => 2, @@ -20,11 +20,11 @@ 'decimals' => 2, ], 'footsteps' => [ - 'format' => 'steps', - 'unit' => 1.458151063, - 'decimals' => 0, + 'format' => 'steps', + 'unit' => 1.458151063, + 'decimals' => 0, ], - ], + ], -]; \ No newline at end of file +]; diff --git a/src/helpers.php b/src/helpers.php index 599e43f..479af6b 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,9 +2,10 @@ use TeamChallengeApps\Distance\Distance; -if ( ! function_exists('distance_value') ) { - function distance_value($value, $unit = 'meters') { - if ( ! $value instanceof Distance ) { +if (!function_exists('distance_value')) { + function distance_value($value, $unit = 'meters') + { + if (!$value instanceof Distance) { $value = new Distance($value, $unit); } @@ -12,12 +13,13 @@ function distance_value($value, $unit = 'meters') { } } -if ( ! function_exists('distance_get') ) { - function distance_get($value, $unit = 'meters', $from = 'meters') { - if ( ! $value instanceof Distance ) { - $value = new Distance($value, $from); - } +if (!function_exists('distance_get')) { + function distance_get($value, $unit = 'meters', $from = 'meters') + { + if (!$value instanceof Distance) { + $value = new Distance($value, $from); + } - return $value->asUnit($unit); - } -} \ No newline at end of file + return $value->asUnit($unit); + } +} diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index be1751c..09fd943 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -18,143 +18,134 @@ public function it_creates_distance() /** @test **/ public function it_converts_to_kilometers() { - $map = new Collection([ - '1000' => 1, - '1500' => 1.5, - '2750' => 2.75, - ]); + $map = new Collection([ + '1000' => 1, + '1500' => 1.5, + '2750' => 2.75, + ]); - foreach ( $map as $meters => $km ) { + foreach ($map as $meters => $km) { + $distance = new Distance($meters); + $distance = $this->loadConfig($distance); - $distance = new Distance($meters); - $distance = $this->loadConfig($distance); + $distance = $distance->toKilometers(); - $distance = $distance->toKilometers(); - - $this->assertEquals($distance->value, $km); - - } + $this->assertEquals($distance->value, $km); + } } /** @test **/ public function it_converts_from_kilometers() { - $map = new Collection([ - '1' => 1000, - '1.5' => 1500, - '2.75' => 2750, - ]); + $map = new Collection([ + '1' => 1000, + '1.5' => 1500, + '2.75' => 2750, + ]); - foreach ( $map as $km => $meters ) { + foreach ($map as $km => $meters) { + $distance = new Distance((float) $km, 'kilometers'); + $distance = $this->loadConfig($distance); - $distance = new Distance((float) $km, 'kilometers'); - $distance = $this->loadConfig($distance); + $distance = $distance->toMeters(); - $distance = $distance->toMeters(); - - $this->assertEquals($distance->value, $meters); - } + $this->assertEquals($distance->value, $meters); + } } /** @test **/ public function it_converts_to_miles() { - $map = new Collection([ - '1000' => 0.62, - '1500' => 0.93, - '2750' => 1.71, - ]); - - foreach ( $map as $meters => $miles ) { + $map = new Collection([ + '1000' => 0.62, + '1500' => 0.93, + '2750' => 1.71, + ]); - $distance = new Distance($meters); - $distance = $this->loadConfig($distance); + foreach ($map as $meters => $miles) { + $distance = new Distance($meters); + $distance = $this->loadConfig($distance); - $distance = $distance->toMiles(); - $distance = $this->loadConfig($distance); + $distance = $distance->toMiles(); + $distance = $this->loadConfig($distance); - $this->assertEquals($distance->round(), $miles); - - } + $this->assertEquals($distance->round(), $miles); + } } /** @test **/ public function it_converts_to_steps() { - $map = new Collection([ - '1' => 1, - '1000' => 1458, - '1500' => 2187, - '2750' => 4010, - ]); - - foreach ( $map as $meters => $steps ) { - - $distance = new Distance($meters); - $distance = $this->loadConfig($distance); - - $distance = $distance->toSteps(); - $distance = $this->loadConfig($distance); - - $this->assertEquals($distance->round(), $steps); - - } + $map = new Collection([ + '1' => 1, + '1000' => 1458, + '1500' => 2187, + '2750' => 4010, + ]); + + foreach ($map as $meters => $steps) { + $distance = new Distance($meters); + $distance = $this->loadConfig($distance); + + $distance = $distance->toSteps(); + $distance = $this->loadConfig($distance); + + $this->assertEquals($distance->round(), $steps); + } } /** @test **/ public function it_formats_to_string_automatically() { - $meters = 10000; + $meters = 10000; - $distance = new Distance(10000); - $distance = $this->loadConfig($distance); + $distance = new Distance(10000); + $distance = $this->loadConfig($distance); - $string = number_format($meters, 2, '.', ','); + $string = number_format($meters, 2, '.', ','); - $this->assertEquals((string) $distance, $string); + $this->assertEquals((string) $distance, $string); } /** @test **/ public function it_formats_to_steps_string_without_decimals() { - $meters = 10000; + $meters = 10000; - $distance = new Distance(10000, 'footsteps'); - $distance = $this->loadConfig($distance); + $distance = new Distance(10000, 'footsteps'); + $distance = $this->loadConfig($distance); - $string = number_format($meters, 0, '.', ','); + $string = number_format($meters, 0, '.', ','); - $this->assertEquals((string) $distance, $string); + $this->assertEquals((string) $distance, $string); } /** @test **/ public function it_allows_global_distance_function() { - $distance = new Distance(1000); - $helper = distance_value(1000); + $distance = new Distance(1000); + $helper = distance_value(1000); - $this->assertEquals($distance, $helper); + $this->assertEquals($distance, $helper); } /** @test **/ public function it_converts_to_unit_value() { - $distance = new Distance(1000); + $distance = new Distance(1000); - $map = new Collection([ - '1000' => 1, - '1500' => 1.5, - '2750' => 2.75, - ]); + $map = new Collection([ + '1000' => 1, + '1500' => 1.5, + '2750' => 2.75, + ]); - foreach ( $map as $meters => $km ) { + foreach ($map as $meters => $km) { + $distance = new Distance($meters); + $distance = $this->loadConfig($distance); - $distance = new Distance($meters); - $distance = $this->loadConfig($distance); - - $this->assertEquals($distance->asUnit('kilometers'), $km); - - } + $this->assertEquals($distance->asUnit('kilometers'), $km); + } } /** @test **/ @@ -215,9 +206,8 @@ public function it_stays_clean_after_copying() protected function loadConfig(Distance $distance) { - $config = include __DIR__.'/../src/config/config.php'; + $config = include __DIR__.'/../src/config/config.php'; - return $distance->setConfig($config); + return $distance->setConfig($config); } - -} \ No newline at end of file +}