|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSanquer\ColibriCsv; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Dialect |
|
7
|
|
|
* |
|
8
|
|
|
* @author Charles Sanquer <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class Dialect |
|
11
|
|
|
{ |
|
12
|
|
|
const ENCLOSING_ALL = 'all'; |
|
13
|
|
|
const ENCLOSING_MINIMAL = 'minimal'; |
|
14
|
|
|
const ENCLOSING_NONNUMERIC = 'nonnumeric'; |
|
15
|
|
|
|
|
16
|
|
|
protected static $defaultOptions = array( |
|
17
|
|
|
'excel' => array( |
|
18
|
|
|
'delimiter' => ';', |
|
19
|
|
|
'enclosure' => '"', |
|
20
|
|
|
'enclosing_mode' => 'minimal', |
|
21
|
|
|
'encoding' => 'CP1252', |
|
22
|
|
|
'eol' => "\r\n", |
|
23
|
|
|
'escape' => "\\", |
|
24
|
|
|
'escape_double' => true, |
|
25
|
|
|
'bom' => false, |
|
26
|
|
|
'translit' => 'translit', |
|
27
|
|
|
'force_encoding_detect' => false, |
|
28
|
|
|
'first_row_header' => false, |
|
29
|
|
|
'skip_empty' => false, |
|
30
|
|
|
'trim' => false, |
|
31
|
|
|
), |
|
32
|
|
|
'unix' => array( |
|
33
|
|
|
'delimiter' => ',', |
|
34
|
|
|
'enclosure' => '"', |
|
35
|
|
|
'enclosing_mode' => 'minimal', |
|
36
|
|
|
'encoding' => 'UTF-8', |
|
37
|
|
|
'eol' => "\n", |
|
38
|
|
|
'escape' => "\\", |
|
39
|
|
|
'escape_double' => true, |
|
40
|
|
|
'bom' => false, |
|
41
|
|
|
'translit' => 'translit', |
|
42
|
|
|
'force_encoding_detect' => false, |
|
43
|
|
|
'first_row_header' => false, |
|
44
|
|
|
'skip_empty' => false, |
|
45
|
|
|
'trim' => false, |
|
46
|
|
|
), |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $translit; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $eol; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $encoding; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $enclosingMode; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $enclosure; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $escape; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @var bool |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $escapeDouble; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @var string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $delimiter; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* @var bool |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $useBom = false; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* |
|
105
|
|
|
* @var bool |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $firstRowHeader = false; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* |
|
111
|
|
|
* @var bool |
|
112
|
|
|
*/ |
|
113
|
|
|
protected $trim = false; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* |
|
117
|
|
|
* @var bool |
|
118
|
|
|
*/ |
|
119
|
|
|
protected $forceEncodingDetection; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* @var bool |
|
124
|
|
|
*/ |
|
125
|
|
|
protected $skipEmptyLines; |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* available options : |
|
129
|
|
|
* - delimiter : (default = ';') |
|
130
|
|
|
* - enclosure : (default = '"') |
|
131
|
|
|
* - encoding : (default = 'CP1252') |
|
132
|
|
|
* - eol : (default = "\r\n") |
|
133
|
|
|
* - escape : (default = "\\") |
|
134
|
|
|
* - first_row_header : (default = false) use the first CSV row as header |
|
135
|
|
|
* - bom : (default = false) add UTF8 BOM marker |
|
136
|
|
|
* - translit : (default = 'translit') iconv translit option possible values : 'translit', 'ignore', null |
|
137
|
|
|
* - force_encoding_detect : (default = false) |
|
138
|
|
|
* - skip_empty : (default = false) remove lines with empty values |
|
139
|
|
|
* - trim : (default = false) trim each values on each line |
|
140
|
|
|
* |
|
141
|
|
|
* N.B. : Be careful, the options 'force_encoding_detect', 'skip_empty' and 'trim' |
|
142
|
|
|
* decrease significantly the performances |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $options Dialect Options to describe CSV file parameters |
|
145
|
|
|
*/ |
|
146
|
139 |
|
public function __construct($options = []) |
|
147
|
|
|
{ |
|
148
|
139 |
|
$options = is_array($options) ? $options : []; |
|
149
|
|
|
|
|
150
|
139 |
|
$cleanedOptions = []; |
|
151
|
139 |
|
foreach ($options as $key => $value) { |
|
152
|
44 |
|
$cleanedOptions[strtolower($key)] = $value; |
|
153
|
139 |
|
} |
|
154
|
|
|
|
|
155
|
139 |
|
$options = array_merge(static::getDialectDefaultOptions('excel'), $cleanedOptions); |
|
156
|
|
|
|
|
157
|
139 |
|
$this->setDelimiter($options['delimiter']); |
|
158
|
139 |
|
$this->setEnclosure($options['enclosure']); |
|
159
|
139 |
|
$this->setEncoding($options['encoding']); |
|
160
|
139 |
|
$this->setEncoding($options['encoding']); |
|
161
|
139 |
|
$this->setEnclosingMode($options['enclosing_mode']); |
|
162
|
139 |
|
$this->setLineEndings($options['eol']); |
|
163
|
139 |
|
$this->setEscape($options['escape']); |
|
164
|
139 |
|
$this->setEscapeDouble($options['escape_double']); |
|
165
|
139 |
|
$this->setTranslit($options['translit']); |
|
166
|
139 |
|
$this->setUseBom($options['bom']); |
|
167
|
139 |
|
$this->setTrim($options['trim']); |
|
168
|
139 |
|
$this->setFirstRowHeader($options['first_row_header']); |
|
169
|
139 |
|
$this->setForceEncodingDetection($options['force_encoding_detect']); |
|
170
|
139 |
|
$this->setSkipEmptyLines($options['skip_empty']); |
|
171
|
139 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* get Default CSV options for a specific CSV reader application like Excel |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $CSVType default = excel |
|
177
|
|
|
* |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
139 |
|
public static function getDialectDefaultOptions($CSVType = 'excel') |
|
181
|
|
|
{ |
|
182
|
139 |
|
return isset(static::$defaultOptions[$CSVType]) ? static::$defaultOptions[$CSVType] : []; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* return a CSV Dialect for Excel |
|
187
|
|
|
* |
|
188
|
|
|
* @return Dialect |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public static function createExcelDialect() |
|
191
|
|
|
{ |
|
192
|
1 |
|
return new self(static::getDialectDefaultOptions('excel')); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* return a standard CSV Dialect for unix with UTF-8 |
|
197
|
|
|
* |
|
198
|
|
|
* @return Dialect |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public static function createUnixDialect() |
|
201
|
|
|
{ |
|
202
|
1 |
|
return new self(static::getDialectDefaultOptions('unix')); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $eol |
|
208
|
|
|
* @return Dialect |
|
209
|
|
|
*/ |
|
210
|
139 |
|
public function setLineEndings($eol) |
|
211
|
|
|
{ |
|
212
|
|
|
switch ($eol) { |
|
213
|
139 |
|
case 'unix': |
|
214
|
139 |
|
case 'linux': |
|
215
|
139 |
|
case "\n": |
|
216
|
42 |
|
$this->eol = "\n"; |
|
217
|
42 |
|
break; |
|
218
|
|
|
|
|
219
|
139 |
|
case 'mac': |
|
220
|
139 |
|
case 'macos': |
|
221
|
139 |
|
case "\r": |
|
222
|
3 |
|
$this->eol = "\r"; |
|
223
|
3 |
|
break; |
|
224
|
|
|
|
|
225
|
139 |
|
case 'windows': |
|
226
|
139 |
|
case 'win': |
|
227
|
139 |
|
case "\r\n": |
|
228
|
139 |
|
default: |
|
229
|
139 |
|
$this->eol = "\r\n"; |
|
230
|
139 |
|
break; |
|
231
|
2 |
|
} |
|
232
|
|
|
|
|
233
|
139 |
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* |
|
238
|
|
|
* @return string |
|
239
|
|
|
*/ |
|
240
|
36 |
|
public function getTranslit() |
|
241
|
|
|
{ |
|
242
|
36 |
|
return $this->translit; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $translit default = "translit" (iconv option : 'translit', 'ignore', null) |
|
248
|
|
|
* @return Dialect |
|
249
|
|
|
*/ |
|
250
|
139 |
|
public function setTranslit($translit) |
|
251
|
|
|
{ |
|
252
|
139 |
|
$translit = strtolower($translit); |
|
253
|
139 |
|
$this->translit = in_array($translit, ['translit', 'ignore']) ? $translit : null; |
|
254
|
|
|
|
|
255
|
139 |
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $encoding |
|
261
|
|
|
* @return Dialect |
|
262
|
|
|
*/ |
|
263
|
139 |
|
public function setEncoding($encoding) |
|
264
|
|
|
{ |
|
265
|
139 |
|
$this->encoding = empty($encoding) ? 'CP1252' : $encoding; |
|
266
|
|
|
|
|
267
|
139 |
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $enclosure |
|
273
|
|
|
* @return Dialect |
|
274
|
|
|
*/ |
|
275
|
139 |
|
public function setEnclosure($enclosure) |
|
276
|
|
|
{ |
|
277
|
139 |
|
$this->enclosure = empty($enclosure) ? '"' : $enclosure; |
|
278
|
|
|
|
|
279
|
139 |
|
return $this; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* |
|
284
|
|
|
* @return string |
|
285
|
|
|
*/ |
|
286
|
19 |
|
public function getEnclosingMode() |
|
287
|
|
|
{ |
|
288
|
19 |
|
return $this->enclosingMode; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $enclosingMode |
|
294
|
|
|
* @return Dialect |
|
295
|
|
|
*/ |
|
296
|
139 |
|
public function setEnclosingMode($enclosingMode) |
|
297
|
|
|
{ |
|
298
|
139 |
|
$this->enclosingMode = in_array($enclosingMode, [ |
|
299
|
139 |
|
static::ENCLOSING_ALL, |
|
300
|
139 |
|
static::ENCLOSING_MINIMAL, |
|
301
|
139 |
|
static::ENCLOSING_NONNUMERIC, |
|
302
|
139 |
|
]) ? $enclosingMode : static::ENCLOSING_MINIMAL; |
|
303
|
|
|
|
|
304
|
139 |
|
return $this; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* |
|
309
|
|
|
* @return bool |
|
310
|
|
|
*/ |
|
311
|
21 |
|
public function getEscapeDouble() |
|
312
|
|
|
{ |
|
313
|
21 |
|
return $this->escapeDouble; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* |
|
318
|
|
|
* @param bool $escapeDouble |
|
319
|
|
|
* @return Dialect |
|
320
|
|
|
*/ |
|
321
|
139 |
|
public function setEscapeDouble($escapeDouble) |
|
322
|
|
|
{ |
|
323
|
139 |
|
$this->escapeDouble = (bool) $escapeDouble; |
|
324
|
|
|
|
|
325
|
139 |
|
return $this; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* |
|
330
|
|
|
* @param string $escape |
|
331
|
|
|
* @return Dialect |
|
332
|
|
|
*/ |
|
333
|
139 |
|
public function setEscape($escape) |
|
334
|
|
|
{ |
|
335
|
139 |
|
$this->escape = empty($escape) ? "\\" : $escape; |
|
336
|
|
|
|
|
337
|
139 |
|
return $this; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* |
|
342
|
|
|
* @param string $delimiter |
|
343
|
|
|
* @return Dialect |
|
344
|
|
|
*/ |
|
345
|
139 |
|
public function setDelimiter($delimiter) |
|
346
|
|
|
{ |
|
347
|
139 |
|
$this->delimiter = empty($delimiter) ? ';' : $delimiter; |
|
348
|
|
|
|
|
349
|
139 |
|
return $this; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* |
|
354
|
|
|
* @param bool $asLabel get EOL as a label string like 'windows', 'unix', 'mac' |
|
355
|
|
|
* @return string |
|
356
|
|
|
*/ |
|
357
|
28 |
|
public function getLineEndings($asLabel = false) |
|
358
|
|
|
{ |
|
359
|
28 |
|
$eol = $this->eol; |
|
360
|
28 |
|
if ($asLabel) { |
|
361
|
11 |
|
switch ($this->eol) { |
|
362
|
11 |
|
case "\n": |
|
363
|
3 |
|
$eol = 'unix'; |
|
364
|
3 |
|
break; |
|
365
|
|
|
|
|
366
|
8 |
|
case "\r": |
|
367
|
3 |
|
$eol = 'mac'; |
|
368
|
3 |
|
break; |
|
369
|
|
|
|
|
370
|
5 |
|
case "\r\n": |
|
371
|
5 |
|
default: |
|
372
|
5 |
|
$eol = 'windows'; |
|
373
|
5 |
|
break; |
|
374
|
11 |
|
} |
|
375
|
11 |
|
} |
|
376
|
|
|
|
|
377
|
28 |
|
return $eol; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* |
|
382
|
|
|
* @return string |
|
383
|
|
|
*/ |
|
384
|
11 |
|
public function getLineEndingsAsLabel() |
|
385
|
|
|
{ |
|
386
|
11 |
|
return $this->getLineEndings(true); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* |
|
391
|
|
|
* @return string |
|
392
|
|
|
*/ |
|
393
|
69 |
|
public function getEncoding() |
|
394
|
|
|
{ |
|
395
|
69 |
|
return $this->encoding; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* |
|
400
|
|
|
* @return string |
|
401
|
|
|
*/ |
|
402
|
44 |
|
public function getEnclosure() |
|
403
|
|
|
{ |
|
404
|
44 |
|
return $this->enclosure; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* |
|
409
|
|
|
* @return string |
|
410
|
|
|
*/ |
|
411
|
44 |
|
public function getEscape() |
|
412
|
|
|
{ |
|
413
|
44 |
|
return $this->escape; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* |
|
418
|
|
|
* @return string |
|
419
|
|
|
*/ |
|
420
|
44 |
|
public function getDelimiter() |
|
421
|
|
|
{ |
|
422
|
44 |
|
return $this->delimiter; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* |
|
427
|
|
|
* @return bool |
|
428
|
|
|
*/ |
|
429
|
37 |
|
public function getUseBom() |
|
430
|
|
|
{ |
|
431
|
37 |
|
return $this->useBom; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* |
|
436
|
|
|
* @param bool $useBom (BOM will be writed when opening the file) |
|
437
|
|
|
* |
|
438
|
|
|
* @return Dialect |
|
439
|
|
|
*/ |
|
440
|
139 |
|
public function setUseBom($useBom) |
|
441
|
|
|
{ |
|
442
|
139 |
|
$this->useBom = (bool) $useBom; |
|
443
|
|
|
|
|
444
|
139 |
|
return $this; |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* |
|
449
|
|
|
* @return bool |
|
450
|
|
|
*/ |
|
451
|
38 |
|
public function getTrim() |
|
452
|
|
|
{ |
|
453
|
38 |
|
return $this->trim; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* |
|
458
|
|
|
* @param bool $trim (trim all values) |
|
459
|
|
|
* |
|
460
|
|
|
* @return Dialect |
|
461
|
|
|
*/ |
|
462
|
139 |
|
public function setTrim($trim) |
|
463
|
|
|
{ |
|
464
|
139 |
|
$this->trim = (bool) $trim; |
|
465
|
|
|
|
|
466
|
139 |
|
return $this; |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* use first row as header |
|
471
|
|
|
* |
|
472
|
|
|
* @return bool |
|
473
|
|
|
*/ |
|
474
|
47 |
|
public function getFirstRowHeader() |
|
475
|
|
|
{ |
|
476
|
47 |
|
return $this->firstRowHeader; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
/** |
|
480
|
|
|
* use first row as header |
|
481
|
|
|
* |
|
482
|
|
|
* @param bool $firstRowHeader |
|
483
|
|
|
* |
|
484
|
|
|
* @return Dialect |
|
485
|
|
|
*/ |
|
486
|
139 |
|
public function setFirstRowHeader($firstRowHeader) |
|
487
|
|
|
{ |
|
488
|
139 |
|
$this->firstRowHeader = (bool) $firstRowHeader; |
|
489
|
|
|
|
|
490
|
139 |
|
return $this; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* |
|
495
|
|
|
* @return bool |
|
496
|
|
|
*/ |
|
497
|
25 |
|
public function getForceEncodingDetection() |
|
498
|
|
|
{ |
|
499
|
25 |
|
return $this->forceEncodingDetection; |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* |
|
504
|
|
|
* @param bool $forceEncodingDetection |
|
505
|
|
|
* @return Dialect |
|
506
|
|
|
*/ |
|
507
|
139 |
|
public function setForceEncodingDetection($forceEncodingDetection) |
|
508
|
|
|
{ |
|
509
|
139 |
|
$this->forceEncodingDetection = (bool) $forceEncodingDetection; |
|
510
|
|
|
|
|
511
|
139 |
|
return $this; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* |
|
516
|
|
|
* @return bool |
|
517
|
|
|
*/ |
|
518
|
35 |
|
public function getSkipEmptyLines() |
|
519
|
|
|
{ |
|
520
|
35 |
|
return $this->skipEmptyLines; |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* |
|
525
|
|
|
* @param bool $skipEmptyLines |
|
526
|
|
|
* @return Dialect |
|
527
|
|
|
*/ |
|
528
|
139 |
|
public function setSkipEmptyLines($skipEmptyLines) |
|
529
|
|
|
{ |
|
530
|
139 |
|
$this->skipEmptyLines = (bool) $skipEmptyLines; |
|
531
|
|
|
|
|
532
|
139 |
|
return $this; |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
|