2017-12-12 21:25:11 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
|
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
|
|
|
|
*/
|
2019-07-16 03:14:40 +08:00
|
|
|
class Flight extends Response
|
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 */
|
|
|
|
$field_values = $this->field_values;
|
|
|
|
if (empty($field_values) || $field_values->count() === 0) {
|
|
|
|
return new stdClass();
|
|
|
|
}
|
|
|
|
|
2018-03-23 11:48:54 +08:00
|
|
|
$fields = [];
|
2020-03-05 07:14:03 +08:00
|
|
|
foreach ($field_values as $field) {
|
2018-03-23 11:48:54 +08:00
|
|
|
$fields[$field->name] = $field->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$res['subfleets'] = Subfleet::collection($this->subfleets);
|
|
|
|
$res['fields'] = $this->setFields();
|
2018-02-05 02:39:51 +08:00
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
return $res;
|
2017-12-12 21:25:11 +08:00
|
|
|
}
|
|
|
|
}
|