Change volume units to be in lbs/kg closes #193
This commit is contained in:
parent
463de8d7e7
commit
653711fa96
@ -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',
|
||||
]);
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
*/
|
||||
|
52
app/Support/Units/Fuel.php
Normal file
52
app/Support/Units/Fuel.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -55,6 +55,7 @@ return [
|
||||
'internal_units' => [
|
||||
'altitude' => 'feet',
|
||||
'distance' => 'nmi',
|
||||
'fuel' => 'lbs',
|
||||
'mass' => 'lbs',
|
||||
'velocity' => 'knots',
|
||||
'volume' => 'gallons',
|
||||
|
@ -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']);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user