/** * Execute the console command. * * @return mixed */ public function handle() { $converter = new PersonalNameConverter(); $string = $converter('Karl-Peter von Schmidt'); dd($string); $loader = LoaderFactory::make($this->argument('loader')); $loader->setFile($this->argument('file')); // $loader->setOption('delimiter', ';'); // $loader->setOption('header_row', 1); // $loader->setOption('headers', 'number'); // $loader->setOption('strict', false); $loader->load(); $reader = $loader->getReader(); // $reader = var_export($reader, 1); // \File::put(base_path('config/components/loaders/csv.php'), $reader); // $reader = json_encode($reader, JSON_PRETTY_PRINT); // dd($reader); // if ($key = $this->argument('key')) { // $reader = array_get($reader, $key); // } // $data = []; // foreach ($reader as $row) { // $data[] = $row; // } // $this->table(array_keys(head($data)), $data); // return; // foreach ($loader->getReader() as $key => $row) // { // echo $key; var_dump($row); // echo '-----'; // } // dd($loader); }
/** * Execute the console command. * * @return mixed */ public function handle() { $loader = LoaderFactory::make('csv'); $loader->setFile('test/names.csv'); $loader->load(); $data = $loader->getData(); $normalized = []; foreach ($data as $key => $row) { $normalized[$row['country']]['country'] = $row['country']; $normalized[$row['country']]['cities'][$row['city']]['city'] = $row['city']; $normalized[$row['country']]['cities'][$row['city']]['people'][$key] = array_only($row, ['first', 'last']); } $countries = []; $cities = []; $people = []; $idCountry = 1; $idCity = 1; $idPerson = 1; foreach ($normalized as $keyCountry => $valueCountry) { $countries[] = ['id' => $keyCountry + 1, 'country' => $valueCountry['country']]; foreach ($valueCountry['cities'] as $keyCity => $valueCity) { $cities[] = ['id' => $idCity, 'city' => $valueCity['city'], 'country' => $idCountry]; foreach ($valueCity['people'] as $keyPerson => $valuePerson) { $people[] = ['id' => $idPerson, 'first' => $valuePerson['first'], 'last' => $valuePerson['last'], 'city' => $idCity]; $idPerson++; } $idCity++; } $idCountry++; } $writer = new CsvWriter(','); $writer->setStream(fopen(storage_path('test/norm_countries.csv'), 'w')); $writer->writeItem(array('id', 'country')); foreach ($countries as $key => $value) { $writer->writeItem($value); } $writer->finish(); $writer = new CsvWriter(','); $writer->setStream(fopen(storage_path('test/norm_cities.csv'), 'w')); $writer->writeItem(array('id', 'city', 'country')); foreach ($cities as $key => $value) { $writer->writeItem($value); } $writer->finish(); $writer = new CsvWriter(','); $writer->setStream(fopen(storage_path('test/norm_people.csv'), 'w')); $writer->writeItem(array('id', 'first', 'last', 'city')); foreach ($people as $key => $value) { $writer->writeItem($value); } $writer->finish(); dd($normalized); }