diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index eec845b6..af8afbac 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -73,7 +73,7 @@ class CreateSettingsTable extends Migration 'group' => 'general', 'value' => 'lbs', 'type' => 'select', - 'options' => 'lbs, kg', + 'options' => 'lbs,kg', 'description' => 'The weight unit', ]); @@ -95,13 +95,13 @@ class CreateSettingsTable extends Migration 'description' => 'The altitude units', ]); - $this->addSetting('general.liquid_unit', [ - 'name' => 'Liquid Units', + $this->addSetting('general.fuel_unit', [ + 'name' => 'Fuel Units', 'group' => 'general', - 'value' => 'gal', + 'value' => 'lbs', 'type' => 'select', - 'options' => 'liters,gal', - 'description' => 'The liquid units', + 'options' => 'lbs,kg', + 'description' => 'The units for fuel', ]); /** diff --git a/app/Http/Resources/Pirep.php b/app/Http/Resources/Pirep.php index a5669b7c..5c746f40 100644 --- a/app/Http/Resources/Pirep.php +++ b/app/Http/Resources/Pirep.php @@ -3,6 +3,7 @@ namespace App\Http\Resources; use App\Support\Units\Distance; +use App\Support\Units\Fuel; use Illuminate\Http\Resources\Json\Resource; class Pirep extends Resource @@ -21,6 +22,10 @@ class Pirep extends Resource $pirep['distance'] = $this->distance->toObject(); } + if ($this->fuel_used instanceof Fuel) { + $pirep['fuel_used'] = $this->fuel_used->toObject(); + } + if ($this->planned_distance instanceof Distance) { $pirep['planned_distance'] = $this->planned_distance->toObject(); } diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 3f04165a..627bdab3 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -7,6 +7,7 @@ use App\Models\Enums\PirepState; use App\Models\Traits\HashId; use App\Support\Units\Distance; +use App\Support\Units\Fuel; use App\Support\Units\Time; use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonStringUnitName; @@ -137,6 +138,26 @@ class Pirep extends BaseModel } } + /** + * Return a new Fuel unit so conversions can be made + * @return int|Fuel + */ + public function getFuelUsedAttribute() + { + if (!array_key_exists('fuel_used', $this->attributes)) { + return null; + } + + try { + $fuel_used = (float) $this->attributes['fuel_used']; + return new Fuel($fuel_used, config('phpvms.internal_units.fuel')); + } catch (NonNumericValue $e) { + return 0; + } catch (NonStringUnitName $e) { + return 0; + } + } + /** * Return the planned_distance in a converter class * @return int|Distance @@ -157,6 +178,21 @@ class Pirep extends BaseModel } } + /** + * Set the amount of fuel used + * @param $value + */ + public function setFuelUsedAttribute($value) + { + if ($value instanceof Fuel) { + $this->attributes['fuel_used'] = $value->toUnit( + config('phpvms.internal_units.fuel') + ); + } else { + $this->attributes['fuel_used'] = $value; + } + } + /** * Set the distance unit, convert to our internal default unit * @param $value diff --git a/app/Support/Units/Distance.php b/app/Support/Units/Distance.php index 4c0dd83e..5cf390e0 100644 --- a/app/Support/Units/Distance.php +++ b/app/Support/Units/Distance.php @@ -19,6 +19,15 @@ class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arr return (string) round($value, 2); } + /** + * Return value in native unit as integer + * @return array + */ + public function toInt() + { + return $this->toArray(); + } + /** * For the HTTP Resource call */ diff --git a/app/Support/Units/Fuel.php b/app/Support/Units/Fuel.php new file mode 100644 index 00000000..28d0f541 --- /dev/null +++ b/app/Support/Units/Fuel.php @@ -0,0 +1,52 @@ +toUnit($unit); + return (string) round($value, 2); + } + + /** + * Return value in native unit as integer + * @return array + */ + public function toInt() + { + return $this->toArray(); + } + + /** + * For the HTTP Resource call + */ + public function toObject() + { + return [ + 'kg' => round($this->toUnit('kg'), 2), + 'lbs' => round($this->toUnit('lbs'), 2), + ]; + } + + /** + * Get the instance as an array. + * @return array + */ + public function toArray() + { + return round($this->toUnit( + config('phpvms.internal_units.fuel') + ), 2); + } +} diff --git a/app/Support/Units/Mass.php b/app/Support/Units/Mass.php index f76b9a7f..26a5387d 100644 --- a/app/Support/Units/Mass.php +++ b/app/Support/Units/Mass.php @@ -9,11 +9,6 @@ use Illuminate\Contracts\Support\Arrayable; */ class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable { - /** - * The unit this is stored as - */ - public const STORAGE_UNIT = 'lbs'; - /** * @return string */ diff --git a/app/Support/Units/Velocity.php b/app/Support/Units/Velocity.php index 0265967f..7112493e 100644 --- a/app/Support/Units/Velocity.php +++ b/app/Support/Units/Velocity.php @@ -19,6 +19,15 @@ class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity implements A return (string) round($value, 2); } + /** + * Return value in native unit as integer + * @return array + */ + public function toInt() + { + return $this->toArray(); + } + /** * For the HTTP Resource call */ diff --git a/config/phpvms.php b/config/phpvms.php index 00e3b156..85125210 100644 --- a/config/phpvms.php +++ b/config/phpvms.php @@ -55,6 +55,7 @@ return [ 'internal_units' => [ 'altitude' => 'feet', 'distance' => 'nmi', + 'fuel' => 'lbs', 'mass' => 'lbs', 'velocity' => 'knots', 'volume' => 'gallons', diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index a365ccaa..756e9b37 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -116,6 +116,32 @@ class PIREPTest extends TestCase $this->assertEquals($route, $saved_route); } + /** + * Make sure the unit conversions look to be proper + */ + public function testUnitFields() + { + $pirep = $this->createPirep(); + $pirep->save(); + + $uri = '/api/pireps/'.$pirep->id; + + $response = $this->get($uri); + $body = $response->json('data'); + + // Check that it has the fuel units + $this->assertHasKeys($body['fuel_used'], ['lbs', 'kg']); + $this->assertEquals($pirep->fuel_used->toInt(), $body['fuel_used']['lbs']); + + // Check that it has the distance units + $this->assertHasKeys($body['distance'], ['km', 'nmi', 'mi']); + $this->assertEquals($pirep->distance->toInt(), $body['distance']['nmi']); + + // Check the planned_distance field + $this->assertHasKeys($body['planned_distance'], ['km', 'nmi', 'mi']); + $this->assertEquals($pirep->planned_distance->toInt(), $body['planned_distance']['nmi']); + } + /** * */