|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader\CSV; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Reader\AbstractReader; |
|
6
|
|
|
use Box\Spout\Common\Exception\IOException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Reader |
|
10
|
|
|
* This class provides support to read data from a CSV file. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Box\Spout\Reader\CSV |
|
13
|
|
|
*/ |
|
14
|
|
|
class Reader extends AbstractReader |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var resource Pointer to the file to be written */ |
|
17
|
|
|
protected $filePointer; |
|
18
|
|
|
|
|
19
|
|
|
/** @var SheetIterator To iterator over the CSV unique "sheet" */ |
|
20
|
|
|
protected $sheetIterator; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $originalAutoDetectLineEndings; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sets the field delimiter for the CSV. |
|
27
|
|
|
* Needs to be called before opening the reader. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $fieldDelimiter Character that delimits fields |
|
30
|
|
|
* @return Reader |
|
31
|
|
|
*/ |
|
32
|
60 |
|
public function setFieldDelimiter($fieldDelimiter) |
|
33
|
|
|
{ |
|
34
|
60 |
|
$this->getOptions()->setFieldDelimiter($fieldDelimiter); |
|
35
|
60 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets the field enclosure for the CSV. |
|
40
|
|
|
* Needs to be called before opening the reader. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $fieldEnclosure Character that enclose fields |
|
43
|
|
|
* @return Reader |
|
44
|
|
|
*/ |
|
45
|
60 |
|
public function setFieldEnclosure($fieldEnclosure) |
|
46
|
|
|
{ |
|
47
|
60 |
|
$this->getOptions()->setFieldEnclosure($fieldEnclosure); |
|
48
|
60 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sets the encoding of the CSV file to be read. |
|
53
|
|
|
* Needs to be called before opening the reader. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $encoding Encoding of the CSV file to be read |
|
56
|
|
|
* @return Reader |
|
57
|
|
|
*/ |
|
58
|
72 |
|
public function setEncoding($encoding) |
|
59
|
|
|
{ |
|
60
|
72 |
|
$this->getOptions()->setEncoding($encoding); |
|
61
|
72 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the EOL for the CSV. |
|
66
|
|
|
* Needs to be called before opening the reader. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $endOfLineCharacter used to properly get lines from the CSV file. |
|
69
|
|
|
* @return Reader |
|
70
|
|
|
*/ |
|
71
|
60 |
|
public function setEndOfLineCharacter($endOfLineCharacter) |
|
72
|
|
|
{ |
|
73
|
60 |
|
$this->getOptions()->setEndOfLineCharacter($endOfLineCharacter); |
|
74
|
60 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the reader's current options |
|
79
|
|
|
* |
|
80
|
|
|
* @return ReaderOptions |
|
81
|
|
|
*/ |
|
82
|
78 |
|
protected function getOptions() |
|
83
|
|
|
{ |
|
84
|
78 |
|
if (!isset($this->options)) { |
|
85
|
78 |
|
$this->options = new ReaderOptions(); |
|
86
|
78 |
|
} |
|
87
|
78 |
|
return $this->options; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns whether stream wrappers are supported |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
6 |
|
protected function doesSupportStreamWrapper() |
|
96
|
|
|
{ |
|
97
|
6 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Opens the file at the given path to make it ready to be read. |
|
102
|
|
|
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $filePath Path of the CSV file to be read |
|
105
|
|
|
* @return void |
|
106
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
|
107
|
|
|
*/ |
|
108
|
81 |
|
protected function openReader($filePath) |
|
109
|
|
|
{ |
|
110
|
81 |
|
$this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings'); |
|
111
|
81 |
|
ini_set('auto_detect_line_endings', '1'); |
|
112
|
|
|
|
|
113
|
81 |
|
$this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r'); |
|
114
|
81 |
|
if (!$this->filePointer) { |
|
115
|
3 |
|
throw new IOException("Could not open file $filePath for reading."); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
78 |
|
$this->sheetIterator = new SheetIterator( |
|
119
|
78 |
|
$this->filePointer, |
|
120
|
78 |
|
$this->getOptions(), |
|
121
|
78 |
|
$this->globalFunctionsHelper |
|
122
|
78 |
|
); |
|
123
|
78 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns an iterator to iterate over sheets. |
|
127
|
|
|
* |
|
128
|
|
|
* @return SheetIterator To iterate over sheets |
|
129
|
|
|
*/ |
|
130
|
78 |
|
protected function getConcreteSheetIterator() |
|
131
|
|
|
{ |
|
132
|
78 |
|
return $this->sheetIterator; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Closes the reader. To be used after reading the file. |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
78 |
|
protected function closeReader() |
|
142
|
|
|
{ |
|
143
|
78 |
|
if ($this->filePointer) { |
|
144
|
78 |
|
$this->globalFunctionsHelper->fclose($this->filePointer); |
|
145
|
78 |
|
} |
|
146
|
|
|
|
|
147
|
78 |
|
ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings); |
|
148
|
78 |
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|