2017-01-24 10:36:27 +01:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__.'/ArrayExtended.php');
|
|
|
|
class CSVReader
|
|
|
|
{
|
|
|
|
private $file_path = '';
|
2017-01-24 15:19:32 +01:00
|
|
|
private $params = array('enable_skip_line' => false, 'separator' => ',', 'enclosure' => '"', 'expected_header' => array());
|
2017-01-24 10:36:27 +01:00
|
|
|
private $handlers = array('start' => null, 'error' => null);
|
|
|
|
private $current_num_line = 0;
|
|
|
|
|
|
|
|
function __construct($file_path)
|
|
|
|
{
|
|
|
|
$this->file_path = $file_path;
|
|
|
|
$this->handlers = array(
|
|
|
|
'start' => function() {},
|
2017-01-24 17:20:32 +01:00
|
|
|
'error' => function($failed_line_number) {},
|
|
|
|
'readline' => function(ArrayExtended $cols) { return true; }
|
2017-01-24 10:36:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSeparator($separator)
|
|
|
|
{
|
|
|
|
$this->params['separator'] = $separator;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
function setEnclosure($enclosure)
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
2017-01-24 15:19:32 +01:00
|
|
|
$this->params['enclosure'] = $enclosure;
|
2017-01-24 10:36:27 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-24 11:57:47 +01:00
|
|
|
function setExpectedHeader(array $expected_header)
|
|
|
|
{
|
|
|
|
$this->params['expected_header'] = $expected_header;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:09:38 +01:00
|
|
|
function enableSkipHeader()
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
|
|
|
$this->params['enable_skip_line'] = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// func : function()
|
2017-01-24 17:20:32 +01:00
|
|
|
function setOnStart(callable $func)
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
|
|
|
$this->handlers['start'] = $func;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// func : function(ArrayExtended $columns)
|
2017-01-24 17:20:32 +01:00
|
|
|
function setOnEachLine(callable $func)
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
|
|
|
$this->handlers['readline'] = $func;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// func : function(int failed_line_number)
|
2017-01-24 17:20:32 +01:00
|
|
|
function setOnError(callable $func)
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
|
|
|
$this->handlers['error'] = $func;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-24 17:20:32 +01:00
|
|
|
function readLines()
|
2017-01-24 10:36:27 +01:00
|
|
|
{
|
|
|
|
$ok = false;
|
|
|
|
$this->current_num_line = 0;
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
$f = fopen($this->file_path, 'r');
|
2017-01-24 10:36:27 +01:00
|
|
|
if ($f) {
|
|
|
|
call_user_func($this->handlers['start']);
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
$ok = true;
|
|
|
|
if ($this->params['enable_skip_line']) {
|
|
|
|
$ok = $this->checkHeader(fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure']));
|
|
|
|
}
|
2017-01-24 10:36:27 +01:00
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
while ($ok && ($cols = fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure']))) {
|
|
|
|
$ok = call_user_func($this->handlers['readline'], new ArrayExtended($cols));
|
|
|
|
$this->current_num_line++;
|
2017-01-24 10:36:27 +01:00
|
|
|
}
|
2017-01-24 15:19:32 +01:00
|
|
|
|
|
|
|
fclose($f);
|
2017-01-24 10:36:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$ok) {
|
|
|
|
call_user_func($this->handlers['error'], $this->current_num_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
private function checkHeader(array $cols)
|
2017-01-24 11:57:47 +01:00
|
|
|
{
|
|
|
|
$ok = true;
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
foreach($this->params['expected_header'] as $expected_col => $expected_pos) {
|
|
|
|
$found = false;
|
|
|
|
foreach($cols as $index => $col) {
|
|
|
|
if (strtolower($col) == strtolower($expected_col) && $expected_pos == $index) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
2017-01-24 11:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 15:19:32 +01:00
|
|
|
$ok = $ok && $found;
|
2017-01-24 11:57:47 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 15:19:32 +01:00
|
|
|
return $ok;
|
2017-01-24 11:57:47 +01:00
|
|
|
}
|
2017-01-24 10:36:27 +01:00
|
|
|
}
|