phpvms/app/Http/Resources/Pirep.php

91 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Resources;
use App\Contracts\Resource;
use App\Http\Resources\SimBrief as SimbriefResource;
2018-05-03 04:14:18 +08:00
use App\Models\Enums\PirepStatus;
use App\Support\Units\Distance;
use App\Support\Units\Fuel;
/**
* @mixin \App\Models\Pirep
*/
class Pirep extends Resource
{
/**
* Transform the resource into an array.
*
2018-08-27 00:40:04 +08:00
* @param \Illuminate\Http\Request $request
*
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
*
* @return array
*/
public function toArray($request)
{
$res = parent::toArray($request);
$res['ident'] = $this->ident;
$res['status_text'] = PirepStatus::label($this->status);
// Set these to the response units
if (!array_key_exists('distance', $res)) {
$res['distance'] = 0;
}
$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;
}
$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)) {
$res['planned_distance'] = 0;
}
$planned_dist = new Distance($res['planned_distance'], config('phpvms.internal_units.distance'));
$res['planned_distance'] = $planned_dist->getResponseUnits();
/*
* Relationship fields
*/
2018-08-27 00:40:04 +08:00
if ($this->block_on_time) {
$res['block_on_time'] = $this->block_on_time->toIso8601ZuluString();
2018-08-27 00:40:04 +08:00
}
2018-08-27 00:40:04 +08:00
if ($this->block_off_time) {
$res['block_off_time'] = $this->block_off_time->toIso8601ZuluString();
2018-08-27 00:40:04 +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
$res['position'] = new Acars($this->position);
$res['comments'] = PirepComment::collection($this->comments);
$res['user'] = [
'id' => $this->user->id,
'name' => $this->user->name,
'avatar' => $this->user->resolveAvatarUrl(),
'home_airport_id' => $this->user->home_airport_id,
'curr_airport_id' => $this->user->curr_airport_id,
];
$res['flight'] = Flight::make($this->whenLoaded('flight'));
2018-08-27 00:40:04 +08:00
// format to kvp
$res['fields'] = new PirepFieldCollection($this->fields);
// Simbrief info
$res['simbrief'] = new SimbriefResource($this->whenLoaded('simbrief'));
return $res;
}
}