phpvms/app/Models/Acars.php

167 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Interfaces\Model;
use App\Models\Traits\HashIdTrait;
use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
/**
* Class Acars
2018-08-27 00:40:04 +08:00
*
* @param string id
2018-08-27 00:40:04 +08:00
*
2018-05-12 01:08:55 +08:00
* @property string pirep_id
* @property int type
* @property string name
2018-08-27 00:40:04 +08:00
* @property float lat
* @property float lon
* @property float altitude
2018-05-12 01:08:55 +08:00
* @property int gs
* @property int heading
* @property int order
* @property int nav_type
*/
class Acars extends Model
{
use HashIdTrait;
public $table = 'acars';
2018-01-01 04:20:52 +08:00
public $incrementing = false;
public $fillable = [
'pirep_id',
'type',
'nav_type',
'order',
'name',
'status',
2017-12-26 08:22:46 +08:00
'log',
'lat',
'lon',
'distance',
2017-12-26 08:22:46 +08:00
'heading',
'altitude',
'vs',
'gs',
'transponder',
'autopilot',
'fuel_flow',
'sim_time',
'created_at',
'updated_at',
];
2017-12-26 08:10:24 +08:00
public $casts = [
'type' => 'integer',
'order' => 'integer',
'nav_type' => 'integer',
'lat' => 'float',
'lon' => 'float',
'distance' => 'integer',
'heading' => 'integer',
'altitude' => 'float',
'vs' => 'float',
'gs' => 'float',
'transponder' => 'integer',
'fuel' => 'float',
'fuel_flow' => 'float',
2017-12-26 08:10:24 +08:00
];
2018-05-12 01:08:55 +08:00
/*public static $sanitize = [
'sim_time' => 'carbon',
'created_at' => '',
];*/
2017-12-26 08:10:24 +08:00
2018-01-01 04:20:52 +08:00
public static $rules = [
'pirep_id' => 'required',
2018-01-01 04:20:52 +08:00
];
/**
* Return a new Length unit so conversions can be made
2018-08-27 00:40:04 +08:00
*
* @return int|Distance
*/
public function getDistanceAttribute()
{
if (!array_key_exists('distance', $this->attributes)) {
2018-08-27 00:40:04 +08:00
return;
}
try {
$distance = (float) $this->attributes['distance'];
if ($this->skip_mutator) {
return $distance;
}
return new Distance($distance, config('phpvms.internal_units.distance'));
} catch (NonNumericValue $e) {
return 0;
} catch (NonStringUnitName $e) {
return 0;
}
}
/**
* Set the distance unit, convert to our internal default unit
2018-08-27 00:40:04 +08:00
*
* @param $value
*/
public function setDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
config('phpvms.internal_units.distance'));
} else {
$this->attributes['distance'] = $value;
}
}
/**
* Return a new Fuel unit so conversions can be made
2018-08-27 00:40:04 +08:00
*
* @return int|Fuel
*/
public function getFuelAttribute()
{
if (!array_key_exists('fuel', $this->attributes)) {
2018-08-27 00:40:04 +08:00
return;
}
try {
$fuel = (float) $this->attributes['fuel'];
return new Fuel($fuel, config('phpvms.internal_units.fuel'));
} catch (NonNumericValue $e) {
return 0;
} catch (NonStringUnitName $e) {
return 0;
}
}
/**
* Set the amount of fuel
2018-08-27 00:40:04 +08:00
*
* @param $value
*/
public function setFuelAttribute($value)
{
if ($value instanceof Fuel) {
$this->attributes['fuel'] = $value->toUnit(
config('phpvms.internal_units.fuel')
);
} else {
$this->attributes['fuel'] = $value;
}
}
/**
* FKs
*/
public function pirep()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Pirep::class, 'pirep_id');
}
}