_fileName = $file; if (!file_exists($this->_fileName)) { throw new \Exception('File ' . $this->_fileName . ' not found'); } $this->handleZipArchive(); $this->_fileHandle = fopen($this->_fileName, 'r'); if ($firstLineIsHeaders) { $this->_headers = $this->getRecord(); } } /** * @throws \Exception */ protected function handleZipArchive() { if (strpos($this->_fileName, '.zip')) { #-> Extract csv from zip archive. $this->processZipFile(); } } /** * @throws \Exception */ protected function processZipFile() { $zip = new \ZipArchive(); if ($this->zipArchiveSuccessfullyOpened($zip)) { $this->extractZipFile($zip); #-> Find the csv file. $this->establishFilePath(); } else { throw new \Exception('CSV Importer Error: Could not open zip archive!'); } } /** * @param $zip * @return bool */ protected function zipArchiveSuccessfullyOpened(\ZipArchive $zip) { return true === $zip->open($this->_fileName); } /** * @param $zip * @throws \Exception */ protected function extractZipFile(\ZipArchive $zip) { if (false === $zip->extractTo(getcwd() . '/data/extract')) { throw new \Exception('CSV Import Utility: Could open but not extract zip archive!'); } $zip->close(); } protected function establishFilePath() { $fileList = scandir(getcwd() . '/data/extract/'); $ignoreFiles = array( '.' => true, '..' => true, '.gitignore' => true ); $numFiles = 0; foreach ($fileList as $fileEntry) { $numFiles = $this->processFileEntry($ignoreFiles, $fileEntry, $numFiles); } $this->ensureSingleCsvFileInZip($numFiles); $this->_isExtractedFile = true; } /** * @param $ignoreFiles * @param $fileEntry * @param $numFiles * @return mixed */ protected function processFileEntry($ignoreFiles, $fileEntry, $numFiles) { if (isset($ignoreFiles[$fileEntry])) { return $numFiles; } if (strpos($fileEntry, '.csv')) { return $this->getFilePath($fileEntry, $numFiles); } else { unlink(getcwd() . '/data/extract/' . $fileEntry); return $numFiles; } } /** * @param $fileEntry * @param $numFiles * @return mixed */ protected function getFilePath($fileEntry, $numFiles) { $numFiles++; if (1 < $numFiles) { unlink($this->_fileName); } $this->_fileName = getcwd() . '/data/extract/' . $fileEntry; return $numFiles; } /** * @param $numFiles * @throws \Exception */ protected function ensureSingleCsvFileInZip($numFiles) { if (1 < $numFiles) { unlink($this->_fileName); throw new \Exception('CSV Importer Error: More than 1 csv file in zip archive!'); } if (0 == $numFiles) { unlink($this->_fileName); throw new \Exception('CSV Importer Error: 0 csv files in zip archive!'); } } public function getHeaders() { return $this->_headers; } public function setDelimiter($delimiter) { $this->_delimiter = $delimiter; if (!empty($this->_headers)) { $this->reset(); $this->_headers = $this->getRecord(self::FETCH_LAZY); } } public function setEnclosure($enclosure) { $this->_enclosure = $enclosure; } public function setEscape($escape) { $this->_escape = $escape; } public function setRecordLength($length) { $this->_recordLength = $length; } /** * Gets the next record in the file * @return array */ public function getRecord($method = self::FETCH_BOTH) { $record = fgetcsv($this->_fileHandle, $this->_recordLength, $this->_delimiter, $this->_enclosure, $this->_escape); \Utility\Debug::errorLog('$record', $record); if($record === false) { return false; } $fieldsCount = count($record); foreach ($this->_specialFields as $key => $value) { $record[$key] = $value->parse($record[$key]); } if (($method & self::FETCH_ASSOC) && !empty($this->_headers)) { for($i = 0; $i < count($this->_headers); $i++) $record[$this->_headers[$i]] = $record[$i]; } if (!($method & self::FETCH_LAZY)) { for ($i = 0; $i < $fieldsCount; $i++) { unset($record[$i]); } } return $record; } public function reset() { fseek($this->_fileHandle, 0); } public function setField($fieldNr, ImportInterface $class) { $this->_specialFields[$fieldNr - 1] = $class; } public function __destruct() { fclose($this->_fileHandle); if ($this->_isExtractedFile) { unlink($this->_fileName); } } }