1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
4
|
|
|
* |
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
7
|
|
|
* suck out of working with CSV. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Luke Visinoni |
10
|
|
|
* @author Luke Visinoni <[email protected]> |
11
|
|
|
* @license See LICENSE file (MIT license) |
12
|
|
|
*/ |
13
|
|
|
namespace CSVelte; |
14
|
|
|
|
15
|
|
|
use Traversable; |
16
|
|
|
use function Noz\collect, |
17
|
|
|
Noz\to_array; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* CSV Dialect - Default dialect |
21
|
|
|
* |
22
|
|
|
* Due to CSV being without any definitive format definition for so long, many dialects of it exist. This class allows |
23
|
|
|
* the creation of reusable "dialects" for commonly used flavors of CSV. You can make one for Excel CSV, another for |
24
|
|
|
* tab delimited CSV, another for pipe-delimited, etc. |
25
|
|
|
* |
26
|
|
|
* @todo Where is escapeChar? Do we just use a backslash if doubleQuote is false? |
27
|
|
|
*/ |
28
|
|
|
class Dialect |
29
|
|
|
{ |
30
|
|
|
/** Quote none - never quote any columns */ |
31
|
|
|
const QUOTE_NONE = 0; |
32
|
|
|
/** Quote all - always quote all columns */ |
33
|
|
|
const QUOTE_ALL = 1; |
34
|
|
|
/** Quote minimal - quote only those columns that contain the delimiter or quote character */ |
35
|
|
|
const QUOTE_MINIMAL = 2; |
36
|
|
|
/** Quote non-numeric - quote only those columns that contain non-numeric values */ |
37
|
|
|
const QUOTE_NONNUMERIC = 3; |
38
|
|
|
|
39
|
|
|
/** Trim all - trim empty space from start and end */ |
40
|
|
|
const TRIM_ALL = true; |
41
|
|
|
/** Trim none - do not trim at all */ |
42
|
|
|
const TRIM_NONE = false; |
43
|
|
|
/** Trim start - trim empty space from the start (left) */ |
44
|
|
|
const TRIM_START = 'start'; |
45
|
|
|
/** Trim end - trim empty space from the end (right) */ |
46
|
|
|
const TRIM_END = 'end'; |
47
|
|
|
|
48
|
|
|
/** Standard attributes (from W3 CSVW working group) */ |
49
|
|
|
|
50
|
|
|
/** @var string The character to use to begin a comment line */ |
51
|
|
|
protected $commentPrefix = "#"; |
52
|
|
|
|
53
|
|
|
/** @var string The character to delimit columns with */ |
54
|
|
|
protected $delimiter = ","; |
55
|
|
|
|
56
|
|
|
/** @var bool Whether to escape quotes within a column by preceding them with another quote */ |
57
|
|
|
protected $doubleQuote = true; |
58
|
|
|
|
59
|
|
|
/** @var string The character encoding for this dialect */ |
60
|
|
|
protected $encoding = "utf-8"; |
61
|
|
|
|
62
|
|
|
/** @var bool Whether the dialect expects a header row (or rows) within the data */ |
63
|
|
|
protected $header = true; |
64
|
|
|
|
65
|
|
|
/** @var int How many header rows are expected within the data */ |
66
|
|
|
protected $headerRowCount = 1; |
67
|
|
|
|
68
|
|
|
/** @var string The line ending character or character sequence */ |
69
|
|
|
protected $lineTerminator = "\n"; |
70
|
|
|
|
71
|
|
|
/** @var string The quoting character (used to quote columns depending on quoteStyle) */ |
72
|
|
|
protected $quoteChar = '"'; |
73
|
|
|
|
74
|
|
|
/** @var bool Whether blank rows within the data should be skipped/ignored */ |
75
|
|
|
protected $skipBlankRows = false; |
76
|
|
|
|
77
|
|
|
/** @var int How many columns to skip/ignore */ |
78
|
|
|
protected $skipColumns = 0; |
79
|
|
|
|
80
|
|
|
/** @var bool Whether to skip/ignore initial space within a column */ |
81
|
|
|
protected $skipInitialSpace = false; |
82
|
|
|
|
83
|
|
|
/** @var int How many rows to skip/ignore */ |
84
|
|
|
protected $skipRows = 0; |
85
|
|
|
|
86
|
|
|
/** @var bool|string Whether to trim empty space and where (see TRIM_* constants above) */ |
87
|
|
|
protected $trim = self::TRIM_ALL; |
88
|
|
|
|
89
|
|
|
/** Non-standard attributes (my own additions) */ |
90
|
|
|
|
91
|
|
|
/** @var int The quoting style (see QUOTE_* constants above) */ |
92
|
|
|
protected $quoteStyle = self::QUOTE_MINIMAL; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Dialect constructor. |
96
|
|
|
* |
97
|
|
|
* Any of the above properties may be set within the $attribs array to initialize the dialect with different |
98
|
|
|
* attributes than are defined above. |
99
|
|
|
* |
100
|
|
|
* @param array|Traversable $attribs An array of dialect attributes |
101
|
|
|
*/ |
102
|
22 |
|
public function __construct($attribs = null) |
103
|
|
|
{ |
104
|
22 |
|
$attribs = to_array($attribs, true); |
105
|
22 |
|
foreach ($attribs as $attr => $val) { |
106
|
12 |
|
if (property_exists($this, $attr)) { |
107
|
|
|
// find the appropriate setter... |
108
|
12 |
|
foreach (['set', 'setIs', 'setHas'] as $prefix) { |
109
|
12 |
|
$setter = $prefix . ucfirst(strtolower($attr)); |
110
|
12 |
|
if (method_exists($this, $setter)) { |
111
|
12 |
|
$this->{$setter}($val); |
112
|
12 |
|
} |
113
|
12 |
|
} |
114
|
12 |
|
} |
115
|
22 |
|
} |
116
|
22 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set comment prefix character(s) |
120
|
|
|
* |
121
|
|
|
* @param string $commentPrefix The character(s) used to begin a comment |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
2 |
|
public function setCommentPrefix($commentPrefix) |
126
|
|
|
{ |
127
|
2 |
|
$this->commentPrefix = (string) $commentPrefix; |
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get comment prefix character(s) |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
3 |
|
public function getCommentPrefix() |
137
|
|
|
{ |
138
|
3 |
|
return $this->commentPrefix; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set delimiter character(s) |
143
|
|
|
* |
144
|
|
|
* @param string $delimiter The character(s) used to delimit data |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
4 |
|
public function setDelimiter($delimiter) |
149
|
|
|
{ |
150
|
4 |
|
$this->delimiter = (string) $delimiter; |
151
|
4 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set delimiter character(s) |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
21 |
|
public function getDelimiter() |
160
|
|
|
{ |
161
|
21 |
|
return $this->delimiter; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set double quote |
166
|
|
|
* |
167
|
|
|
* @param bool $doubleQuote Whether to escape quote character with a preceding quote character |
168
|
|
|
* |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
2 |
|
public function setIsDoubleQuote($doubleQuote) |
172
|
|
|
{ |
173
|
2 |
|
$this->doubleQuote = (bool) $doubleQuote; |
174
|
2 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get double quote |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
20 |
|
public function isDoubleQuote() |
183
|
|
|
{ |
184
|
20 |
|
return $this->doubleQuote; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set character encoding |
189
|
|
|
* |
190
|
|
|
* @param string $encoding The character encoding |
191
|
|
|
* |
192
|
|
|
* @return self |
193
|
|
|
*/ |
194
|
2 |
|
public function setEncoding($encoding) |
195
|
|
|
{ |
196
|
2 |
|
$this->encoding = (string) $encoding; |
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get character encoding |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
3 |
|
public function getEncoding() |
206
|
|
|
{ |
207
|
3 |
|
return $this->encoding; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set header row flag |
212
|
|
|
* |
213
|
|
|
* @param bool $header Whether the data has header row(s) |
214
|
|
|
* |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
12 |
|
public function setHasHeader($header) |
218
|
|
|
{ |
219
|
12 |
|
$this->header = (bool) $header; |
220
|
12 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get whether dialect expects header row(s) |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
18 |
|
public function hasHeader() |
229
|
|
|
{ |
230
|
18 |
|
return $this->header; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set header row count |
235
|
|
|
* |
236
|
|
|
* @param int $headerRowCount The amount of expected header rows |
237
|
|
|
* |
238
|
|
|
* @return self |
239
|
|
|
*/ |
240
|
2 |
|
public function setHeaderRowCount($headerRowCount) |
241
|
|
|
{ |
242
|
2 |
|
$this->headerRowCount = (int) $headerRowCount; |
243
|
2 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get header row count |
248
|
|
|
* |
249
|
|
|
* @return int |
250
|
|
|
*/ |
251
|
2 |
|
public function getHeaderRowCount() |
252
|
|
|
{ |
253
|
2 |
|
return $this->headerRowCount; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set line terminator character or character sequence |
258
|
|
|
* |
259
|
|
|
* @param string $lineTerminator The line ending character(s) |
260
|
|
|
* |
261
|
|
|
* @return self |
262
|
|
|
*/ |
263
|
2 |
|
public function setLineTerminator($lineTerminator) |
264
|
|
|
{ |
265
|
2 |
|
$this->lineTerminator = (string) $lineTerminator; |
266
|
2 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get line terminator |
271
|
|
|
* |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
20 |
|
public function getLineTerminator() |
275
|
|
|
{ |
276
|
20 |
|
return $this->lineTerminator; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Set quote character |
281
|
|
|
* |
282
|
|
|
* @param string $quoteChar The quote character |
283
|
|
|
* |
284
|
|
|
* @return self |
285
|
|
|
*/ |
286
|
2 |
|
public function setQuoteChar($quoteChar) |
287
|
|
|
{ |
288
|
2 |
|
$this->quoteChar = (string) $quoteChar; |
289
|
2 |
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get quote character |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
20 |
|
public function getQuoteChar() |
298
|
|
|
{ |
299
|
20 |
|
return $this->quoteChar; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set whether to skip blank rows |
304
|
|
|
* |
305
|
|
|
* @param bool $skipBlankRows Whether to skip blank rows |
306
|
|
|
* |
307
|
|
|
* @return self |
308
|
|
|
*/ |
309
|
2 |
|
public function setIsSkipBlankRows($skipBlankRows) |
310
|
|
|
{ |
311
|
2 |
|
$this->skipBlankRows = (bool) $skipBlankRows; |
312
|
2 |
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Get skip blank rows flag |
317
|
|
|
* |
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
3 |
|
public function isSkipBlankRows() |
321
|
|
|
{ |
322
|
3 |
|
return $this->skipBlankRows; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Set number of columns to skip/ignore |
327
|
|
|
* |
328
|
|
|
* @param int $skipColumns The number of columns to skip/ignore |
329
|
|
|
* |
330
|
|
|
* @return self |
331
|
|
|
*/ |
332
|
2 |
|
public function setSkipColumns($skipColumns) |
333
|
|
|
{ |
334
|
2 |
|
$this->skipColumns = (int) $skipColumns; |
335
|
2 |
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get number of columns to skip/ignore |
340
|
|
|
* |
341
|
|
|
* @return int |
342
|
|
|
*/ |
343
|
3 |
|
public function getSkipColumns() |
344
|
|
|
{ |
345
|
3 |
|
return $this->skipColumns; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Set skip initial space flag |
350
|
|
|
* |
351
|
|
|
* @param bool $skipInitialSpace Skip initial space flag |
352
|
|
|
* |
353
|
|
|
* @return self |
354
|
|
|
*/ |
355
|
2 |
|
public function setIsSkipInitialSpace($skipInitialSpace) |
356
|
|
|
{ |
357
|
2 |
|
$this->skipInitialSpace = (bool) $skipInitialSpace; |
358
|
2 |
|
return $this; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Get skip initial space flag |
363
|
|
|
* |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
3 |
|
public function isSkipInitialSpace() |
367
|
|
|
{ |
368
|
3 |
|
return $this->skipInitialSpace; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Set number of rows to skip/ignore |
373
|
|
|
* |
374
|
|
|
* @param int $skipRows Number of rows to skip/ignore |
375
|
|
|
* |
376
|
|
|
* @return self |
377
|
|
|
*/ |
378
|
2 |
|
public function setSkipRows($skipRows) |
379
|
|
|
{ |
380
|
2 |
|
$this->skipRows = (int) $skipRows; |
381
|
2 |
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Get number of rows to skip/ignore |
386
|
|
|
* |
387
|
|
|
* @return int |
388
|
|
|
*/ |
389
|
3 |
|
public function getSkipRows() |
390
|
|
|
{ |
391
|
3 |
|
return $this->skipRows; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Set trim type |
396
|
|
|
* |
397
|
|
|
* Allows you to set whether you want data to be trimmed on one, both, or neither sides. |
398
|
|
|
* |
399
|
|
|
* @param bool|string $trim The type trimming you want to do (see TRIM_* constants above) |
400
|
|
|
* |
401
|
|
|
* @return self |
402
|
|
|
*/ |
403
|
2 |
|
public function setTrim($trim) |
404
|
|
|
{ |
405
|
2 |
|
$this->trim = $trim; |
406
|
2 |
|
return $this; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Get trim type |
411
|
|
|
* |
412
|
|
|
* The type will coincide with one of the TRIM_* constants defined above. |
413
|
|
|
* |
414
|
|
|
* @return bool|string |
415
|
|
|
*/ |
416
|
3 |
|
public function getTrim() |
417
|
|
|
{ |
418
|
3 |
|
return $this->trim; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Set the quoting style |
423
|
|
|
* |
424
|
|
|
* Allows you to set how data is quoted |
425
|
|
|
* |
426
|
|
|
* @param int $quoteStyle The desired quoting style (see QUOTE_* constants above) |
427
|
|
|
* |
428
|
|
|
* @return self |
429
|
|
|
*/ |
430
|
2 |
|
public function setQuoteStyle($quoteStyle) |
431
|
|
|
{ |
432
|
2 |
|
$this->quoteStyle = (int) $quoteStyle; |
433
|
2 |
|
return $this; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get quoting style |
438
|
|
|
* |
439
|
|
|
* The quoteStyle value will coincide with one of the QUOTE_* constants defined above. |
440
|
|
|
* |
441
|
|
|
* @return int |
442
|
|
|
*/ |
443
|
5 |
|
public function getQuoteStyle() |
444
|
|
|
{ |
445
|
5 |
|
return $this->quoteStyle; |
446
|
|
|
} |
447
|
|
|
} |