Version: current

# Class CSVParser

Class to handle parsing of CSV files, where the column headers are in the first row. The idea is that you pass it another object to handle the actual procesing of the data in the CSV file.

Usage:

    $parser = new CSVParser('myfile.csv');
    $parser->mapColumns(
       'first name' => 'FirstName'
       'lastname' => 'Surname',
       'last name' => 'Surname'
    ));
    foreach($parser as $row) {
             // $row is a map of column name => column value
      $obj = new MyDataObject();
      $obj->update($row);
      $obj->write();
    }

  Object
  CSVParser implements Iterator

Package: sapphire\bulkloading
Located at sapphire/dev/CSVParser.php

## Methods summary

`public` | # `__construct( mixed $filename, mixed $delimiter = ",", mixed $enclosure = '"' )` Open a CSV file for parsing. You can use the object returned in a foreach loop to extract the data Open a CSV file for parsing. You can use the object returned in a foreach loop to extract the data #### Parameters $filename $filename The name of the file. If relative, it will be relative to the site's base dir $delimiter $delimiter The character for seperating columns $enclosure $enclosure The character for quoting or enclosing columns #### Overrides `Object::__construct`
`public` | # `mapColumns( mixed $columnMap )` Re-map columns in the CSV file. This can be useful for identifying synonyms in the file For example:
    $csv->mapColumns(array(
      'firstname' => 'FirstName',
      'last name' => 'Surname',
    ));
Re-map columns in the CSV file. This can be useful for identifying synonyms in the file For example:
    $csv->mapColumns(array(
      'firstname' => 'FirstName',
      'last name' => 'Surname',
    ));
`public` | # `provideHeaderRow( mixed $headerRow )` If your CSV file doesn't have a header row, then you can call this function to provide one. If you call this function, then the first row of the CSV will be included in the data returned. If your CSV file doesn't have a header row, then you can call this function to provide one. If you call this function, then the first row of the CSV will be included in the data returned.
`protected` | # `openFile( )` Open the CSV file for reading Open the CSV file for reading
`protected` | # `closeFile( )` Close the CSV file and re-set all of the internal variables Close the CSV file and re-set all of the internal variables
`protected` | # `fetchCSVHeader( )` Get a header row from the CSV file Get a header row from the CSV file
`protected` | # `remapHeader( mixed $header )` Map the contents of a header array using $this->mappedColumns Map the contents of a header array using $this->mappedColumns
`protected` | # `fetchCSVRow( )` Get a row from the CSV file and update $this->currentRow; Get a row from the CSV file and update $this->currentRow;

### Methods inherited from Object

`__call()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `defineMethods()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Methods inherited from Iterator

`current()`, `key()`, `next()`, `rewind()`, `valid()`

### Magic methods summary

## Properties summary

`protected mixed` | $filename | #
`protected mixed` | $fileHandle | #
`protected array` | $columnMap | # Map of source columns to output columns Once they get into this variable, all of the source columns are in lowercase Map of source columns to output columns Once they get into this variable, all of the source columns are in lowercase
`protected mixed` | $headerRow | # The header row used to map data in the CSV file To begin with, this is null. Once it has been set, data will get returned from the CSV file The header row used to map data in the CSV file To begin with, this is null. Once it has been set, data will get returned from the CSV file
`protected mixed` | $providedHeaderRow | # A custom header row provided by the caller A custom header row provided by the caller
`protected mixed` | $currentRow | # The data of the current row The data of the current row
`protected integer` | $rowNum | # The current row number 1 is the first data row in the CSV file; the header row, if it exists, is ignored The current row number 1 is the first data row in the CSV file; the header row, if it exists, is ignored
`protected string` | $delimiter | # The character for separating columns The character for separating columns
`protected string` | $enclosure | # The character for quoting colums The character for quoting colums

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -