false, 'separator' => ',', 'enclosure' => '"', 'expected_header' => array()); 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; } ); } function setSeparator($separator) { $this->params['separator'] = $separator; return $this; } function setEnclosure($enclosure) { $this->params['enclosure'] = $enclosure; return $this; } function setExpectedHeader(array $expected_header) { $this->params['expected_header'] = $expected_header; return $this; } function enableSkipHeader() { $this->params['enable_skip_line'] = true; return $this; } // func : function() function setOnStart(callable $func) { $this->handlers['start'] = $func; return $this; } // func : function(ArrayExtended $columns) function setOnEachLine(callable $func) { $this->handlers['readline'] = $func; return $this; } // func : function(int failed_line_number) function setOnError(callable $func) { $this->handlers['error'] = $func; return $this; } function readLines() { $ok = false; $this->current_num_line = 0; $f = fopen($this->file_path, 'r'); 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'])); } 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++; } fclose($f); } if (!$ok) { call_user_func($this->handlers['error'], $this->current_num_line); } return $ok; } private function checkHeader(array $cols) { $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; } } $ok = $ok && $found; } return $ok; } }