Project

General

Profile

Feature #5718 » CsvFileReader.php

Alexandre Martinez, 2009-12-11 14:21

 
<?php
/**
* A CSV file reader
* @author Alexandre Martinez <alexandre.martinez76@gmail.com>
*
*/
class Tx_Extbase_Utility_CsvFileReader extends Tx_Extbase_Utility_AbstractFileReader
{
/**
* Parsed content of file
* @var array
*/
protected $parsedContent;

/**
* Actual data content
* @var array
*/
protected $dataContent;

/**
* Global header data for file,
* before customers blocks
* @var array
*/
protected $header_data;
/**
* Getter for raw parsed content
* No usable data here
* @return array
*/
public function getParsedContent()
{
return $this->parsedContent;
}

/**
* Getter for parsed content to data
* Usable data is here
* @return array
*/
public function getDataContent()
{
return $this->dataContent;
}

/**
* Parses the content of file
* @return array
*/
protected function parseContent()
{
if (!is_array($this->parsedContent)) {
$this->readCsv();
}
if (!is_array($this->dataContent)) {
$this->populateData();
}
$return=each($this->dataContent);
return is_array($return)?$return['value']:false;
}

/**
* Reads CSV data from file
* @return void
*/
protected function readCsv()
{
$fhandler=fopen($this->file->getPath(true),"r");
if (!$fhandler) throw new Tx_Extbase_Exception('Cannot open file '.$this->file->getPath(true));
$this->parsedContent=array();
while($data=fgetcsv($fhandler,1000,";")) {
$this->parsedContent[]=$data;
}
if (count($this->parsedContent)==0) throw new Tx_Extbase_Exception('No data in file '.$this->file->getName());
fclose($fhandler);
}

/**
* Populates data array from previously parsed csv content
* @return void
*/
protected function populateData()
{
$this->dataContent=array();
/**
* Here goes the logic converting raw parsed data to usable data, ready to be mapped
*/
if (count($this->dataContent)==0) throw new Tx_Extbase_Exception('No data content in '.$this->file->getName());
reset($this->dataContent);
}

}
(4-4/4)