2017-12-13 01:49:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
use App\Models\Enums\PirepStatus;
|
2019-07-23 20:41:20 +08:00
|
|
|
use App\Support\Units\Distance;
|
|
|
|
use App\Support\Units\Fuel;
|
2017-12-13 01:49:35 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
class Pirep extends Response
|
2017-12-13 01:49:35 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Transform the resource into an array.
|
|
|
|
*
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2019-07-23 20:41:20 +08:00
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
|
|
|
*
|
2017-12-13 01:49:35 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray($request)
|
|
|
|
{
|
2019-07-16 03:14:40 +08:00
|
|
|
$res = parent::toArray($request);
|
|
|
|
$res['ident'] = $this->ident;
|
2018-01-05 11:21:51 +08:00
|
|
|
|
2019-07-23 20:41:20 +08:00
|
|
|
// Set these to the response units
|
2019-09-20 21:05:09 +08:00
|
|
|
if (!array_key_exists('distance', $res)) {
|
|
|
|
$res['distance'] = 0;
|
2019-07-23 20:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 21:05:09 +08:00
|
|
|
$distance = new Distance($res['distance'], config('phpvms.internal_units.distance'));
|
|
|
|
$res['distance'] = $distance->getResponseUnits();
|
|
|
|
|
|
|
|
if (!array_key_exists('fuel_used', $res)) {
|
|
|
|
$res['fuel_used'] = 0;
|
2019-07-23 20:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 21:05:09 +08:00
|
|
|
$fuel_used = new Fuel($res['fuel_used'], config('phpvms.internal_units.fuel'));
|
|
|
|
$res['fuel_used'] = $fuel_used->getResponseUnits();
|
|
|
|
|
2019-09-20 21:11:34 +08:00
|
|
|
if (!array_key_exists('planned_distance', $res)) {
|
2019-09-20 21:05:09 +08:00
|
|
|
$res['planned_distance'] = 0;
|
2019-07-23 20:41:20 +08:00
|
|
|
}
|
2018-02-11 12:04:30 +08:00
|
|
|
|
2019-09-20 21:05:09 +08:00
|
|
|
$planned_dist = new Distance($res['planned_distance'], config('phpvms.internal_units.distance'));
|
|
|
|
$res['planned_distance'] = $planned_dist->getResponseUnits();
|
|
|
|
|
2018-02-12 10:38:56 +08:00
|
|
|
/*
|
|
|
|
* Relationship fields
|
|
|
|
*/
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($this->block_on_time) {
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['block_on_time'] = $this->block_on_time->toIso8601ZuluString();
|
2018-08-27 00:40:04 +08:00
|
|
|
}
|
2018-05-02 11:52:31 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($this->block_off_time) {
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['block_off_time'] = $this->block_off_time->toIso8601ZuluString();
|
2018-08-27 00:40:04 +08:00
|
|
|
}
|
2018-05-02 11:52:31 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['status_text'] = PirepStatus::label($this->status);
|
2018-05-03 04:14:18 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['airline'] = new Airline($this->airline);
|
|
|
|
$res['dpt_airport'] = new Airport($this->dpt_airport);
|
|
|
|
$res['arr_airport'] = new Airport($this->arr_airport);
|
2018-05-03 04:14:18 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['position'] = new Acars($this->position);
|
|
|
|
$res['comments'] = PirepComment::collection($this->comments);
|
|
|
|
$res['user'] = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'id' => $this->user->id,
|
|
|
|
'name' => $this->user->name,
|
2018-01-29 01:12:13 +08:00
|
|
|
'home_airport_id' => $this->user->home_airport_id,
|
|
|
|
'curr_airport_id' => $this->user->curr_airport_id,
|
|
|
|
];
|
2018-01-05 11:21:51 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// format to kvp
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['fields'] = new PirepFieldCollection($this->fields);
|
2018-02-07 03:59:40 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
return $res;
|
2017-12-13 01:49:35 +08:00
|
|
|
}
|
|
|
|
}
|