2018-02-22 11:38:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Repositories\FlightRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ImporterService
|
|
|
|
* @package App\Services
|
|
|
|
*/
|
|
|
|
class ImporterService extends BaseService
|
|
|
|
{
|
|
|
|
protected $flightRepo;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
FlightRepository $flightRepo
|
|
|
|
) {
|
|
|
|
$this->flightRepo = $flightRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a key-value pair to an array
|
|
|
|
* @param $kvp_str
|
|
|
|
* @param array $arr
|
|
|
|
*/
|
|
|
|
protected function setKvp($kvp_str, array &$arr)
|
|
|
|
{
|
|
|
|
$item = explode('=', $kvp_str);
|
|
|
|
if(\count($item) === 1) { # just a list?
|
|
|
|
$arr[] = trim($item[0]);
|
|
|
|
} else { # actually a key-value pair
|
|
|
|
$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-02-22 11:42:47 +08:00
|
|
|
* @return array|string
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-02-22 11:42:47 +08:00
|
|
|
public function parseMultiColumnValues($field)
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
|
|
|
$ret = [];
|
|
|
|
$split_values = explode(';', $field);
|
2018-02-22 11:42:47 +08:00
|
|
|
|
|
|
|
# No multiple values in here, just a straight value
|
|
|
|
if(\count($split_values) === 1) {
|
|
|
|
return $split_values[0];
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:38:35 +08:00
|
|
|
foreach($split_values as $value) {
|
|
|
|
|
|
|
|
# This isn't in the query string format, so it's
|
|
|
|
# just a straight key-value pair set
|
|
|
|
if(strpos($value, '?') === false) {
|
|
|
|
$this->setKvp($value, $ret);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# This contains the query string, which turns it
|
|
|
|
# into the multi-level array
|
|
|
|
|
|
|
|
$query_str = explode('?', $value);
|
|
|
|
$parent = trim($query_str[0]);
|
|
|
|
|
|
|
|
$children = [];
|
|
|
|
$kvp = explode('&', trim($query_str[1]));
|
|
|
|
foreach($kvp as $items) {
|
|
|
|
$this->setKvp($items, $children);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret[$parent] = $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import flights
|
|
|
|
* @param $csv_str
|
2018-02-22 11:42:47 +08:00
|
|
|
* @param bool $delete_previous
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-02-22 11:42:47 +08:00
|
|
|
public function importFlights($csv_str, bool $delete_previous=true)
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|