/
home
/
corsairdevelopme
/
public_html
/
amplivo-console
/
app
/
Services
/
Upload File
HOME
<?php namespace App\Services; class CsvMapping { /** * @param $file (Uploaded $request->file) * @return array (fileName and filePath) */ public function moveUploadedFile($file): array { $fileName = time() . '.' . $file->getClientOriginalExtension(); $file->move(public_path('imports'), $fileName); return [ 'fileName' => $fileName, 'filePath' => public_path('imports/' . $fileName) ]; } /** * @param $filePath * @param string $delimiter * @return array|false */ public function getCsvHeaderAndRecords($filePath, string $delimiter = ','): bool|array { if (!file_exists($filePath) || !is_readable($filePath)) return false; $header = null; $records = []; if (($handle = fopen($filePath, 'r')) !== false) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { if (!$header) $header = $row; else { if (count($row) == count($header)) $records[] = array_combine($header, $row); } } fclose($handle); } return [ 'header' => $header, 'records' => $records ]; } /** * @param $filePath * @param string $delimiter * @return array|false */ public function getCsvHeader($filePath, string $delimiter = ','): bool|array { if (!file_exists($filePath) || !is_readable($filePath)) return false; $header = null; $records = []; if (($handle = fopen($filePath, 'r')) !== false) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { if (!$header) $header = $row; break; } fclose($handle); } return [ 'header' => $header, 'records' => $records ]; } function getColumnListings($model, $excludedColumns = []): array { $columnsToExclude = ['id', 'expired_date', 'user_id', 'remember_token', 'created_at', 'updated_at', 'login_token']; if (isset($excludedColumns) && count($excludedColumns) > 0) { $columnsToExclude = array_unique(array_merge($columnsToExclude, $excludedColumns)); } $csrListTable = new $model(); $databaseColumns = $csrListTable->getConnection()->getSchemaBuilder()->getColumnListing($csrListTable->getTable()); return array_diff($databaseColumns, $columnsToExclude); } }