Coseva\CSV::parse PHP Method

parse() public method

This method will convert the csv to an array and will run all registered filters against it.
public parse ( integer $rowOffset ) : CSV
$rowOffset integer Determines which row the parser will start on. Zero-based index. Note: When using a row offset, skipped rows will never be parsed nor stored. As such, we encourage to use different instances when mixing offsets, to prevent resultsets from interfering.
return CSV $this
    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;
    }