phpvms/app/Http/Resources/Flight.php
2018-08-26 16:40:04 +00:00

43 lines
975 B
PHP

<?php
namespace App\Http\Resources;
use App\Support\Units\Distance;
use Illuminate\Http\Resources\Json\Resource;
class Flight extends Resource
{
/**
* Set the fields on the flight object
*
* @return array
*/
private function setFields()
{
$fields = [];
foreach ($this->field_values as $field) {
$fields[$field->name] = $field->value;
}
return $fields;
}
public function toArray($request)
{
$flight = parent::toArray($request);
$flight['ident'] = $this->ident;
// Return multiple measures so the client can pick what they want
if ($this->distance instanceof Distance) {
$flight['distance'] = $this->distance->units;
}
$flight['airline'] = new Airline($this->airline);
$flight['subfleets'] = Subfleet::collection($this->subfleets);
$flight['fields'] = $this->setFields();
return $flight;
}
}