1
|
<?php
|
2
|
/**
|
3
|
* A CSV file reader
|
4
|
* @author Alexandre Martinez <alexandre.martinez76@gmail.com>
|
5
|
*
|
6
|
*/
|
7
|
class Tx_Extbase_Utility_CsvFileReader extends Tx_Extbase_Utility_AbstractFileReader
|
8
|
{
|
9
|
/**
|
10
|
* Parsed content of file
|
11
|
* @var array
|
12
|
*/
|
13
|
protected $parsedContent;
|
14
|
|
15
|
/**
|
16
|
* Actual data content
|
17
|
* @var array
|
18
|
*/
|
19
|
protected $dataContent;
|
20
|
|
21
|
/**
|
22
|
* Global header data for file,
|
23
|
* before customers blocks
|
24
|
* @var array
|
25
|
*/
|
26
|
protected $header_data;
|
27
|
/**
|
28
|
* Getter for raw parsed content
|
29
|
* No usable data here
|
30
|
* @return array
|
31
|
*/
|
32
|
public function getParsedContent()
|
33
|
{
|
34
|
return $this->parsedContent;
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Getter for parsed content to data
|
39
|
* Usable data is here
|
40
|
* @return array
|
41
|
*/
|
42
|
public function getDataContent()
|
43
|
{
|
44
|
return $this->dataContent;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Parses the content of file
|
49
|
* @return array
|
50
|
*/
|
51
|
protected function parseContent()
|
52
|
{
|
53
|
if (!is_array($this->parsedContent)) {
|
54
|
$this->readCsv();
|
55
|
}
|
56
|
if (!is_array($this->dataContent)) {
|
57
|
$this->populateData();
|
58
|
}
|
59
|
$return=each($this->dataContent);
|
60
|
return is_array($return)?$return['value']:false;
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Reads CSV data from file
|
65
|
* @return void
|
66
|
*/
|
67
|
protected function readCsv()
|
68
|
{
|
69
|
$fhandler=fopen($this->file->getPath(true),"r");
|
70
|
if (!$fhandler) throw new Tx_Extbase_Exception('Cannot open file '.$this->file->getPath(true));
|
71
|
$this->parsedContent=array();
|
72
|
while($data=fgetcsv($fhandler,1000,";")) {
|
73
|
$this->parsedContent[]=$data;
|
74
|
}
|
75
|
if (count($this->parsedContent)==0) throw new Tx_Extbase_Exception('No data in file '.$this->file->getName());
|
76
|
fclose($fhandler);
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Populates data array from previously parsed csv content
|
81
|
* @return void
|
82
|
*/
|
83
|
protected function populateData()
|
84
|
{
|
85
|
$this->dataContent=array();
|
86
|
/**
|
87
|
* Here goes the logic converting raw parsed data to usable data, ready to be mapped
|
88
|
*/
|
89
|
if (count($this->dataContent)==0) throw new Tx_Extbase_Exception('No data content in '.$this->file->getName());
|
90
|
reset($this->dataContent);
|
91
|
}
|
92
|
|
93
|
}
|