path($this->import->filename); $reader = new Xlsx(); $spreadsheet = $reader->load($path); $this->sheet = $spreadsheet->getActiveSheet(); $this->rowIterator = $this->sheet->getRowIterator(); } protected function rowToArray($row): array { $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(FALSE); $row_content = []; $keys = array_values($this->headers); $i = 0; foreach ($cellIterator as $cell) { if (isset($keys[$i])) { $row_content[$keys[$i]] = $cell->getValue(); } $i++; } return $row_content; } /** * @param array $headers * @return bool */ protected function checkHeaders(array $headers): bool { foreach ($headers as $k => $header) { $headers[$k] = Str::before($header, "\n"); } return $headers == array_keys($this->headers); } protected function findId(string $tableNameCol, string $value): string { list($table, $column) = explode('.', $tableNameCol); $model = DB::table($table) ->where($column, $value) ->first(); if (!$model) { echo $this->import->log("SKIP: no {$tableNameCol} {$value} found!", 'WARNING'); } return $model?->id ?? ''; } protected function prepare(): bool { try { $this->import->log('Reading file...'); $this->readFile(); } catch (Exception $exception){ $this->import->log($exception->getMessage(), 'ERROR'); $this->import->status = 'ERROR'; $this->import->save(); return false; } try { $this->import->log('Checking headers...'); $headers = $this->rowToArray($this->rowIterator->current()); if (!$this->checkHeaders(array_values($headers))) { throw new Exception("Invalid headers"); } } catch (Exception $exception){ $this->import->log($exception->getMessage(), 'ERROR'); $this->import->status = 'ERROR'; $this->import->save(); return false; } return true; } }