2017-12-12 21:25:11 +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;
|
2019-07-23 20:41:20 +08:00
|
|
|
use App\Support\Units\Distance;
|
2020-03-05 07:14:03 +08:00
|
|
|
use stdClass;
|
2019-07-23 20:41:20 +08:00
|
|
|
|
2020-03-06 09:19:12 +08:00
|
|
|
/**
|
|
|
|
* @mixin \App\Models\Flight
|
|
|
|
*/
|
2020-03-23 21:31:35 +08:00
|
|
|
class Flight extends Resource
|
2017-12-12 21:25:11 +08:00
|
|
|
{
|
2018-03-23 11:48:54 +08:00
|
|
|
/**
|
|
|
|
* Set the fields on the flight object
|
|
|
|
*/
|
|
|
|
private function setFields()
|
|
|
|
{
|
2020-03-05 07:14:03 +08:00
|
|
|
/** @var \Illuminate\Support\Collection $field_values */
|
2020-10-30 00:38:40 +08:00
|
|
|
$return_values = new stdClass();
|
2020-03-05 07:14:03 +08:00
|
|
|
$field_values = $this->field_values;
|
|
|
|
if (empty($field_values) || $field_values->count() === 0) {
|
2020-10-30 00:38:40 +08:00
|
|
|
return $return_values;
|
2020-03-05 07:14:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($field_values as $field) {
|
2020-10-30 00:38:40 +08:00
|
|
|
$return_values->{$field->name} = $field->value;
|
2018-03-23 11:48:54 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 00:38:40 +08:00
|
|
|
return $return_values;
|
2018-03-23 11:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-07-23 20:41:20 +08:00
|
|
|
/**
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-12-12 21:25:11 +08:00
|
|
|
public function toArray($request)
|
|
|
|
{
|
2019-07-16 03:14:40 +08:00
|
|
|
$res = parent::toArray($request);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['ident'] = $this->ident;
|
2018-05-14 05:59:35 +08:00
|
|
|
|
2020-03-06 09:19:12 +08:00
|
|
|
if (empty($res['load_factor'])) {
|
|
|
|
$res['load_factor'] = setting('flights.default_load_factor');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($res['load_factor_variance'])) {
|
|
|
|
$res['load_factor_variance'] = setting('flights.load_factor_variance');
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:05:09 +08:00
|
|
|
$distance = new Distance($res['distance'], config('phpvms.internal_units.distance'));
|
|
|
|
$res['distance'] = $distance->getResponseUnits();
|
2018-02-11 11:16:32 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['airline'] = new Airline($this->airline);
|
2020-03-23 21:31:35 +08:00
|
|
|
$res['subfleets'] = Subfleet::collection($this->whenLoaded('subfleets'));
|
2019-07-16 03:14:40 +08:00
|
|
|
$res['fields'] = $this->setFields();
|
2018-02-05 02:39:51 +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-12 21:25:11 +08:00
|
|
|
}
|
|
|
|
}
|