1 | <?php |
||
19 | class CsvFormat implements CsvFormatInterface |
||
20 | { |
||
21 | use GetOptionTrait; |
||
22 | |||
23 | const DEFAULT_DELIMITER = ','; |
||
24 | const DEFAULT_NULL_OUTPUT = '\\N'; |
||
25 | const DEFAULT_HEADER_ROW = -1; |
||
26 | const DEFAULT_DATA_START = 1; |
||
27 | const DEFAULT_LINE_TERMINATOR = "\n"; |
||
28 | const DEFAULT_QUOTE_CHARACTER = '"'; |
||
29 | const DEFAULT_ESCAPE = '\\'; |
||
30 | const DEFAULT_LIMIT = -1; |
||
31 | const DEFAULT_DOUBLE_QUOTE = false; |
||
32 | const DEFAULT_BOM = null; |
||
33 | const DEFAULT_ENCODING = 'UTF-8'; |
||
34 | |||
35 | const OPTION_DELIMITER = 'delimiter'; |
||
36 | const OPTION_NULL_OUTPUT = 'nullOutput'; |
||
37 | const OPTION_HEADER_ROW = 'headerRow'; |
||
38 | const OPTION_DATA_START = 'dataStart'; |
||
39 | const OPTION_LINE_TERMINATOR = 'lineTerminator'; |
||
40 | const OPTION_QUOTE_CHARACTER = 'quoteCharacter'; |
||
41 | const OPTION_ESCAPE = 'escape'; |
||
42 | const OPTION_LIMIT = 'limit'; |
||
43 | const OPTION_DOUBLE_QUOTE = 'doubleQuote'; |
||
44 | const OPTION_BOM = 'bom'; |
||
45 | const OPTION_ENCODING = 'encoding'; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $delimiter; |
||
49 | /** @var string */ |
||
50 | protected $quoteCharacter; |
||
51 | /** @var string */ |
||
52 | protected $nullOutput; |
||
53 | /** @var int */ |
||
54 | protected $headerRow; |
||
55 | /** @var string */ |
||
56 | protected $lineTerminator; |
||
57 | /** @var bool */ |
||
58 | protected $nullQuotes; |
||
59 | /** @var string */ |
||
60 | protected $escape; |
||
61 | /** @var int */ |
||
62 | protected $limit; |
||
63 | /** @var bool */ |
||
64 | protected $doubleQuote; |
||
65 | /** @var int */ |
||
66 | protected $dataStart; |
||
67 | /** @var string|null */ |
||
68 | protected $bom; |
||
69 | /** @var string */ |
||
70 | protected $encoding; |
||
71 | |||
72 | /** |
||
73 | * @param array $options -delimiter <string> (Default: ,) Character to use between fields |
||
74 | * -quoteCharacter <string> (Default: ") |
||
75 | * -nullOutput <string> (Default: \N) |
||
76 | * -headerRow <int> (Default: -1) -1 for no header row. (1 is the first line of the file) |
||
77 | * -dataStart <int> (Default: 1) The line where the data starts (1 is the first list of the |
||
78 | * file) |
||
79 | * -lineTerminator <string> (Default: \n) |
||
80 | * -escape <string> (Default: \\) Character to use for escaping |
||
81 | * -limit <int> Total number of data rows to return |
||
82 | * -doubleQuote <bool> instances of quote in fields are indicated by a double quote |
||
83 | * -bom <string> (Default: null) Specify a ByteOrderMark for this file |
||
84 | * -encoding <string> (Default: UTF-8) Specify the encoding of the csv file |
||
85 | */ |
||
86 | 16 | public function __construct(array $options = []) |
|
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | 27 | public function getDelimiter() |
|
109 | |||
110 | /** |
||
111 | * @param string $delimiter |
||
112 | * |
||
113 | * @return static |
||
114 | */ |
||
115 | 2 | public function setDelimiter($delimiter) |
|
120 | |||
121 | /** |
||
122 | * @return bool |
||
123 | */ |
||
124 | 18 | public function hasQuotes() |
|
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 16 | public function getNullOutput() |
|
136 | |||
137 | /** |
||
138 | * @param string $nullOutput |
||
139 | * |
||
140 | * @return static |
||
141 | */ |
||
142 | 1 | public function setNullOutput($nullOutput) |
|
147 | |||
148 | /** |
||
149 | * @return bool |
||
150 | */ |
||
151 | 13 | public function hasHeaderRow() |
|
155 | |||
156 | /** |
||
157 | * @param int $headerRow |
||
158 | * |
||
159 | * @return static |
||
160 | */ |
||
161 | 2 | public function setHeaderRow($headerRow) |
|
166 | |||
167 | /** |
||
168 | * @return int |
||
169 | */ |
||
170 | 7 | public function getHeaderRow() |
|
174 | |||
175 | /** |
||
176 | * @param int $row |
||
177 | * |
||
178 | * @return static |
||
179 | */ |
||
180 | 2 | public function setDataStart($row) |
|
185 | |||
186 | /** |
||
187 | * @return int |
||
188 | */ |
||
189 | 13 | public function getDataStart() |
|
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | 16 | public function getLineTerminator() |
|
204 | |||
205 | /** |
||
206 | * @param string $lineTerminator |
||
207 | * |
||
208 | * @return static |
||
209 | */ |
||
210 | 1 | public function setLineTerminator($lineTerminator) |
|
215 | |||
216 | /** |
||
217 | * @note Csv Rfc spec defines escaping of quotes to be done using double quotes `""` |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 26 | public function getQuoteCharacter() |
|
225 | |||
226 | /** |
||
227 | * @param string $quoteCharacter |
||
228 | * |
||
229 | * @return static |
||
230 | */ |
||
231 | 1 | public function setQuoteCharacter($quoteCharacter) |
|
236 | |||
237 | /** |
||
238 | * Type type of file format (defined in FileFormatType::) |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 3 | public function getType() |
|
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | 27 | public function getEscapeCharacter() |
|
254 | |||
255 | /** |
||
256 | * @param string $escape |
||
257 | * |
||
258 | * @return static |
||
259 | */ |
||
260 | 1 | public function setEscapeCharacter($escape) |
|
265 | |||
266 | /** |
||
267 | * @return bool |
||
268 | */ |
||
269 | 3 | public function hasEscapeCharacter() |
|
273 | |||
274 | /** |
||
275 | * Get the limit that should be returned (-1 for no limit) |
||
276 | * |
||
277 | * @return int |
||
278 | */ |
||
279 | 12 | public function getLimit() |
|
283 | |||
284 | /** |
||
285 | * Set the limit of the number of items to be returned (-1 for not limit) |
||
286 | * |
||
287 | * @param int $limit |
||
288 | * |
||
289 | * @return static |
||
290 | */ |
||
291 | 1 | public function setLimit($limit) |
|
296 | |||
297 | /** |
||
298 | * @return bool |
||
299 | */ |
||
300 | 24 | public function isDoubleQuote() |
|
304 | |||
305 | /** |
||
306 | * @param bool $doubleQuote |
||
307 | * |
||
308 | * @return static |
||
309 | */ |
||
310 | 1 | public function setDoubleQuote($doubleQuote) |
|
315 | |||
316 | /** |
||
317 | * @return null|string |
||
318 | */ |
||
319 | 27 | public function getBom() |
|
323 | |||
324 | /** |
||
325 | * @param null|string $bom |
||
326 | * |
||
327 | * @return static |
||
328 | */ |
||
329 | 16 | public function setBom($bom) |
|
330 | { |
||
331 | 16 | $this->bom = $bom; |
|
332 | 16 | if (!is_null($bom)) { |
|
333 | 4 | Bom::getEncoding($bom); |
|
334 | } |
||
335 | 16 | return $this; |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * @return string |
||
340 | */ |
||
341 | 24 | public function getEncoding() |
|
348 | |||
349 | /** |
||
350 | * @param string $encoding |
||
351 | * |
||
352 | * @return static |
||
353 | */ |
||
354 | 1 | public function setEncoding($encoding) |
|
359 | } |
||
360 |