2017-12-13 01:49:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Contracts\Resource;
|
|
|
|
use App\Http\Resources\SimBrief as SimbriefResource;
|
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
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
/**
|
|
|
|
* @mixin \App\Models\Pirep
|
|
|
|
*/
|
|
|
|
class Pirep extends Resource
|
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;
|
2020-03-30 01:33:14 +08:00
|
|
|
$res['status_text'] = PirepStatus::label($this->status);
|
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['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
|
|
|
|
2020-04-01 05:26:55 +08:00
|
|
|
$res['position'] = Acars::make($this->whenLoaded('position'));
|
|
|
|
$res['comments'] = PirepComment::make($this->whenLoaded('comments'));
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['user'] = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'id' => $this->user->id,
|
|
|
|
'name' => $this->user->name,
|
2020-03-30 01:33:14 +08:00
|
|
|
'avatar' => $this->user->resolveAvatarUrl(),
|
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
|
|
|
|
2020-03-30 01:33:14 +08:00
|
|
|
$res['flight'] = Flight::make($this->whenLoaded('flight'));
|
|
|
|
|
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
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
// Simbrief info
|
|
|
|
$res['simbrief'] = new SimbriefResource($this->whenLoaded('simbrief'));
|
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
return $res;
|
2017-12-13 01:49:35 +08:00
|
|
|
}
|
|
|
|
}
|