phpvms/app/Services/ImportExport/AirportImporter.php

61 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\ImportExport;
use App\Contracts\ImportExport;
use App\Models\Airport;
/**
* Import airports
*/
class AirportImporter extends ImportExport
{
public $assetType = 'airport';
/**
* All of the columns that are in the CSV import
* Should match the database fields, for the most part
*/
public static $columns = [
2018-07-13 11:21:39 +08:00
'icao' => 'required',
'iata' => 'required',
'name' => 'required',
'location' => 'nullable',
'country' => 'nullable',
'timezone' => 'nullable',
'hub' => 'nullable|boolean',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
'ground_handling_cost' => 'nullable|numeric',
'fuel_100ll_cost' => 'nullable|numeric',
'fuel_jeta_cost' => 'nullable|numeric',
'fuel_mogas_cost' => 'nullable|numeric',
];
/**
* Import a flight, parse out the different rows
2018-08-27 00:40:04 +08:00
*
* @param array $row
* @param int $index
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
public function import(array $row, $index): bool
{
$row['id'] = $row['icao'];
$row['hub'] = get_truth_state($row['hub']);
try {
$airport = Airport::updateOrCreate([
'id' => $row['icao'],
], $row);
2018-07-13 11:21:39 +08:00
} catch (\Exception $e) {
2018-03-23 06:17:37 +08:00
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
return false;
}
2018-03-23 06:17:37 +08:00
$this->log('Imported '.$row['icao']);
return true;
}
}