1 | <?php |
||
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) |
|
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) |
|
130 | |||
131 | /** |
||
132 | * Get comment prefix character(s) |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 3 | public function getCommentPrefix() |
|
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) |
|
153 | |||
154 | /** |
||
155 | * Set delimiter character(s) |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | 21 | public function getDelimiter() |
|
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) |
|
176 | |||
177 | /** |
||
178 | * Get double quote |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 20 | public function isDoubleQuote() |
|
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) |
|
199 | |||
200 | /** |
||
201 | * Get character encoding |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 3 | public function getEncoding() |
|
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) |
|
222 | |||
223 | /** |
||
224 | * Get whether dialect expects header row(s) |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | 18 | public function hasHeader() |
|
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) |
|
245 | |||
246 | /** |
||
247 | * Get header row count |
||
248 | * |
||
249 | * @return int |
||
250 | */ |
||
251 | 2 | public function getHeaderRowCount() |
|
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) |
|
268 | |||
269 | /** |
||
270 | * Get line terminator |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | 20 | public function getLineTerminator() |
|
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) |
|
291 | |||
292 | /** |
||
293 | * Get quote character |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 20 | public function getQuoteChar() |
|
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) |
|
314 | |||
315 | /** |
||
316 | * Get skip blank rows flag |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | 3 | public function isSkipBlankRows() |
|
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) |
|
337 | |||
338 | /** |
||
339 | * Get number of columns to skip/ignore |
||
340 | * |
||
341 | * @return int |
||
342 | */ |
||
343 | 3 | public function getSkipColumns() |
|
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) |
|
360 | |||
361 | /** |
||
362 | * Get skip initial space flag |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | 3 | public function isSkipInitialSpace() |
|
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) |
|
383 | |||
384 | /** |
||
385 | * Get number of rows to skip/ignore |
||
386 | * |
||
387 | * @return int |
||
388 | */ |
||
389 | 3 | public function getSkipRows() |
|
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) |
|
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() |
|
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) |
|
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() |
|
447 | } |