silverstripe-framework/dev/CSVParser.php

317 lines
6.0 KiB
PHP
Raw Permalink Normal View History

<?php
/**
2014-08-15 08:53:05 +02:00
* Class to handle parsing of CSV files, where the column headers are in the
2013-05-14 12:01:15 +02:00
* first row.
*
2014-08-15 08:53:05 +02:00
* The idea is that you pass it another object to handle the actual processing
2013-05-14 12:01:15 +02:00
* of the data in the CSV file.
2014-08-15 08:53:05 +02:00
*
* Usage:
2013-05-14 12:01:15 +02:00
*
* <code>
* $parser = new CSVParser('myfile.csv');
* $parser->mapColumns(array(
* '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();
* }
* </code>
2014-08-15 08:53:05 +02:00
*
* @package framework
* @subpackage bulkloading
*/
class CSVParser extends Object implements Iterator {
2013-05-14 12:01:15 +02:00
/**
* @var string $filename
*/
protected $filename;
2013-05-14 12:01:15 +02:00
/**
* @var resource $fileHandle
*/
protected $fileHandle;
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* Map of source columns to output columns.
*
2014-08-15 08:53:05 +02:00
* Once they get into this variable, all of the source columns are in
2013-05-14 12:01:15 +02:00
* lowercase.
*
* @var array
*/
protected $columnMap = array();
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* The header row used to map data in the CSV file.
*
2014-08-15 08:53:05 +02:00
* To begin with, this is null. Once it has been set, data will get
2013-05-14 12:01:15 +02:00
* returned from the CSV file.
*
* @var array
*/
protected $headerRow = null;
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* A custom header row provided by the caller.
*
* @var array
*/
protected $providedHeaderRow = null;
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* The data of the current row.
*
* @var array
*/
protected $currentRow = null;
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* The current row number.
*
2014-08-15 08:53:05 +02:00
* 1 is the first data row in the CSV file; the header row, if it exists,
2013-05-14 12:01:15 +02:00
* is ignored.
*
* @var int
*/
protected $rowNum = 0;
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* The character for separating columns.
*
* @var string
*/
protected $delimiter = ",";
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* The character for quoting columns.
*
* @var string
*/
protected $enclosure = '"';
2014-08-15 08:53:05 +02:00
/**
* Open a CSV file for parsing.
2013-05-14 12:01:15 +02:00
*
* You can use the object returned in a foreach loop to extract the data.
*
* @param $filename The name of the file. If relative, it will be relative to the site's base dir
* @param $delimiter The character for seperating columns
* @param $enclosure The character for quoting or enclosing columns
*/
public function __construct($filename, $delimiter = ",", $enclosure = '"') {
$filename = Director::getAbsFile($filename);
$this->filename = $filename;
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
2013-05-14 12:01:15 +02:00
parent::__construct();
}
2014-08-15 08:53:05 +02:00
/**
* Re-map columns in the CSV file.
2013-05-14 12:01:15 +02:00
*
* This can be useful for identifying synonyms in the file. For example:
*
* <code>
* $csv->mapColumns(array(
* 'firstname' => 'FirstName',
* 'last name' => 'Surname',
* ));
* </code>
2013-05-14 12:01:15 +02:00
*
* @param array
*/
public function mapColumns($columnMap) {
if($columnMap) {
$lowerColumnMap = array();
2014-08-15 08:53:05 +02:00
foreach($columnMap as $k => $v) {
$lowerColumnMap[strtolower($k)] = $v;
}
$this->columnMap = array_merge($this->columnMap, $lowerColumnMap);
}
}
2013-05-14 12:01:15 +02:00
/**
2014-08-15 08:53:05 +02:00
* If your CSV file doesn't have a header row, then you can call this
2013-05-14 12:01:15 +02:00
* function to provide one.
*
2014-08-15 08:53:05 +02:00
* If you call this function, then the first row of the CSV will be
2013-05-14 12:01:15 +02:00
* included in the data returned.
*
* @param array
*/
public function provideHeaderRow($headerRow) {
$this->providedHeaderRow = $headerRow;
}
/**
2013-05-14 12:01:15 +02:00
* Open the CSV file for reading.
*/
protected function openFile() {
ini_set('auto_detect_line_endings',1);
$this->fileHandle = fopen($this->filename,'r');
2014-08-15 08:53:05 +02:00
if($this->providedHeaderRow) {
$this->headerRow = $this->remapHeader($this->providedHeaderRow);
}
}
/**
2013-05-14 12:01:15 +02:00
* Close the CSV file and re-set all of the internal variables.
*/
protected function closeFile() {
2013-05-14 12:01:15 +02:00
if($this->fileHandle) {
fclose($this->fileHandle);
}
2013-05-14 12:01:15 +02:00
$this->fileHandle = null;
$this->rowNum = 0;
$this->currentRow = null;
$this->headerRow = null;
}
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* Get a header row from the CSV file.
*/
protected function fetchCSVHeader() {
2013-05-14 12:01:15 +02:00
$srcRow = fgetcsv(
2014-08-15 08:53:05 +02:00
$this->fileHandle,
0,
$this->delimiter,
2013-05-14 12:01:15 +02:00
$this->enclosure
);
$this->headerRow = $this->remapHeader($srcRow);
}
2014-08-15 08:53:05 +02:00
/**
2013-05-14 12:01:15 +02:00
* Map the contents of a header array using $this->mappedColumns.
*
* @param array
*
* @return array
*/
protected function remapHeader($header) {
$mappedHeader = array();
2013-05-14 12:01:15 +02:00
foreach($header as $item) {
2013-05-14 12:01:15 +02:00
if(isset($this->columnMap[strtolower($item)])) {
$item = $this->columnMap[strtolower($item)];
}
$mappedHeader[] = $item;
}
return $mappedHeader;
}
2014-08-15 08:53:05 +02:00
/**
* Get a row from the CSV file and update $this->currentRow;
2013-05-14 12:01:15 +02:00
*
* @return array
*/
protected function fetchCSVRow() {
2013-05-14 12:01:15 +02:00
if(!$this->fileHandle) {
$this->openFile();
}
if(!$this->headerRow) {
$this->fetchCSVHeader();
}
$this->rowNum++;
2014-08-15 08:53:05 +02:00
2013-05-14 12:01:15 +02:00
$srcRow = fgetcsv(
2014-08-15 08:53:05 +02:00
$this->fileHandle,
0,
$this->delimiter,
2013-05-14 12:01:15 +02:00
$this->enclosure
);
2014-08-15 08:53:05 +02:00
if($srcRow) {
$row = array();
2014-08-15 08:53:05 +02:00
foreach($srcRow as $i => $value) {
// Allow escaping of quotes and commas in the data
$value = str_replace(
2013-05-14 12:01:15 +02:00
array('\\'.$this->enclosure,'\\'.$this->delimiter),
array($this->enclosure, $this->delimiter),
$value
);
// Trim leading tab
// [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab
$value = ltrim($value, "\t");
if(array_key_exists($i, $this->headerRow)) {
2013-05-14 12:01:15 +02:00
if($this->headerRow[$i]) {
$row[$this->headerRow[$i]] = $value;
}
} else {
user_error("No heading for column $i on row $this->rowNum", E_USER_WARNING);
}
}
2014-08-15 08:53:05 +02:00
$this->currentRow = $row;
} else {
$this->closeFile();
}
2013-05-14 12:01:15 +02:00
2014-08-15 08:53:05 +02:00
return $this->currentRow;
}
2014-08-15 08:53:05 +02:00
/**
* @ignore
*/
public function __destruct() {
$this->closeFile();
}
//// ITERATOR FUNCTIONS
/**
2014-08-15 08:53:05 +02:00
* @ignore
*/
public function rewind() {
$this->closeFile();
$this->fetchCSVRow();
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* @ignore
*/
public function current() {
return $this->currentRow;
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* @ignore
*/
public function key() {
return $this->rowNum;
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* @ignore
*/
public function next() {
$this->fetchCSVRow();
2014-08-15 08:53:05 +02:00
return $this->currentRow;
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* @ignore
*/
public function valid() {
return $this->currentRow ? true : false;
}
2014-08-15 08:53:05 +02:00
}