2 namespace Utility\Import;
4 class Csv implements ImportInterface
6 private $_isExtractedFile = false;
9 private $_delimiter = ',';
10 private $_enclosure = '"';
11 private $_escape = '\\';
12 private $_recordLength = 0;
13 private $_specialFields = array();
14 protected $_headers = array();
17 const FETCH_ASSOC = 2;
20 public function __construct($file, $firstLineIsHeaders = false, $delimiter = '')
24 $this->setDelimiter($delimiter);
27 $this->_fileName = $file;
28 if (!file_exists($this->_fileName))
30 throw new \Exception('File ' . $this->_fileName . ' not found');
32 $this->handleZipArchive();
33 $this->_fileHandle = fopen($this->_fileName, 'r');
34 if ($firstLineIsHeaders)
36 $this->_headers = $this->getRecord();
43 protected function handleZipArchive()
45 if (strpos($this->_fileName, '.zip'))
47 #-> Extract csv from zip archive.
48 $this->processZipFile();
55 protected function processZipFile()
57 $zip = new \ZipArchive();
58 if ($this->zipArchiveSuccessfullyOpened($zip))
60 $this->extractZipFile($zip);
62 #-> Find the csv file.
63 $this->establishFilePath();
67 throw new \Exception('CSV Importer Error: Could not open zip archive!');
75 protected function zipArchiveSuccessfullyOpened(\ZipArchive $zip)
77 return true === $zip->open($this->_fileName);
84 protected function extractZipFile(\ZipArchive $zip)
86 if (false === $zip->extractTo(getcwd() . '/data/extract'))
88 throw new \Exception('CSV Import Utility: Could open but not extract zip archive!');
93 protected function establishFilePath()
95 $fileList = scandir(getcwd() . '/data/extract/');
102 foreach ($fileList as $fileEntry)
104 $numFiles = $this->processFileEntry($ignoreFiles, $fileEntry, $numFiles);
106 $this->ensureSingleCsvFileInZip($numFiles);
107 $this->_isExtractedFile = true;
111 * @param $ignoreFiles
116 protected function processFileEntry($ignoreFiles, $fileEntry, $numFiles)
118 if (isset($ignoreFiles[$fileEntry]))
122 if (strpos($fileEntry, '.csv'))
124 return $this->getFilePath($fileEntry, $numFiles);
128 unlink(getcwd() . '/data/extract/' . $fileEntry);
138 protected function getFilePath($fileEntry, $numFiles)
143 unlink($this->_fileName);
145 $this->_fileName = getcwd() . '/data/extract/' . $fileEntry;
153 protected function ensureSingleCsvFileInZip($numFiles)
157 unlink($this->_fileName);
158 throw new \Exception('CSV Importer Error: More than 1 csv file in zip archive!');
162 unlink($this->_fileName);
163 throw new \Exception('CSV Importer Error: 0 csv files in zip archive!');
167 public function getHeaders()
169 return $this->_headers;
172 public function setDelimiter($delimiter)
174 $this->_delimiter = $delimiter;
175 if (!empty($this->_headers))
178 $this->_headers = $this->getRecord(self::FETCH_LAZY);
182 public function setEnclosure($enclosure)
184 $this->_enclosure = $enclosure;
187 public function setEscape($escape)
189 $this->_escape = $escape;
192 public function setRecordLength($length)
194 $this->_recordLength = $length;
198 * Gets the next record in the file
201 public function getRecord($method = self::FETCH_BOTH)
203 $record = fgetcsv($this->_fileHandle, $this->_recordLength, $this->_delimiter, $this->_enclosure, $this->_escape);
205 // \Utility\Debug::errorLog('$record', $record);
207 if($record === false)
212 $fieldsCount = count($record);
213 foreach ($this->_specialFields as $key => $value)
215 $record[$key] = $value->parse($record[$key]);
217 if (($method & self::FETCH_ASSOC) && !empty($this->_headers))
219 for($i = 0; $i < count($this->_headers); $i++)
221 $record[$this->_headers[$i]] = $record[$i];
224 if (!($method & self::FETCH_LAZY))
226 for ($i = 0; $i < $fieldsCount; $i++)
235 public function reset()
237 fseek($this->_fileHandle, 0);
240 public function setField($fieldNr, ImportInterface $class)
242 $this->_specialFields[$fieldNr - 1] = $class;
245 public function __destruct()
247 fclose($this->_fileHandle);
248 if ($this->_isExtractedFile)
250 unlink($this->_fileName);