Completed
Pull Request — master (#6)
by Harry
07:18 queued 03:24
created

CsvFormat::getDataStart()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
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 16
    /** @var string|null */
68
    protected $bom;
69 16
    /** @var string */
70 16
    protected $encoding;
71 16
72 16
    /**
73 16
     * @param array $options -delimiter <string> (Default: ,) Character to use between fields
74 16
     *                       -quoteCharacter <string> (Default: ")
75 16
     *                       -nullOutput <string> (Default: \N)
76 16
     *                       -headerRow <int> (Default: -1) -1 for no header row. (1 is the first line of the file)
77 16
     *                       -dataStart <int> (Default: 1) The line where the data starts (1 is the first list of the
78 16
     *                       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 25
     *                       -bom <string> (Default: null) Specify a ByteOrderMark for this file
84
     *                       -encoding <string> (Default: UTF-8) Specify the encoding of the csv file
85 25
     */
86
    public function __construct(array $options = [])
87
    {
88
        $this->options = $options;
89
        $this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
90
        $this->quoteCharacter = $this->getOption(static::OPTION_QUOTE_CHARACTER, static::DEFAULT_QUOTE_CHARACTER);
91
        $this->nullOutput = $this->getOption(static::OPTION_NULL_OUTPUT, static::DEFAULT_NULL_OUTPUT);
92
        $this->headerRow = $this->getOption(static::OPTION_HEADER_ROW, static::DEFAULT_HEADER_ROW);
93 3
        $this->dataStart = $this->getOption(static::OPTION_DATA_START, static::DEFAULT_DATA_START);
94
        $this->lineTerminator = $this->getOption(static::OPTION_LINE_TERMINATOR, static::DEFAULT_LINE_TERMINATOR);
95 3
        $this->escape = $this->getOption(static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
96 3
        $this->limit = $this->getOption(static::OPTION_LIMIT, static::DEFAULT_LIMIT);
97
        $this->doubleQuote = $this->getOption(static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
98
        $this->setBom($this->getOption(static::OPTION_BOM, static::DEFAULT_BOM));
99
        $this->encoding = $this->getOption(static::OPTION_ENCODING, static::DEFAULT_ENCODING);
100
    }
101
102 20
    /**
103
     * @return string
104 20
     */
105
    public function getDelimiter()
106
    {
107
        return $this->delimiter;
108
    }
109
110 15
    /**
111
     * @param string $delimiter
112 15
     *
113
     * @return static
114
     */
115
    public function setDelimiter($delimiter)
116
    {
117
        $this->delimiter = $delimiter;
118
        return $this;
119
    }
120 2
121
    /**
122 2
     * @return bool
123 2
     */
124
    public function hasQuotes()
125
    {
126
        return $this->quoteCharacter <> '';
127
    }
128
129 11
    /**
130
     * @return string
131 11
     */
132
    public function getNullOutput()
133
    {
134
        return $this->nullOutput;
135
    }
136
137
    /**
138
     * @param string $nullOutput
139 2
     *
140
     * @return static
141 2
     */
142 2
    public function setNullOutput($nullOutput)
143
    {
144
        $this->nullOutput = $nullOutput;
145
        return $this;
146
    }
147
148 6
    /**
149
     * @return bool
150 6
     */
151
    public function hasHeaderRow()
152
    {
153
        return $this->headerRow > 0;
154
    }
155
156 15
    /**
157
     * @param int $headerRow
158 15
     *
159
     * @return static
160
     */
161
    public function setHeaderRow($headerRow)
162
    {
163
        $this->headerRow = $headerRow;
164
        return $this;
165
    }
166 2
167
    /**
168 2
     * @return int
169 2
     */
170
    public function getHeaderRow()
171
    {
172
        return $this->headerRow;
173
    }
174
175
    /**
176
     * @param int $row
177 24
     *
178
     * @return static
179 24
     */
180
    public function setDataStart($row)
181
    {
182
        $this->dataStart = $row;
183
        return $this;
184
    }
185
186
    /**
187 2
     * @return int
188
     */
189 2
    public function getDataStart()
190 2
    {
191
        if ($this->hasHeaderRow() && $this->getHeaderRow() >= $this->dataStart) {
192
            return max(1, $this->getHeaderRow() + 1);
193
        }
194
        return max(1, $this->dataStart);
195
    }
196
197
    /**
198 3
     * @return string
199
     */
200 3
    public function getLineTerminator()
201
    {
202
        return $this->lineTerminator;
203
    }
204
205
    /**
206 22
     * @param string $lineTerminator
207
     *
208 22
     * @return static
209
     */
210
    public function setLineTerminator($lineTerminator)
211
    {
212
        $this->lineTerminator = $lineTerminator;
213
        return $this;
214
    }
215
216 1
    /**
217
     * @note Csv Rfc spec defines escaping of quotes to be done using double quotes `""`
218 1
     *
219 1
     * @return string
220
     */
221
    public function getQuoteCharacter()
222
    {
223
        return $this->quoteCharacter;
224
    }
225 3
226
    /**
227 3
     * @param string $quoteCharacter
228
     *
229
     * @return static
230
     */
231
    public function setQuoteCharacter($quoteCharacter)
232
    {
233
        $this->quoteCharacter = $quoteCharacter;
234
        return $this;
235 8
    }
236
237 8
    /**
238
     * Type type of file format (defined in FileFormatType::)
239
     *
240
     * @return string
241
     */
242
    public function getType()
243
    {
244
        return 'csv';
245
    }
246
247 1
    /**
248
     * @return string
249 1
     */
250 1
    public function getEscapeCharacter()
251
    {
252
        return $this->escape;
253
    }
254
255
    /**
256 19
     * @param string $escape
257
     *
258 19
     * @return static
259
     */
260
    public function setEscapeCharacter($escape)
261
    {
262
        $this->escape = $escape;
263
        return $this;
264
    }
265
266 1
    /**
267
     * @return bool
268 1
     */
269 1
    public function hasEscapeCharacter()
270
    {
271
        return $this->escape !== '';
272
    }
273
274
    /**
275
     * Get the limit that should be returned (-1 for no limit)
276
     *
277
     * @return int
278
     */
279
    public function getLimit()
280
    {
281
        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
    public function setLimit($limit)
292
    {
293
        $this->limit = $limit;
294
        return $this;
295
    }
296
297
    /**
298
     * @return bool
299
     */
300
    public function isDoubleQuote()
301
    {
302
        return $this->doubleQuote;
303
    }
304
305
    /**
306
     * @param bool $doubleQuote
307
     *
308
     * @return static
309
     */
310
    public function setDoubleQuote($doubleQuote)
311
    {
312
        $this->doubleQuote = $doubleQuote;
313
        return $this;
314
    }
315
316
    /**
317
     * @return null|string
318
     */
319
    public function getBom()
320
    {
321
        return $this->bom;
322
    }
323
324
    /**
325
     * @param null|string $bom
326
     *
327
     * @return static
328
     */
329
    public function setBom($bom)
330
    {
331
        $this->bom = $bom;
332
        if (!is_null($bom)) {
333
            Bom::getEncoding($bom);
334
        }
335
        return $this;
336
    }
337
338
    /**
339
     * @return string
340
     */
341
    public function getEncoding()
342
    {
343
        if (!is_null($this->bom)) {
344
            return Bom::getEncoding($this->bom);
345
        }
346
        return $this->encoding;
347
    }
348
349
    /**
350
     * @param string $encoding
351
     *
352
     * @return static
353
     */
354
    public function setEncoding($encoding)
355
    {
356
        $this->encoding = $encoding;
357
        return $this;
358
    }
359
}
360