diff --git a/app/Services/ImporterService.php b/app/Services/ImporterService.php new file mode 100644 index 00000000..00e4df9a --- /dev/null +++ b/app/Services/ImporterService.php @@ -0,0 +1,88 @@ +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 + * @return array + */ + public function parseMultiColumnValues($field): array + { + $ret = []; + $split_values = explode(';', $field); + 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 + */ + public function importFlights($csv_str) + { + + } +} diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php new file mode 100644 index 00000000..840e4edd --- /dev/null +++ b/tests/ImporterTest.php @@ -0,0 +1,68 @@ +importerSvc = app(\App\Services\ImporterService::class); + } + + /** + * Test the parsing of different field/column which can be used + * for specifying different field values + */ + public function testMultiFieldValues() + { + $tests = [ + [ + 'input' => 'gate;cost index', + 'expected' => [ + 'gate', + 'cost index', + ] + ], + [ + 'input' => 'gate=B32;cost index=100', + 'expected' => [ + 'gate' => 'B32', + 'cost index' => '100' + ] + ], + [ + 'input' => 'Y?price=200&cost=100; F?price=1200', + 'expected' => [ + 'Y' => [ + 'price' => 200, + 'cost' => 100, + ], + 'F' => [ + 'price' => 1200 + ] + ] + ], + [ + 'input' => 'Y?price&cost; F?price=1200', + 'expected' => [ + 'Y' => [ + 'price', + 'cost', + ], + 'F' => [ + 'price' => 1200 + ] + ] + ] + ]; + + foreach($tests as $test) { + $parsed = $this->importerSvc->parseMultiColumnValues($test['input']); + $this->assertEquals($parsed, $test['expected']); + } + } +}