phpvms/app/Services/ImportExport/FlightExporter.php

155 lines
3.6 KiB
PHP
Raw Normal View History

2018-03-22 06:07:30 +08:00
<?php
namespace App\Services\ImportExport;
2018-03-22 06:07:30 +08:00
use App\Interfaces\ImportExport;
use App\Models\Enums\Days;
2018-03-22 06:07:30 +08:00
use App\Models\Flight;
/**
* The flight importer can be imported or export. Operates on rows
*
* @package App\Services\Import
*/
class FlightExporter extends ImportExport
{
public $assetType = 'flight';
2018-03-22 06:07:30 +08:00
/**
* Set the current columns and other setup
*/
public function __construct()
{
self::$columns = array_keys(FlightImporter::$columns);
2018-03-22 06:07:30 +08:00
}
/**
* Import a flight, parse out the different rows
* @param Flight $flight
* @return array
*/
2018-04-23 21:46:28 +08:00
public function export($flight): array
2018-03-22 06:07:30 +08:00
{
$ret = [];
foreach(self::$columns as $column) {
$ret[$column] = $flight->{$column};
}
# Modify special fields
$ret['airline'] = $ret['airline']->icao;
$ret['distance'] = $ret['distance'][config('phpvms.internal_units.distance')];
2018-03-22 06:07:30 +08:00
$ret['dpt_airport'] = $flight->dpt_airport_id;
$ret['arr_airport'] = $flight->arr_airport_id;
if($flight->alt_airport) {
$ret['alt_airport'] = $flight->alt_airport_id;
}
$ret['days'] = $this->getDays($flight);
2018-03-23 06:17:37 +08:00
2018-03-22 06:07:30 +08:00
$ret['fares'] = $this->getFares($flight);
$ret['fields'] = $this->getFields($flight);
$ret['subfleets'] = $this->getSubfleets($flight);
return $ret;
}
/**
* Return the days string
* @param Flight $flight
* @return string
*/
protected function getDays(Flight &$flight)
{
$days_str = '';
if($flight->on_day(Days::MONDAY)) {
$days_str .= '1';
}
if ($flight->on_day(Days::TUESDAY)) {
$days_str .= '2';
}
if ($flight->on_day(Days::WEDNESDAY)) {
$days_str .= '3';
}
if ($flight->on_day(Days::THURSDAY)) {
$days_str .= '4';
}
if ($flight->on_day(Days::FRIDAY)) {
$days_str .= '5';
}
if ($flight->on_day(Days::SATURDAY)) {
$days_str .= '6';
}
if ($flight->on_day(Days::SUNDAY)) {
$days_str .= '7';
}
return $days_str;
}
2018-03-22 06:07:30 +08:00
/**
* Return any custom fares that have been made to this flight
* @param Flight $flight
* @return string
*/
protected function getFares(Flight &$flight): string
{
$fares = [];
foreach($flight->fares as $fare) {
$fare_export = [];
if($fare->pivot->price) {
$fare_export['price'] = $fare->pivot->price;
}
if ($fare->pivot->cost) {
$fare_export['cost'] = $fare->pivot->cost;
}
if ($fare->pivot->capacity) {
$fare_export['capacity'] = $fare->pivot->capacity;
}
$fares[$fare->code] = $fare_export;
}
return $this->objectToMultiString($fares);
}
/**
* Parse all of the subfields
* @param Flight $flight
* @return string
*/
protected function getFields(Flight &$flight): string
{
$ret = [];
foreach ($flight->field_values as $field) {
$ret[$field->name] = $field->value;
}
return $this->objectToMultiString($ret);
}
/**
* Create the list of subfleets that are associated here
* @param Flight $flight
* @return string
*/
protected function getSubfleets(Flight &$flight): string
{
$subfleets = [];
foreach($flight->subfleets as $subfleet) {
$subfleets[] = $subfleet->type;
}
return $this->objectToMultiString($subfleets);
}
}