112 lines
2.9 KiB
PHP
Raw Normal View History

2017-01-24 10:36:27 +01:00
<?php
require_once(__DIR__.'/ArrayExtended.php');
class CSVReader
{
private $file_path = '';
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() {},
'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;
}
function setEnclosure($enclosure)
2017-01-24 10:36:27 +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;
}
function enableSkipHeader()
2017-01-24 10:36:27 +01:00
{
$this->params['enable_skip_line'] = true;
return $this;
}
// func : function()
function setOnStart(callable $func)
2017-01-24 10:36:27 +01:00
{
$this->handlers['start'] = $func;
return $this;
}
// func : function(ArrayExtended $columns)
function setOnEachLine(callable $func)
2017-01-24 10:36:27 +01:00
{
$this->handlers['readline'] = $func;
return $this;
}
// func : function(int failed_line_number)
function setOnError(callable $func)
2017-01-24 10:36:27 +01:00
{
$this->handlers['error'] = $func;
return $this;
}
function readLines()
2017-01-24 10:36:27 +01:00
{
$ok = false;
$this->current_num_line = 0;
$f = fopen($this->file_path, 'r');
2017-01-24 10:36:27 +01:00
if ($f) {
call_user_func($this->handlers['start']);
$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
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
}
fclose($f);
2017-01-24 10:36:27 +01:00
}
if (!$ok) {
call_user_func($this->handlers['error'], $this->current_num_line);
}
return $ok;
}
private function checkHeader(array $cols)
2017-01-24 11:57:47 +01:00
{
$ok = true;
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
}
}
$ok = $ok && $found;
2017-01-24 11:57:47 +01:00
}
return $ok;
2017-01-24 11:57:47 +01:00
}
2017-01-24 10:36:27 +01:00
}