2018-02-22 11:38:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Service;
|
2018-02-22 11:38:35 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ImporterService
|
|
|
|
* @package App\Services
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class ImporterService extends Service
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
|
|
|
protected $flightRepo;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* ImporterService constructor.
|
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
*/
|
2018-02-22 11:38:35 +08:00
|
|
|
public function __construct(
|
|
|
|
FlightRepository $flightRepo
|
|
|
|
) {
|
|
|
|
$this->flightRepo = $flightRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a key-value pair to an array
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $kvp_str
|
2018-02-22 11:38:35 +08:00
|
|
|
* @param array $arr
|
|
|
|
*/
|
|
|
|
protected function setKvp($kvp_str, array &$arr)
|
|
|
|
{
|
|
|
|
$item = explode('=', $kvp_str);
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\count($item) === 1) { # just a list?
|
2018-02-22 11:38:35 +08:00
|
|
|
$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
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\count($split_values) === 1) {
|
2018-02-22 11:42:47 +08:00
|
|
|
return $split_values[0];
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($split_values as $value) {
|
2018-02-22 11:38:35 +08:00
|
|
|
# This isn't in the query string format, so it's
|
|
|
|
# just a straight key-value pair set
|
2018-03-20 09:50:40 +08:00
|
|
|
if (strpos($value, '?') === false) {
|
2018-02-22 11:38:35 +08:00
|
|
|
$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]));
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($kvp as $items) {
|
2018-02-22 11:38:35 +08:00
|
|
|
$this->setKvp($items, $children);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret[$parent] = $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import flights
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $csv_str
|
2018-02-22 11:42:47 +08:00
|
|
|
* @param bool $delete_previous
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function importFlights($csv_str, bool $delete_previous = true)
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|