Completed
Push — master ( e42624...c35825 )
by Harry
9s
created

CsvFormat::hasHeaderRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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