phpvms/app/Http/Resources/Flight.php

55 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Resources;
use App\Support\Units\Distance;
use stdClass;
class Flight extends Response
{
2018-03-23 11:48:54 +08:00
/**
* Set the fields on the flight object
2018-08-27 00:40:04 +08:00
*
* @mixin \App\Models\Flight
2018-03-23 11:48:54 +08:00
*/
private function setFields()
{
/** @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 = [];
foreach ($field_values as $field) {
2018-03-23 11:48:54 +08:00
$fields[$field->name] = $field->value;
}
return $fields;
}
/**
* @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;
$distance = new Distance($res['distance'], config('phpvms.internal_units.distance'));
$res['distance'] = $distance->getResponseUnits();
2018-02-11 11:16:32 +08:00
$res['airline'] = new Airline($this->airline);
$res['subfleets'] = Subfleet::collection($this->subfleets);
$res['fields'] = $this->setFields();
return $res;
}
}