<?php
 namespace Modules\Admin\ImportEntities\Model\FileParser; class Csv extends \Modules\Admin\ImportEntities\Model\FileParserAbstract { public $delimiter=';'; protected function getParsedContent() { $return = []; if (!$this->filePath) { return false; } $fileHandle = fopen($this->filePath, "r"); if (($data = fgetcsv($fileHandle, null, $this->delimiter, "\"", "")) !== FALSE) { $this->fileHeader = array_map('trim',$data); } $this->fileHeader[0] = $this->removeUtf8Bom($this->fileHeader[0]); while (1) { if (!$fileHandle) { break; } $row = fgetcsv($fileHandle, null, $this->delimiter, "\"", ""); if ($row===false) { break; } if (!$row or !array_filter($row)) { continue; } $row = array_map('trim', $row); if (count($this->fileHeader)==count($row)) { $return[] = array_combine($this->fileHeader, $row); } } $this->fileContent = $return; } public function getExistingColumns() { if (!$this->columnsFormat) { return null; } if (!$this->fileHeader) { $this->getParsedContent(); } return array_intersect($this->columnsFormat, $this->fileHeader); } public function getFileContent() { if (!$this->fileContent) { $this->getParsedContent(); } return $this->fileContent; } private function removeUtf8Bom($text) { $bom = pack('H*','EFBBBF'); $text = preg_replace("/^$bom/", '', $text); return $text; } }