base conversion classes for units #189
This commit is contained in:
@@ -24,6 +24,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline
|
||||
'alt_airport_id' => function () {
|
||||
return factory(App\Models\Airport::class)->create()->id;
|
||||
},
|
||||
'distance' => $faker->numberBetween(0, 3000),
|
||||
'route' => $faker->randomElement(['', $faker->text(5)]),
|
||||
'dpt_time' => $faker->time(),
|
||||
'arr_time' => $faker->time(),
|
||||
|
||||
@@ -62,16 +62,16 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.distance_unit', [
|
||||
'name' => 'Distance Units',
|
||||
'group' => 'general',
|
||||
'value' => 'nm',
|
||||
'value' => 'NM',
|
||||
'type' => 'select',
|
||||
'options' => 'km,mi,nm',
|
||||
'options' => 'km,mi,NM',
|
||||
'description' => 'The distance unit to show',
|
||||
]);
|
||||
|
||||
$this->addSetting('general.weight_unit', [
|
||||
'name' => 'Weight Units',
|
||||
'group' => 'general',
|
||||
'value' => 'kg',
|
||||
'value' => 'lbs',
|
||||
'type' => 'select',
|
||||
'options' => 'lbs, kg',
|
||||
'description' => 'The weight unit',
|
||||
@@ -80,9 +80,9 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.speed_unit', [
|
||||
'name' => 'Speed Units',
|
||||
'group' => 'general',
|
||||
'value' => 'Km/H',
|
||||
'value' => 'knot',
|
||||
'type' => 'select',
|
||||
'options' => 'Km/H,kts',
|
||||
'options' => 'km/h,knot',
|
||||
'description' => 'The speed unit',
|
||||
]);
|
||||
|
||||
@@ -98,9 +98,9 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.liquid_unit', [
|
||||
'name' => 'Liquid Units',
|
||||
'group' => 'general',
|
||||
'value' => 'lbs',
|
||||
'value' => 'gal',
|
||||
'type' => 'select',
|
||||
'options' => 'liters,gal,kg,lbs',
|
||||
'options' => 'liters,gal',
|
||||
'description' => 'The liquid units',
|
||||
]);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Support\Units\Distance;
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
|
||||
class Flight extends Resource
|
||||
@@ -10,7 +11,11 @@ class Flight extends Resource
|
||||
{
|
||||
$flight = parent::toArray($request);
|
||||
|
||||
$flight['field'] = true;
|
||||
// Return multiple measures so the client can pick what they want
|
||||
if($flight['distance'] instanceof Distance) {
|
||||
$flight['distance'] = $flight['distance']->toJson();
|
||||
}
|
||||
|
||||
$flight['airline'] = new Airline($this->airline);
|
||||
$flight['subfleets'] = Subfleet::collection($this->subfleets);
|
||||
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Support\Units\Distance;
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
|
||||
use App\Models\Traits\HashId;
|
||||
|
||||
class Flight extends BaseModel
|
||||
@@ -58,11 +62,46 @@ class Flight extends BaseModel
|
||||
$flight_id = $this->airline->code;
|
||||
$flight_id .= $this->flight_number;
|
||||
|
||||
# TODO: Add in code/leg if set
|
||||
if (filled($this->route_code)) {
|
||||
$flight_id .= '/C' . $this->route_code;
|
||||
}
|
||||
|
||||
if (filled($this->route_leg)) {
|
||||
$flight_id .= '/L' . $this->route_leg;
|
||||
}
|
||||
|
||||
return $flight_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new Length unit so conversions can be made
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float) $this->attributes['distance'];
|
||||
return new Distance($distance, Distance::STORAGE_UNIT);
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the distance unit, convert to our internal default unit
|
||||
* @param $value
|
||||
*/
|
||||
public function setDistanceAttribute($value)
|
||||
{
|
||||
if($value instanceof Distance) {
|
||||
$this->attributes['distance'] = $value->toUnit(Distance::STORAGE_UNIT);
|
||||
} else {
|
||||
$this->attributes['distance'] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,10 @@ namespace App\Models;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Traits\HashId;
|
||||
use App\Support\Units\Distance;
|
||||
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
|
||||
/**
|
||||
* Class Pirep
|
||||
@@ -99,6 +103,38 @@ class Pirep extends BaseModel
|
||||
return $flight_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new Length unit so conversions can be made
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float)$this->attributes['distance'];
|
||||
return new Distance($distance, 'mi');
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the planned_distance in a converter class
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getPlannedDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float) $this->attributes['planned_distance'];
|
||||
return new Distance($distance, 'mi');
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do some cleanup on the route
|
||||
* @param $route
|
||||
|
||||
36
app/Support/Units/Altitude.php
Normal file
36
app/Support/Units/Altitude.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'feet';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.altitude_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'ft' => round($this->toUnit('feet'), 2),
|
||||
'm' => round($this->toUnit('meters') / 1000, 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
37
app/Support/Units/Distance.php
Normal file
37
app/Support/Units/Distance.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'mi';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.distance_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'mi' => round($this->toUnit('miles'), 2),
|
||||
'nmi' => round($this->toUnit('nmi'), 2),
|
||||
'km' => round($this->toUnit('meters') / 1000, 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
31
app/Support/Units/Mass.php
Normal file
31
app/Support/Units/Mass.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Class Mass
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.weight_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string)round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'kg' => round($this->toUnit('kg'), 2),
|
||||
'lgs' => round($this->toUnit('lbs'), 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
36
app/Support/Units/Velocity.php
Normal file
36
app/Support/Units/Velocity.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Class Velocity
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'knot';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.speed_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'knot' => round($this->toUnit('knot'), 2),
|
||||
'km/h' => round($this->toUnit('km/h'), 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
36
app/Support/Units/Volume.php
Normal file
36
app/Support/Units/Volume.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'gal';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.liquid_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'gal' => round($this->toUnit('gal'), 2),
|
||||
'liters' => round($this->toUnit('liters'), 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user