phpvms/app/Services/ImportExport/SubfleetExporter.php

109 lines
2.4 KiB
PHP
Raw Normal View History

2018-03-23 06:59:10 +08:00
<?php
namespace App\Services\ImportExport;
use App\Interfaces\ImportExport;
use App\Models\Flight;
use App\Models\Subfleet;
/**
* The flight importer can be imported or export. Operates on rows
*/
class SubfleetExporter extends ImportExport
{
public $assetType = 'subfleet';
/**
* Set the current columns and other setup
*/
public function __construct()
{
self::$columns = array_keys(SubfleetImporter::$columns);
2018-03-23 06:59:10 +08:00
}
/**
* Import a flight, parse out the different rows
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @param Subfleet $subfleet
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @return array
*/
2018-04-23 21:46:28 +08:00
public function export($subfleet): array
2018-03-23 06:59:10 +08:00
{
$ret = [];
2018-08-27 00:40:04 +08:00
foreach (self::$columns as $column) {
2018-03-23 06:59:10 +08:00
$ret[$column] = $subfleet->{$column};
}
2018-08-27 00:40:04 +08:00
// Modify special fields
$ret['airline'] = $subfleet->airline->icao;
2018-03-23 06:59:10 +08:00
$ret['fares'] = $this->getFares($subfleet);
return $ret;
}
/**
* Return any custom fares that have been made to this flight
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @param Subfleet $subfleet
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @return string
*/
protected function getFares(Subfleet &$subfleet): string
{
$fares = [];
2018-08-27 00:40:04 +08:00
foreach ($subfleet->fares as $fare) {
2018-03-23 06:59:10 +08:00
$fare_export = [];
2018-08-27 00:40:04 +08:00
if ($fare->pivot->price) {
2018-03-23 06:59:10 +08:00
$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
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @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
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @return string
*/
protected function getSubfleets(Flight &$flight): string
{
$subfleets = [];
2018-08-27 00:40:04 +08:00
foreach ($flight->subfleets as $subfleet) {
2018-03-23 06:59:10 +08:00
$subfleets[] = $subfleet->type;
}
return $this->objectToMultiString($subfleets);
}
}