|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader\CSV; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Helper\EncodingHelper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ReaderOptions |
|
9
|
|
|
* This class is used to customize the reader's behavior |
|
10
|
|
|
* |
|
11
|
|
|
* @package Box\Spout\Reader\CSV |
|
12
|
|
|
*/ |
|
13
|
|
|
class ReaderOptions extends \Box\Spout\Reader\Common\ReaderOptions |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string Defines the character used to delimit fields (one character only) */ |
|
16
|
|
|
protected $fieldDelimiter = ','; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string Defines the character used to enclose fields (one character only) */ |
|
19
|
|
|
protected $fieldEnclosure = '"'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string Encoding of the CSV file to be read */ |
|
22
|
|
|
protected $encoding = EncodingHelper::ENCODING_UTF8; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string Defines the End of line */ |
|
25
|
|
|
protected $endOfLineCharacter = "\n"; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Alignment with other functions like fgets() is discussed here: https://bugs.php.net/bug.php?id=48421 |
|
29
|
|
|
* @var int Number of bytes to read |
|
30
|
78 |
|
*/ |
|
31
|
|
|
protected $maxReadBytesPerLine = 32768; |
|
32
|
78 |
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getFieldDelimiter() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->fieldDelimiter; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
60 |
|
* Sets the field delimiter for the CSV. |
|
43
|
|
|
* Needs to be called before opening the reader. |
|
44
|
60 |
|
* |
|
45
|
60 |
|
* @param string $fieldDelimiter Character that delimits fields |
|
46
|
|
|
* @return ReaderOptions |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setFieldDelimiter($fieldDelimiter) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->fieldDelimiter = $fieldDelimiter; |
|
51
|
78 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
78 |
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getFieldEnclosure() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->fieldEnclosure; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
60 |
|
* Sets the field enclosure for the CSV. |
|
64
|
|
|
* Needs to be called before opening the reader. |
|
65
|
60 |
|
* |
|
66
|
60 |
|
* @param string $fieldEnclosure Character that enclose fields |
|
67
|
|
|
* @return ReaderOptions |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setFieldEnclosure($fieldEnclosure) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->fieldEnclosure = $fieldEnclosure; |
|
72
|
78 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
78 |
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getEncoding() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->encoding; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
72 |
|
* Sets the encoding of the CSV file to be read. |
|
85
|
|
|
* Needs to be called before opening the reader. |
|
86
|
72 |
|
* |
|
87
|
72 |
|
* @param string $encoding Encoding of the CSV file to be read |
|
88
|
|
|
* @return ReaderOptions |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setEncoding($encoding) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->encoding = $encoding; |
|
93
|
78 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
78 |
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string EOL for the CSV |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getEndOfLineCharacter() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->endOfLineCharacter; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
60 |
|
* Sets the EOL for the CSV. |
|
106
|
|
|
* Needs to be called before opening the reader. |
|
107
|
60 |
|
* |
|
108
|
60 |
|
* @param string $endOfLineCharacter used to properly get lines from the CSV file. |
|
109
|
|
|
* @return ReaderOptions |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setEndOfLineCharacter($endOfLineCharacter) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->endOfLineCharacter = $endOfLineCharacter; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets maximum bytes to read in line |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $maxReadBytesPerLine |
|
121
|
|
|
* @return ReaderOptions |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setMaxReadBytesPerLine($maxReadBytesPerLine) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->maxReadBytesPerLine = $maxReadBytesPerLine; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets maximum bytes to read in line |
|
131
|
|
|
* |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getMaxReadBytesPerLine() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->maxReadBytesPerLine; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|