public function parse($rowOffset = 0)
{
// Cast the row offset as an integer.
$rowOffset = (int) $rowOffset;
if (!isset($this->_rows)) {
// Open the file if there is no SplFIleObject present.
if (!$this->_file instanceof SplFileObject) {
$this->_file = new SplFileObject($this->_fileConfig['filename'], $this->_fileConfig['open_mode'], $this->_fileConfig['use_include_path']);
// Set the flag to parse CSV.
$this->_file->setFlags(SplFileObject::READ_CSV);
// Set the delimiter
$this->setDelimiter($this->_fileConfig['delimiter']);
}
$this->_rows = array();
// Fetch the rows.
foreach (new LimitIterator($this->_file, $rowOffset) as $key => $row) {
// Apply any filters.
$this->_rows[$key] = $this->_applyFilters($row);
// Flush empty rows.
if ($this->_flushOnAfterFilter) {
$this->_flushEmptyRow($row, $key, true);
}
}
// Flush the filters.
$this->flushFilters();
// We won't need the file anymore.
unset($this->_file);
} elseif (empty($this->_filters)) {
// Nothing to do here.
// We return now to avoid triggering garbage collection.
return $this;
}
if (!empty($this->_filters)) {
// We explicitely divide the strategies here, since checking this
// after applying filters on every row makes for a double iteration
// through $this->flushEmptyRows().
// We therefore do this while iterating, but array_map cannot supply
// us with a proper index and therefore the flush would be delayed.
if ($this->_flushOnAfterFilter) {
foreach ($this->_rows as $index => &$row) {
// Apply the filters.
$row = $this->_applyFilters($row);
// Flush it if it's empty.
$this->_flushEmptyRow($row, $index);
}
} else {
// Apply our filters.
$this->_rows = array_map(array($this, '_applyFilters'), $this->_rows);
}
// Flush the filters.
$this->flushFilters();
}
// Do some garbage collection to free memory of garbage we won't use.
// @see http://php.net/manual/en/function.gc-collect-cycles.php
if ($this->_garbageCollection) {
gc_collect_cycles();
}
return $this;
}