Last active
August 20, 2021 18:18
-
-
Save ProjectOrangeBox/6a464d062b2d11aae6796a48d34a43b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class readCsv implements Iterator | |
{ | |
const ROW_SIZE = 8192; | |
protected $handle = NULL; | |
protected $currentRow = NULL; | |
protected $rowCounter = NULL; | |
protected $hasHeader = null; | |
protected $delimiter = ','; | |
protected $headers = []; | |
protected $normalizeHeader = true; | |
public function __construct(string $filePath, bool $hasHeader = true, bool $normalizeHeader = true, string $delimiter = ',') | |
{ | |
$this->hasHeader = $hasHeader; | |
$this->normalizeHeader = $normalizeHeader; | |
$this->delimiter = $delimiter; | |
if (!file_exists($filePath)) { | |
throw new Exception('File not found.'); | |
} | |
$this->handle = fopen($filePath, 'rb'); | |
if ($this->handle === false) { | |
throw new Exception('File open error.'); | |
} | |
} | |
public function rewind() | |
{ | |
$this->rowCounter = 0; | |
rewind($this->handle); | |
if ($this->hasHeader) { | |
$headers = fgetcsv($this->handle, self::ROW_SIZE, $this->delimiter); | |
$this->rowCounter++; | |
$this->headers = ($this->normalizeHeader) ? $this->cleanHeader($headers) : $headers; | |
} | |
} | |
public function current() | |
{ | |
$row = fgetcsv($this->handle, self::ROW_SIZE, $this->delimiter); | |
if ($this->hasHeader) { | |
if (count($this->headers) != count($row)) { | |
throw new Error('Header and Column count do not match row #' . $this->rowCounter); | |
} | |
$this->currentRow = array_combine($this->headers, $row); | |
} else { | |
$this->currentRow = $row; | |
} | |
$this->rowCounter++; | |
return $this->currentRow; | |
} | |
public function key() | |
{ | |
return $this->rowCounter; | |
} | |
public function next() | |
{ | |
return !feof($this->handle); | |
} | |
public function valid() | |
{ | |
if (!$this->next()) { | |
if (is_resource($this->handle)) { | |
fclose($this->handle); | |
} | |
return false; | |
} | |
return true; | |
} | |
protected function cleanHeader(array $row): array | |
{ | |
$re = '/[^0-9A-Za-z. _ -]+/m'; | |
foreach ($row as $index => $value) { | |
$row[$index] = strtolower(trim(preg_replace($re, '', $value))); | |
} | |
return $row; | |
} | |
public function __destruct() | |
{ | |
if (is_resource($this->handle)) { | |
fclose($this->handle); | |
} | |
} | |
} /* end class */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class writeCsv | |
{ | |
protected $handle = null; | |
protected $separator = ''; | |
public function __construct(string $filePath, array $header = null, string $separator = ',') | |
{ | |
$this->separator = $separator; | |
$dir = dirname($filePath); | |
if (!is_dir($dir)) { | |
mkdir($dir, 0777, true); | |
} | |
$this->handle = fopen($filePath, 'w'); | |
if (is_array($header)) { | |
$this->write($header); | |
} | |
} | |
public function write(array $row): int | |
{ | |
$bytes = fputcsv($this->handle, $row); | |
if ($bytes === false) { | |
throw new Exception('Error writing to CSV file.'); | |
} | |
return $bytes; | |
} | |
public function __destruct() | |
{ | |
if (is_resource($this->handle)) { | |
fclose($this->handle); | |
} | |
} | |
} /* end class */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment