Completed
Push — master ( d97897...85851c )
by Harry
03:38
created

CsvFormat::setEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format;
15
16
use Graze\CsvToken\Csv\Bom;
17
use Graze\DataFile\Helper\GetOptionTrait;
18
19
class CsvFormat implements CsvFormatInterface
20
{
21
    use GetOptionTrait;
22
23
    const DEFAULT_DELIMITER       = ',';
24
    const DEFAULT_NULL_OUTPUT     = '\\N';
25
    const DEFAULT_HEADER_ROW      = -1;
26
    const DEFAULT_DATA_START      = 1;
27
    const DEFAULT_LINE_TERMINATOR = "\n";
28
    const DEFAULT_QUOTE_CHARACTER = '"';
29
    const DEFAULT_ESCAPE          = '\\';
30
    const DEFAULT_LIMIT           = -1;
31
    const DEFAULT_DOUBLE_QUOTE    = false;
32
    const DEFAULT_BOM             = null;
33
    const DEFAULT_ENCODING        = 'UTF-8';
34
35
    const OPTION_DELIMITER       = 'delimiter';
36
    const OPTION_NULL_OUTPUT     = 'nullOutput';
37
    const OPTION_HEADER_ROW      = 'headerRow';
38
    const OPTION_DATA_START      = 'dataStart';
39
    const OPTION_LINE_TERMINATOR = 'lineTerminator';
40
    const OPTION_QUOTE_CHARACTER = 'quoteCharacter';
41
    const OPTION_ESCAPE          = 'escape';
42
    const OPTION_LIMIT           = 'limit';
43
    const OPTION_DOUBLE_QUOTE    = 'doubleQuote';
44
    const OPTION_BOM             = 'bom';
45
    const OPTION_ENCODING        = 'encoding';
46
47
    /** @var string */
48
    protected $delimiter;
49
    /** @var string */
50
    protected $quoteCharacter;
51
    /** @var string */
52
    protected $nullOutput;
53
    /** @var int */
54
    protected $headerRow;
55
    /** @var string */
56
    protected $lineTerminator;
57
    /** @var bool */
58
    protected $nullQuotes;
59
    /** @var string */
60
    protected $escape;
61
    /** @var int */
62
    protected $limit;
63
    /** @var bool */
64
    protected $doubleQuote;
65
    /** @var int */
66
    protected $dataStart;
67
    /** @var string|null */
68
    protected $bom;
69
    /** @var string */
70
    protected $encoding;
71
72
    /**
73
     * @param array $options -delimiter <string> (Default: ,) Character to use between fields
74
     *                       -quoteCharacter <string> (Default: ")
75
     *                       -nullOutput <string> (Default: \N)
76
     *                       -headerRow <int> (Default: -1) -1 for no header row. (1 is the first line of the file)
77
     *                       -dataStart <int> (Default: 1) The line where the data starts (1 is the first list of the
78
     *                       file)
79
     *                       -lineTerminator <string> (Default: \n)
80
     *                       -escape <string> (Default: \\) Character to use for escaping
81
     *                       -limit <int> Total number of data rows to return
82
     *                       -doubleQuote <bool> instances of quote in fields are indicated by a double quote
83
     *                       -bom <string> (Default: null) Specify a ByteOrderMark for this file
84
     *                       -encoding <string> (Default: UTF-8) Specify the encoding of the csv file
85
     */
86 16
    public function __construct(array $options = [])
87
    {
88 16
        $this->options = $options;
89 16
        $this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
90 16
        $this->quoteCharacter = $this->getOption(static::OPTION_QUOTE_CHARACTER, static::DEFAULT_QUOTE_CHARACTER);
91 16
        $this->nullOutput = $this->getOption(static::OPTION_NULL_OUTPUT, static::DEFAULT_NULL_OUTPUT);
92 16
        $this->headerRow = $this->getOption(static::OPTION_HEADER_ROW, static::DEFAULT_HEADER_ROW);
93 16
        $this->dataStart = $this->getOption(static::OPTION_DATA_START, static::DEFAULT_DATA_START);
94 16
        $this->lineTerminator = $this->getOption(static::OPTION_LINE_TERMINATOR, static::DEFAULT_LINE_TERMINATOR);
95 16
        $this->escape = $this->getOption(static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
96 16
        $this->limit = $this->getOption(static::OPTION_LIMIT, static::DEFAULT_LIMIT);
97 16
        $this->doubleQuote = $this->getOption(static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
98 16
        $this->setBom($this->getOption(static::OPTION_BOM, static::DEFAULT_BOM));
99 16
        $this->encoding = $this->getOption(static::OPTION_ENCODING, static::DEFAULT_ENCODING);
100 16
    }
101
102
    /**
103
     * @return string
104
     */
105 27
    public function getDelimiter()
106
    {
107 27
        return $this->delimiter;
108
    }
109
110
    /**
111
     * @param string $delimiter
112
     *
113
     * @return static
114
     */
115 2
    public function setDelimiter($delimiter)
116
    {
117 2
        $this->delimiter = $delimiter;
118 2
        return $this;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124 18
    public function hasQuotes()
125
    {
126 18
        return $this->quoteCharacter <> '';
127
    }
128
129
    /**
130
     * @return string
131
     */
132 16
    public function getNullOutput()
133
    {
134 16
        return $this->nullOutput;
135
    }
136
137
    /**
138
     * @param string $nullOutput
139
     *
140
     * @return static
141
     */
142 1
    public function setNullOutput($nullOutput)
143
    {
144 1
        $this->nullOutput = $nullOutput;
145 1
        return $this;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151 13
    public function hasHeaderRow()
152
    {
153 13
        return $this->headerRow > 0;
154
    }
155
156
    /**
157
     * @param int $headerRow
158
     *
159
     * @return static
160
     */
161 2
    public function setHeaderRow($headerRow)
162
    {
163 2
        $this->headerRow = $headerRow;
164 2
        return $this;
165
    }
166
167
    /**
168
     * @return int
169
     */
170 7
    public function getHeaderRow()
171
    {
172 7
        return $this->headerRow;
173
    }
174
175
    /**
176
     * @param int $row
177
     *
178
     * @return static
179
     */
180 2
    public function setDataStart($row)
181
    {
182 2
        $this->dataStart = $row;
183 2
        return $this;
184
    }
185
186
    /**
187
     * @return int
188
     */
189 13
    public function getDataStart()
190
    {
191 13
        if ($this->hasHeaderRow() && $this->getHeaderRow() >= $this->dataStart) {
192 4
            return max(1, $this->getHeaderRow() + 1);
193
        }
194 10
        return max(1, $this->dataStart);
195
    }
196
197
    /**
198
     * @return string
199
     */
200 16
    public function getLineTerminator()
201
    {
202 16
        return $this->lineTerminator;
203
    }
204
205
    /**
206
     * @param string $lineTerminator
207
     *
208
     * @return static
209
     */
210 1
    public function setLineTerminator($lineTerminator)
211
    {
212 1
        $this->lineTerminator = $lineTerminator;
213 1
        return $this;
214
    }
215
216
    /**
217
     * @note Csv Rfc spec defines escaping of quotes to be done using double quotes `""`
218
     *
219
     * @return string
220
     */
221 26
    public function getQuoteCharacter()
222
    {
223 26
        return $this->quoteCharacter;
224
    }
225
226
    /**
227
     * @param string $quoteCharacter
228
     *
229
     * @return static
230
     */
231 1
    public function setQuoteCharacter($quoteCharacter)
232
    {
233 1
        $this->quoteCharacter = $quoteCharacter;
234 1
        return $this;
235
    }
236
237
    /**
238
     * Type type of file format (defined in FileFormatType::)
239
     *
240
     * @return string
241
     */
242 3
    public function getType()
243
    {
244 3
        return 'csv';
245
    }
246
247
    /**
248
     * @return string
249
     */
250 27
    public function getEscapeCharacter()
251
    {
252 27
        return $this->escape;
253
    }
254
255
    /**
256
     * @param string $escape
257
     *
258
     * @return static
259
     */
260 1
    public function setEscapeCharacter($escape)
261
    {
262 1
        $this->escape = $escape;
263 1
        return $this;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269 3
    public function hasEscapeCharacter()
270
    {
271 3
        return $this->escape !== '';
272
    }
273
274
    /**
275
     * Get the limit that should be returned (-1 for no limit)
276
     *
277
     * @return int
278
     */
279 12
    public function getLimit()
280
    {
281 12
        return $this->limit;
282
    }
283
284
    /**
285
     * Set the limit of the number of items to be returned (-1 for not limit)
286
     *
287
     * @param int $limit
288
     *
289
     * @return static
290
     */
291 1
    public function setLimit($limit)
292
    {
293 1
        $this->limit = $limit;
294 1
        return $this;
295
    }
296
297
    /**
298
     * @return bool
299
     */
300 24
    public function isDoubleQuote()
301
    {
302 24
        return $this->doubleQuote;
303
    }
304
305
    /**
306
     * @param bool $doubleQuote
307
     *
308
     * @return static
309
     */
310 1
    public function setDoubleQuote($doubleQuote)
311
    {
312 1
        $this->doubleQuote = $doubleQuote;
313 1
        return $this;
314
    }
315
316
    /**
317
     * @return null|string
318
     */
319 27
    public function getBom()
320
    {
321 27
        return $this->bom;
322
    }
323
324
    /**
325
     * @param null|string $bom
326
     *
327
     * @return static
328
     */
329 16
    public function setBom($bom)
330
    {
331 16
        $this->bom = $bom;
332 16
        if (!is_null($bom)) {
333 4
            Bom::getEncoding($bom);
334
        }
335 16
        return $this;
336
    }
337
338
    /**
339
     * @return string
340
     */
341 24
    public function getEncoding()
342
    {
343 24
        if (!is_null($this->bom)) {
344 3
            return Bom::getEncoding($this->bom);
345
        }
346 22
        return $this->encoding;
347
    }
348
349
    /**
350
     * @param string $encoding
351
     *
352
     * @return static
353
     */
354 1
    public function setEncoding($encoding)
355
    {
356 1
        $this->encoding = $encoding;
357 1
        return $this;
358
    }
359
}
360