Completed
Pull Request — master (#183)
by Luke
09:18 queued 06:34
created

Dialect::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 15
ccs 13
cts 13
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0
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
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)
101
    {
102 14
        $attribs = to_array($attribs, true);
103 14
        foreach ($attribs as $attr => $val) {
104 8
            if (property_exists($this, $attr)) {
105
                // find the appropriate setter...
106 8
                foreach (['set', 'setIs', 'setHas'] as $prefix) {
107 8
                    $setter = $prefix . ucfirst(strtolower($attr));
108 8
                    if (method_exists($this, $setter)) {
109 8
                        $this->{$setter}($val);
110 8
                    }
111 8
                }
112 8
            }
113 14
        }
114 14
    }
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)
124
    {
125 2
        $this->commentPrefix = (string) $commentPrefix;
126 2
        return $this;
127
    }
128
129
    /**
130
     * Get comment prefix character(s)
131
     *
132
     * @return string
133
     */
134 3
    public function getCommentPrefix()
135
    {
136 3
        return $this->commentPrefix;
137
    }
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)
147
    {
148 2
        $this->delimiter = (string) $delimiter;
149 2
        return $this;
150
    }
151
152
    /**
153
     * Set delimiter character(s)
154
     *
155
     * @return string
156
     */
157 14
    public function getDelimiter()
158
    {
159 14
        return $this->delimiter;
160
    }
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)
170
    {
171 2
        $this->doubleQuote = (bool) $doubleQuote;
172 2
        return $this;
173
    }
174
175
    /**
176
     * Get double quote
177
     *
178
     * @return bool
179
     */
180 14
    public function isDoubleQuote()
181
    {
182 14
        return $this->doubleQuote;
183
    }
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)
193
    {
194 2
        $this->encoding = (string) $encoding;
195 2
        return $this;
196
    }
197
198
    /**
199
     * Get character encoding
200
     *
201
     * @return string
202
     */
203 3
    public function getEncoding()
204
    {
205 3
        return $this->encoding;
206
    }
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)
216
    {
217 9
        $this->header = (bool) $header;
218 9
        return $this;
219
    }
220
221
    /**
222
     * Get whether dialect expects header row(s)
223
     *
224
     * @return bool
225
     */
226 14
    public function hasHeader()
227
    {
228 14
        return $this->header;
229
    }
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)
239
    {
240 2
        $this->headerRowCount = (int) $headerRowCount;
241 2
        return $this;
242
    }
243
244
    /**
245
     * Get header row count
246
     *
247
     * @return int
248
     */
249 2
    public function getHeaderRowCount()
250
    {
251 2
        return $this->headerRowCount;
252
    }
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)
285
    {
286 2
        $this->quoteChar = (string) $quoteChar;
287 2
        return $this;
288
    }
289
290
    /**
291
     * Get quote character
292
     *
293
     * @return string
294
     */
295 14
    public function getQuoteChar()
296
    {
297 14
        return $this->quoteChar;
298
    }
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)
308
    {
309 2
        $this->skipBlankRows = (bool) $skipBlankRows;
310 2
        return $this;
311
    }
312
313
    /**
314
     * Get skip blank rows flag
315
     *
316
     * @return bool
317
     */
318 3
    public function isSkipBlankRows()
319
    {
320 3
        return $this->skipBlankRows;
321
    }
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)
331
    {
332 2
        $this->skipColumns = (int) $skipColumns;
333 2
        return $this;
334
    }
335
336
    /**
337
     * Get number of columns to skip/ignore
338
     *
339
     * @return int
340
     */
341 3
    public function getSkipColumns()
342
    {
343 3
        return $this->skipColumns;
344
    }
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)
354
    {
355 2
        $this->skipInitialSpace = (bool) $skipInitialSpace;
356 2
        return $this;
357
    }
358
359
    /**
360
     * Get skip initial space flag
361
     *
362
     * @return bool
363
     */
364 3
    public function isSkipInitialSpace()
365
    {
366 3
        return $this->skipInitialSpace;
367
    }
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)
377
    {
378 2
        $this->skipRows = (int) $skipRows;
379 2
        return $this;
380
    }
381
382
    /**
383
     * Get number of rows to skip/ignore
384
     *
385
     * @return int
386
     */
387 3
    public function getSkipRows()
388
    {
389 3
        return $this->skipRows;
390
    }
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)
402
    {
403 2
        $this->trim = $trim;
404 2
        return $this;
405
    }
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()
415
    {
416 3
        return $this->trim;
417
    }
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)
429
    {
430 2
        $this->quoteStyle = (int) $quoteStyle;
431 2
        return $this;
432
    }
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()
442
    {
443 3
        return $this->quoteStyle;
444
    }
445
}