phpvms/app/Http/Resources/Flight.php

43 lines
975 B
PHP
Raw Normal View History

<?php
namespace App\Http\Resources;
2018-02-11 11:16:32 +08:00
use App\Support\Units\Distance;
use Illuminate\Http\Resources\Json\Resource;
class Flight extends Resource
{
2018-03-23 11:48:54 +08:00
/**
* Set the fields on the flight object
2018-08-27 00:40:04 +08:00
*
2018-03-23 11:48:54 +08:00
* @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;
2018-02-11 11:16:32 +08:00
// Return multiple measures so the client can pick what they want
if ($this->distance instanceof Distance) {
$flight['distance'] = $this->distance->units;
2018-02-11 11:16:32 +08:00
}
$flight['airline'] = new Airline($this->airline);
$flight['subfleets'] = Subfleet::collection($this->subfleets);
2018-03-23 11:48:54 +08:00
$flight['fields'] = $this->setFields();
return $flight;
}
}