Change volume units to be in lbs/kg closes #193

This commit is contained in:
Nabeel Shahzad 2018-02-20 14:07:33 -06:00
parent 463de8d7e7
commit 653711fa96
9 changed files with 144 additions and 11 deletions

View File

@ -73,7 +73,7 @@ class CreateSettingsTable extends Migration
'group' => 'general', 'group' => 'general',
'value' => 'lbs', 'value' => 'lbs',
'type' => 'select', 'type' => 'select',
'options' => 'lbs, kg', 'options' => 'lbs,kg',
'description' => 'The weight unit', 'description' => 'The weight unit',
]); ]);
@ -95,13 +95,13 @@ class CreateSettingsTable extends Migration
'description' => 'The altitude units', 'description' => 'The altitude units',
]); ]);
$this->addSetting('general.liquid_unit', [ $this->addSetting('general.fuel_unit', [
'name' => 'Liquid Units', 'name' => 'Fuel Units',
'group' => 'general', 'group' => 'general',
'value' => 'gal', 'value' => 'lbs',
'type' => 'select', 'type' => 'select',
'options' => 'liters,gal', 'options' => 'lbs,kg',
'description' => 'The liquid units', 'description' => 'The units for fuel',
]); ]);
/** /**

View File

@ -3,6 +3,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Support\Units\Distance; use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use Illuminate\Http\Resources\Json\Resource; use Illuminate\Http\Resources\Json\Resource;
class Pirep extends Resource class Pirep extends Resource
@ -21,6 +22,10 @@ class Pirep extends Resource
$pirep['distance'] = $this->distance->toObject(); $pirep['distance'] = $this->distance->toObject();
} }
if ($this->fuel_used instanceof Fuel) {
$pirep['fuel_used'] = $this->fuel_used->toObject();
}
if ($this->planned_distance instanceof Distance) { if ($this->planned_distance instanceof Distance) {
$pirep['planned_distance'] = $this->planned_distance->toObject(); $pirep['planned_distance'] = $this->planned_distance->toObject();
} }

View File

@ -7,6 +7,7 @@ use App\Models\Enums\PirepState;
use App\Models\Traits\HashId; use App\Models\Traits\HashId;
use App\Support\Units\Distance; use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use App\Support\Units\Time; use App\Support\Units\Time;
use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName; 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 the planned_distance in a converter class
* @return int|Distance * @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 * Set the distance unit, convert to our internal default unit
* @param $value * @param $value

View File

@ -19,6 +19,15 @@ class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arr
return (string) round($value, 2); 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 * For the HTTP Resource call
*/ */

View File

@ -0,0 +1,52 @@
<?php
namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/**
* Class Mass
* @package App\Support\Units
*/
class Fuel extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable
{
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.fuel_unit');
$value = $this->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);
}
}

View File

@ -9,11 +9,6 @@ use Illuminate\Contracts\Support\Arrayable;
*/ */
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable
{ {
/**
* The unit this is stored as
*/
public const STORAGE_UNIT = 'lbs';
/** /**
* @return string * @return string
*/ */

View File

@ -19,6 +19,15 @@ class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity implements A
return (string) round($value, 2); 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 * For the HTTP Resource call
*/ */

View File

@ -55,6 +55,7 @@ return [
'internal_units' => [ 'internal_units' => [
'altitude' => 'feet', 'altitude' => 'feet',
'distance' => 'nmi', 'distance' => 'nmi',
'fuel' => 'lbs',
'mass' => 'lbs', 'mass' => 'lbs',
'velocity' => 'knots', 'velocity' => 'knots',
'volume' => 'gallons', 'volume' => 'gallons',

View File

@ -116,6 +116,32 @@ class PIREPTest extends TestCase
$this->assertEquals($route, $saved_route); $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']);
}
/** /**
* *
*/ */