2018-03-21 08:17:11 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
use App\Models\Airline;
|
2020-09-11 21:18:27 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2018-03-23 06:17:37 +08:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Common functionality used across all of the importers
|
|
|
|
*/
|
|
|
|
class ImportExport
|
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
public $assetType;
|
2018-03-23 06:17:37 +08:00
|
|
|
public $status = [
|
|
|
|
'success' => [],
|
2018-08-27 00:40:04 +08:00
|
|
|
'errors' => [],
|
2018-03-23 06:17:37 +08:00
|
|
|
];
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold the columns for the particular table
|
|
|
|
*/
|
|
|
|
public static $columns = [];
|
|
|
|
|
2018-04-23 21:46:28 +08:00
|
|
|
/**
|
|
|
|
* @param mixed $row
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-23 21:46:28 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function export($row): array
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('export not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $row
|
|
|
|
* @param mixed $index
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-23 21:46:28 +08:00
|
|
|
* @throws \RuntimeException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-04-23 21:46:28 +08:00
|
|
|
*/
|
|
|
|
public function import(array $row, $index): bool
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('import not implemented');
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
2018-03-23 01:43:58 +08:00
|
|
|
* Get the airline from the ICAO. Create it if it doesn't exist
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param $code
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-09-11 21:18:27 +08:00
|
|
|
* @return Airline
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
2020-09-11 21:18:27 +08:00
|
|
|
public function getAirline($code): Airline
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
$airline = Airline::firstOrCreate([
|
|
|
|
'icao' => $code,
|
|
|
|
], ['name' => $code]);
|
|
|
|
|
|
|
|
return $airline;
|
2018-03-21 08:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return static::$columns;
|
|
|
|
}
|
|
|
|
|
2018-03-23 06:17:37 +08:00
|
|
|
/**
|
|
|
|
* Do a basic check that the number of columns match
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param $row
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkColumns($row): bool
|
|
|
|
{
|
|
|
|
return \count($row) === \count($this->getColumns());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bubble up an error to the interface that we need to stop
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param $error
|
|
|
|
* @param $e
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
protected function throwError($error, \Exception $e = null): void
|
|
|
|
{
|
|
|
|
Log::error($error);
|
|
|
|
if ($e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = Validator::make([], []);
|
|
|
|
$validator->errors()->add('csv_file', $error);
|
2018-08-27 00:40:04 +08:00
|
|
|
|
2018-03-23 06:17:37 +08:00
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the log messages for this importer
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param $msg
|
|
|
|
*/
|
|
|
|
public function log($msg): void
|
|
|
|
{
|
|
|
|
$this->status['success'][] = $msg;
|
|
|
|
Log::info($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the error log for this import
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param $msg
|
|
|
|
*/
|
|
|
|
public function errorLog($msg): void
|
|
|
|
{
|
|
|
|
$this->status['errors'][] = $msg;
|
|
|
|
Log::error($msg);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* Set a key-value pair to an array
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param $kvp_str
|
|
|
|
* @param array $arr
|
|
|
|
*/
|
|
|
|
protected function kvpToArray($kvp_str, array &$arr)
|
|
|
|
{
|
|
|
|
$item = explode('=', $kvp_str);
|
2018-08-27 00:40:04 +08:00
|
|
|
if (\count($item) === 1) { // just a list?
|
2018-03-21 08:17:11 +08:00
|
|
|
$arr[] = trim($item[0]);
|
2018-08-27 00:40:04 +08:00
|
|
|
} else { // actually a key-value pair
|
2018-03-21 08:17:11 +08:00
|
|
|
$k = trim($item[0]);
|
|
|
|
$v = trim($item[1]);
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a multi column values field. E.g:
|
|
|
|
* Y?price=200&cost=100; F?price=1200
|
|
|
|
* or
|
|
|
|
* gate=B32;cost index=100
|
|
|
|
*
|
|
|
|
* Converted into a multi-dimensional array
|
|
|
|
*
|
|
|
|
* @param $field
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public function parseMultiColumnValues($field)
|
|
|
|
{
|
|
|
|
$ret = [];
|
|
|
|
$split_values = explode(';', $field);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// No multiple values in here, just a straight value
|
2018-03-21 08:17:11 +08:00
|
|
|
if (\count($split_values) === 1) {
|
2018-08-27 00:40:04 +08:00
|
|
|
if (trim($split_values[0]) === '') {
|
2018-03-31 07:53:43 +08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
return [$split_values[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($split_values as $value) {
|
2021-07-23 03:56:03 +08:00
|
|
|
$value = trim($value);
|
|
|
|
if ($value === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// This isn't in the query string format, so it's
|
|
|
|
// just a straight key-value pair set
|
2018-03-21 08:17:11 +08:00
|
|
|
if (strpos($value, '?') === false) {
|
|
|
|
$this->kvpToArray($value, $ret);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// This contains the query string, which turns it
|
|
|
|
// into the multi-level array
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
$query_str = explode('?', $value);
|
|
|
|
$parent = trim($query_str[0]);
|
|
|
|
|
|
|
|
$children = [];
|
|
|
|
$kvp = explode('&', trim($query_str[1]));
|
|
|
|
foreach ($kvp as $items) {
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!$items) {
|
2018-03-22 08:12:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->kvpToArray($items, $children);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret[$parent] = $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2018-03-22 06:07:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $obj
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-22 06:07:30 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function objectToMultiString($obj)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!\is_array($obj)) {
|
2018-03-22 06:07:30 +08:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret_list = [];
|
|
|
|
foreach ($obj as $key => $val) {
|
2018-08-27 00:40:04 +08:00
|
|
|
if (is_numeric($key) && !\is_array($val)) {
|
2018-03-22 06:07:30 +08:00
|
|
|
$ret_list[] = $val;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = trim($key);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!\is_array($val)) {
|
2018-03-22 06:07:30 +08:00
|
|
|
$val = trim($val);
|
|
|
|
$ret_list[] = "{$key}={$val}";
|
|
|
|
} else {
|
|
|
|
$q = [];
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($val as $subkey => $subval) {
|
|
|
|
if (is_numeric($subkey)) {
|
2018-03-22 06:07:30 +08:00
|
|
|
$q[] = $subval;
|
|
|
|
} else {
|
|
|
|
$q[] = "{$subkey}={$subval}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$q = implode('&', $q);
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!empty($q)) {
|
2018-03-22 06:07:30 +08:00
|
|
|
$ret_list[] = "{$key}?{$q}";
|
|
|
|
} else {
|
|
|
|
$ret_list[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(';', $ret_list);
|
|
|
|
}
|
2018-03-21 08:17:11 +08:00
|
|
|
}
|