base conversion classes for units #189

This commit is contained in:
Nabeel Shahzad
2018-02-10 21:16:32 -06:00
parent 176855b680
commit a8e06c6cc6
14 changed files with 349 additions and 37 deletions

View File

@@ -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
*/

View File

@@ -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