1 | <?php |
||
26 | class Dialect |
||
27 | { |
||
28 | /** Quote none - never quote any columns */ |
||
29 | const QUOTE_NONE = 0; |
||
30 | /** Quote all - always quote all columns */ |
||
31 | const QUOTE_ALL = 1; |
||
32 | /** Quote minimal - quote only those columns that contain the delimiter or quote character */ |
||
33 | const QUOTE_MINIMAL = 2; |
||
34 | /** Quote non-numeric - quote only those columns that contain non-numeric values */ |
||
35 | const QUOTE_NONNUMERIC = 3; |
||
36 | |||
37 | /** Trim all - trim empty space from start and end */ |
||
38 | const TRIM_ALL = true; |
||
39 | /** Trim none - do not trim at all */ |
||
40 | const TRIM_NONE = false; |
||
41 | /** Trim start - trim empty space from the start (left) */ |
||
42 | const TRIM_START = 'start'; |
||
43 | /** Trim end - trim empty space from the end (right) */ |
||
44 | const TRIM_END = 'end'; |
||
45 | |||
46 | /** Standard attributes (from W3 CSVW working group) */ |
||
47 | |||
48 | /** @var string The character to use to begin a comment line */ |
||
49 | protected $commentPrefix = "#"; |
||
50 | |||
51 | /** @var string The character to delimit columns with */ |
||
52 | protected $delimiter = ","; |
||
53 | |||
54 | /** @var bool Whether to escape quotes within a column by preceding them with another quote */ |
||
55 | protected $doubleQuote = true; |
||
56 | |||
57 | /** @var string The character encoding for this dialect */ |
||
58 | protected $encoding = "utf-8"; |
||
59 | |||
60 | /** @var bool Whether the dialect expects a header row (or rows) within the data */ |
||
61 | protected $header = true; |
||
62 | |||
63 | /** @var int How many header rows are expected within the data */ |
||
64 | protected $headerRowCount = 1; |
||
65 | |||
66 | /** @var string The line ending character or character sequence */ |
||
67 | protected $lineTerminator = "\n"; |
||
68 | |||
69 | /** @var string The quoting character (used to quote columns depending on quoteStyle) */ |
||
70 | protected $quoteChar = '"'; |
||
71 | |||
72 | /** @var bool Whether blank rows within the data should be skipped/ignored */ |
||
73 | protected $skipBlankRows = false; |
||
74 | |||
75 | /** @var int How many columns to skip/ignore */ |
||
76 | protected $skipColumns = 0; |
||
77 | |||
78 | /** @var bool Whether to skip/ignore initial space within a column */ |
||
79 | protected $skipInitialSpace = false; |
||
80 | |||
81 | /** @var int How many rows to skip/ignore */ |
||
82 | protected $skipRows = 0; |
||
83 | |||
84 | /** @var bool|string Whether to trim empty space and where (see TRIM_* constants above) */ |
||
85 | protected $trim = self::TRIM_ALL; |
||
86 | |||
87 | /** Non-standard attributes (my own additions) */ |
||
88 | |||
89 | /** @var int The quoting style (see QUOTE_* constants above) */ |
||
90 | protected $quoteStyle = self::QUOTE_MINIMAL; |
||
91 | |||
92 | /** |
||
93 | * Dialect constructor. |
||
94 | * |
||
95 | * Any of the above properties may be set within the $attribs array to initialize the dialect with different |
||
96 | * attributes than are defined above. |
||
97 | * |
||
98 | * @param array|Traversable $attribs An array of dialect attributes |
||
99 | */ |
||
100 | 14 | public function __construct($attribs = null) |
|
115 | |||
116 | /** |
||
117 | * Set comment prefix character(s) |
||
118 | * |
||
119 | * @param string $commentPrefix The character(s) used to begin a comment |
||
120 | * |
||
121 | * @return self |
||
122 | */ |
||
123 | 2 | public function setCommentPrefix($commentPrefix) |
|
128 | |||
129 | /** |
||
130 | * Get comment prefix character(s) |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 3 | public function getCommentPrefix() |
|
138 | |||
139 | /** |
||
140 | * Set delimiter character(s) |
||
141 | * |
||
142 | * @param string $delimiter The character(s) used to delimit data |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | 2 | public function setDelimiter($delimiter) |
|
151 | |||
152 | /** |
||
153 | * Set delimiter character(s) |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 14 | public function getDelimiter() |
|
161 | |||
162 | /** |
||
163 | * Set double quote |
||
164 | * |
||
165 | * @param bool $doubleQuote Whether to escape quote character with a preceding quote character |
||
166 | * |
||
167 | * @return self |
||
168 | */ |
||
169 | 2 | public function setIsDoubleQuote($doubleQuote) |
|
174 | |||
175 | /** |
||
176 | * Get double quote |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 14 | public function isDoubleQuote() |
|
184 | |||
185 | /** |
||
186 | * Set character encoding |
||
187 | * |
||
188 | * @param string $encoding The character encoding |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | 2 | public function setEncoding($encoding) |
|
197 | |||
198 | /** |
||
199 | * Get character encoding |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 3 | public function getEncoding() |
|
207 | |||
208 | /** |
||
209 | * Set header row flag |
||
210 | * |
||
211 | * @param bool $header Whether the data has header row(s) |
||
212 | * |
||
213 | * @return self |
||
214 | */ |
||
215 | 9 | public function setHasHeader($header) |
|
220 | |||
221 | /** |
||
222 | * Get whether dialect expects header row(s) |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 14 | public function hasHeader() |
|
230 | |||
231 | /** |
||
232 | * Set header row count |
||
233 | * |
||
234 | * @param int $headerRowCount The amount of expected header rows |
||
235 | * |
||
236 | * @return self |
||
237 | */ |
||
238 | 2 | public function setHeaderRowCount($headerRowCount) |
|
243 | |||
244 | /** |
||
245 | * Get header row count |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | 2 | public function getHeaderRowCount() |
|
253 | |||
254 | /** |
||
255 | * Set line terminator character or character sequence |
||
256 | * |
||
257 | * @param string $lineTerminator The line ending character(s) |
||
258 | * |
||
259 | * @return self |
||
260 | */ |
||
261 | 2 | public function setLineTerminator($lineTerminator) |
|
262 | { |
||
263 | 2 | $this->lineTerminator = (string) $lineTerminator; |
|
264 | 2 | return $this; |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get line terminator |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | 14 | public function getLineTerminator() |
|
273 | { |
||
274 | 14 | return $this->lineTerminator; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Set quote character |
||
279 | * |
||
280 | * @param string $quoteChar The quote character |
||
281 | * |
||
282 | * @return self |
||
283 | */ |
||
284 | 2 | public function setQuoteChar($quoteChar) |
|
289 | |||
290 | /** |
||
291 | * Get quote character |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 14 | public function getQuoteChar() |
|
299 | |||
300 | /** |
||
301 | * Set whether to skip blank rows |
||
302 | * |
||
303 | * @param bool $skipBlankRows Whether to skip blank rows |
||
304 | * |
||
305 | * @return self |
||
306 | */ |
||
307 | 2 | public function setIsSkipBlankRows($skipBlankRows) |
|
312 | |||
313 | /** |
||
314 | * Get skip blank rows flag |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | 3 | public function isSkipBlankRows() |
|
322 | |||
323 | /** |
||
324 | * Set number of columns to skip/ignore |
||
325 | * |
||
326 | * @param int $skipColumns The number of columns to skip/ignore |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | 2 | public function setSkipColumns($skipColumns) |
|
335 | |||
336 | /** |
||
337 | * Get number of columns to skip/ignore |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | 3 | public function getSkipColumns() |
|
345 | |||
346 | /** |
||
347 | * Set skip initial space flag |
||
348 | * |
||
349 | * @param bool $skipInitialSpace Skip initial space flag |
||
350 | * |
||
351 | * @return self |
||
352 | */ |
||
353 | 2 | public function setIsSkipInitialSpace($skipInitialSpace) |
|
358 | |||
359 | /** |
||
360 | * Get skip initial space flag |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | 3 | public function isSkipInitialSpace() |
|
368 | |||
369 | /** |
||
370 | * Set number of rows to skip/ignore |
||
371 | * |
||
372 | * @param int $skipRows Number of rows to skip/ignore |
||
373 | * |
||
374 | * @return self |
||
375 | */ |
||
376 | 2 | public function setSkipRows($skipRows) |
|
381 | |||
382 | /** |
||
383 | * Get number of rows to skip/ignore |
||
384 | * |
||
385 | * @return int |
||
386 | */ |
||
387 | 3 | public function getSkipRows() |
|
391 | |||
392 | /** |
||
393 | * Set trim type |
||
394 | * |
||
395 | * Allows you to set whether you want data to be trimmed on one, both, or neither sides. |
||
396 | * |
||
397 | * @param bool|string $trim The type trimming you want to do (see TRIM_* constants above) |
||
398 | * |
||
399 | * @return self |
||
400 | */ |
||
401 | 2 | public function setTrim($trim) |
|
406 | |||
407 | /** |
||
408 | * Get trim type |
||
409 | * |
||
410 | * The type will coincide with one of the TRIM_* constants defined above. |
||
411 | * |
||
412 | * @return bool|string |
||
413 | */ |
||
414 | 3 | public function getTrim() |
|
418 | |||
419 | /** |
||
420 | * Set the quoting style |
||
421 | * |
||
422 | * Allows you to set how data is quoted |
||
423 | * |
||
424 | * @param int $quoteStyle The desired quoting style (see QUOTE_* constants above) |
||
425 | * |
||
426 | * @return self |
||
427 | */ |
||
428 | 2 | public function setQuoteStyle($quoteStyle) |
|
433 | |||
434 | /** |
||
435 | * Get quoting style |
||
436 | * |
||
437 | * The quoteStyle value will coincide with one of the QUOTE_* constants defined above. |
||
438 | * |
||
439 | * @return int |
||
440 | */ |
||
441 | 3 | public function getQuoteStyle() |
|
445 | } |