Cleanup column types and assign Time class to fields #189

This commit is contained in:
Nabeel Shahzad 2018-02-11 20:38:56 -06:00
parent fd4407a798
commit 61af5fe226
8 changed files with 89 additions and 7 deletions

View File

@ -28,7 +28,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline
'route' => $faker->randomElement(['', $faker->text(5)]),
'dpt_time' => $faker->time(),
'arr_time' => $faker->time(),
'flight_time' => $faker->randomFloat(2),
'flight_time' => $faker->numberBetween(60, 360),
'has_bid' => false,
'active' => true,
'created_at' => $faker->dateTimeBetween('-1 week', 'now'),

View File

@ -37,8 +37,8 @@ $factory->define(App\Models\Pirep::class, function (Faker $faker) {
'level' => $faker->numberBetween(20, 400),
'distance' => $faker->randomFloat(2),
'planned_distance' => $faker->randomFloat(2),
'flight_time' => $faker->randomFloat(2),
'planned_flight_time' => $faker->randomFloat(2),
'flight_time' => $faker->numberBetween(60, 360),
'planned_flight_time' => $faker->numberBetween(60, 360),
'zfw' => $faker->randomFloat(2),
'block_fuel' => $faker->randomFloat(2),
'fuel_used' => $faker->randomFloat(2),

View File

@ -28,7 +28,7 @@ class CreateFlightTables extends Migration
$table->string('arr_time', 10)->nullable();
$table->unsignedInteger('level')->nullable()->default(0);
$table->unsignedDecimal('distance', 19)->nullable()->default(0.0);
$table->unsignedDecimal('flight_time', 19)->nullable();
$table->unsignedInteger('flight_time')->nullable();
$table->tinyInteger('flight_type')->default(FlightType::PASSENGER);
$table->text('route')->nullable();
$table->text('notes')->nullable();

View File

@ -31,8 +31,8 @@ class CreatePirepTables extends Migration
$table->unsignedInteger('level')->nullable();
$table->unsignedDecimal('distance')->nullable();
$table->unsignedDecimal('planned_distance')->nullable();
$table->unsignedDecimal('flight_time', 19)->nullable();
$table->unsignedDecimal('planned_flight_time', 19)->nullable();
$table->unsignedInteger('flight_time')->nullable();
$table->unsignedInteger('planned_flight_time')->nullable();
$table->unsignedDecimal('zfw', 19)->nullable();
$table->unsignedDecimal('block_fuel', 19)->nullable();
$table->unsignedDecimal('fuel_used', 19)->nullable();

View File

@ -25,6 +25,10 @@ class Pirep extends Resource
$pirep['planned_distance'] = $this->planned_distance->toObject();
}
/*
* Relationship fields
*/
$pirep['airline'] = new Airline($this->airline);
$pirep['dpt_airport'] = new Airport($this->dpt_airport);
$pirep['arr_airport'] = new Airport($this->arr_airport);

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Support\Units\Distance;
use App\Support\Units\Time;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
@ -28,6 +29,7 @@ class Flight extends BaseModel
'arr_time',
'level',
'distance',
'flight_time',
'flight_type',
'route',
'notes',
@ -39,6 +41,7 @@ class Flight extends BaseModel
'flight_number' => 'integer',
'level' => 'integer',
'distance' => 'float',
'flight_time' => 'integer',
'flight_type' => 'integer',
'has_bid' => 'boolean',
'active' => 'boolean',
@ -108,6 +111,30 @@ class Flight extends BaseModel
}
}
/**
* @return Time
*/
public function getFlightTimeAttribute()
{
if (!array_key_exists('flight_time', $this->attributes)) {
return null;
}
return new Time($this->attributes['flight_time']);
}
/**
* @param $value
*/
public function setFlightTimeAttribute($value)
{
if ($value instanceof Time) {
$this->attributes['flight_time'] = $value->getMinutes();
} else {
$this->attributes['flight_time'] = $value;
}
}
/**
* Relationship
*/

View File

@ -7,6 +7,7 @@ use App\Models\Enums\PirepState;
use App\Models\Traits\HashId;
use App\Support\Units\Distance;
use App\Support\Units\Time;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
@ -137,6 +138,30 @@ class Pirep extends BaseModel
}
}
/**
* @return Time
*/
public function getFlightTimeAttribute()
{
if (!array_key_exists('flight_time', $this->attributes)) {
return null;
}
return new Time($this->attributes['flight_time']);
}
/**
* @param $value
*/
public function setFlightTimeAttribute($value)
{
if($value instanceof Time) {
$this->attributes['flight_time'] = $value->getMinutes();
} else {
$this->attributes['flight_time'] = $value;
}
}
/**
* Return the planned_distance in a converter class
* @return int|Distance
@ -172,6 +197,30 @@ class Pirep extends BaseModel
}
}
/**
* @return Time
*/
public function getPlannedFlightTimeAttribute()
{
if (!array_key_exists('planned_flight_time', $this->attributes)) {
return null;
}
return new Time($this->attributes['planned_flight_time']);
}
/**
* @param $value
*/
public function setPlannedFlightTimeAttribute($value)
{
if ($value instanceof Time) {
$this->attributes['planned_flight_time'] = $value->getMinutes();
} else {
$this->attributes['planned_flight_time'] = $value;
}
}
/**
* Do some cleanup on the route
* @param $route

View File

@ -20,8 +20,10 @@ class Time implements Arrayable
*/
public function __construct($minutes, $hours=null)
{
$minutes = (int) $minutes;
if(!empty($hours)) {
$this->hours = $hours;
$this->hours = (int) $hours;
} else {
$this->hours = floor($minutes / 60);
}