Hello fellow PHP Builders,
I just wrote up a class to iterate over a CSV file and would like come comments so that I can improve it and make it more useful. Right now it does what I need but I would like to make it better for the PHP community as a whole.
I am only going to post the class here but if you wish to see usage and examples (and downloads) please see my site http://white-box.us.
Here it is in all its glory:
/**
* Class: CSVIterator
* Desc: Read a CSV file
* @author: Raymond J Kolbe
* @copyright: July 10th, 2007
*/
class CSVIterator extends ArrayIterator{
private $csvFile; // the location to our file
private $colseparator; // the column separator
private $csvData; // The array iterator object
/**
* @param $file The csv file we wish to parse
* @param $colseparator The column separator for data. Defaults to "," (comma)
* @return void
*/
public function __construct($file, $colseparator = ","){
if(!self::isValidFile($file)){
throw new Exception("File is not valid!");
}
$this->csvFile = $file;
$this->colseparator = $colseparator;
// Get and pass our array of data to ArrayIterator
parent::__construct(self::genCsvArray());
}
/**
* @return An array of the lines from our csv file.
* @throws Exception
*/
private function genCsvArray(){
$lines = @file($this->csvFile, FILE_SKIP_EMPTY_LINES);
if(!$lines){
throw new Exception("Unable to pull lines into array.");
}
return $lines;
}
/**
* @param $file The file in question
* @return boolean True if the file is a file or false if it is not a valid file.
*/
public static function isValidFile($file){
return (is_file($file));
}
/**
* @return Void
* @desc: Moves our array pointer forward.
*/
public function moveForward(){
parent::next();
}
/**
* @return array The next row in our file with each column as its own key
*/
public function getDataRow(){
return explode($this->colseparator, parent::current());
}
/**
* @return boolean Returns true if there is still data in the array, false if there is nothing
*/
public function hasData(){
return (parent::valid());
}
}