Completed
Pull Request — master (#3)
by Harry
03:17
created

CsvFormat::getLineTerminator()   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_HEADERS         = 1;
25
    const DEFAULT_LINE_TERMINATOR = "\n";
26
    const DEFAULT_QUOTE_CHARACTER = '"';
27
    const DEFAULT_ESCAPE          = '\\';
28
    const DEFAULT_LIMIT           = -1;
29
    const DEFAULT_DOUBLE_QUOTE    = false;
30
31
    const OPTION_DELIMITER       = 'delimiter';
32
    const OPTION_NULL_OUTPUT     = 'nullOutput';
33
    const OPTION_HEADERS         = 'headers';
34
    const OPTION_LINE_TERMINATOR = 'lineTerminator';
35
    const OPTION_QUOTE_CHARACTER = 'quoteCharacter';
36
    const OPTION_ESCAPE          = 'escape';
37
    const OPTION_LIMIT           = 'limit';
38
    const OPTION_DOUBLE_QUOTE    = 'doubleQuote';
39
40
    /** @var string */
41
    protected $delimiter;
42
    /** @var string */
43
    protected $quoteCharacter;
44
    /** @var string */
45
    protected $nullOutput;
46
    /** @var int */
47
    protected $headers;
48
    /** @var string */
49
    protected $lineTerminator;
50
    /** @var bool */
51
    protected $nullQuotes;
52
    /** @var string */
53
    protected $escape;
54
    /** @var int */
55
    protected $limit;
56
    /** @var bool */
57
    protected $doubleQuote;
58
59
    /**
60
     * @param array $options -delimiter <string> (Default: ,) Character to use between fields
61
     *                       -quoteCharacter <string> (Default: ")
62
     *                       -nullOutput <string> (Default: \N)
63
     *                       -headers <int> (Default: 1)
64
     *                       -lineTerminator <string> (Default: \n)
65
     *
66
     */
67 16
    public function __construct(array $options = [])
68
    {
69 16
        $this->options = $options;
70 16
        $this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
71 16
        $this->quoteCharacter = $this->getOption(static::OPTION_QUOTE_CHARACTER, static::DEFAULT_QUOTE_CHARACTER);
72 16
        $this->nullOutput = $this->getOption(static::OPTION_NULL_OUTPUT, static::DEFAULT_NULL_OUTPUT);
73 16
        $this->headers = $this->getOption(static::OPTION_HEADERS, static::DEFAULT_HEADERS);
74 16
        $this->lineTerminator = $this->getOption(static::OPTION_LINE_TERMINATOR, static::DEFAULT_LINE_TERMINATOR);
75 16
        $this->escape = $this->getOption(static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
76 16
        $this->limit = $this->getOption(static::OPTION_LIMIT, static::DEFAULT_LIMIT);
77 16
        $this->doubleQuote = $this->getOption(static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
78 16
    }
79
80
    /**
81
     * @return string
82
     */
83 25
    public function getDelimiter()
84
    {
85 25
        return $this->delimiter;
86
    }
87
88
    /**
89
     * @param string $delimiter
90
     *
91
     * @return static
92
     */
93 3
    public function setDelimiter($delimiter)
94
    {
95 3
        $this->delimiter = $delimiter;
96 3
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 20
    public function hasQuotes()
103
    {
104 20
        return $this->quoteCharacter <> '';
105
    }
106
107
    /**
108
     * @return string
109
     */
110 15
    public function getNullOutput()
111
    {
112 15
        return $this->nullOutput;
113
    }
114
115
    /**
116
     * @param string $nullOutput
117
     *
118
     * @return static
119
     */
120 2
    public function setNullOutput($nullOutput)
121
    {
122 2
        $this->nullOutput = $nullOutput;
123 2
        return $this;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 11
    public function hasHeaders()
130
    {
131 11
        return $this->headers > 0;
132
    }
133
134
    /**
135
     * @param int $headers
136
     *
137
     * @return static
138
     */
139 2
    public function setHeaders($headers)
140
    {
141 2
        $this->headers = $headers;
142 2
        return $this;
143
    }
144
145
    /**
146
     * @return int
147
     */
148 6
    public function getHeaders()
149
    {
150 6
        return $this->headers;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 15
    public function getLineTerminator()
157
    {
158 15
        return $this->lineTerminator;
159
    }
160
161
    /**
162
     * @param string $lineTerminator
163
     *
164
     * @return static
165
     */
166 2
    public function setLineTerminator($lineTerminator)
167
    {
168 2
        $this->lineTerminator = $lineTerminator;
169 2
        return $this;
170
    }
171
172
    /**
173
     * @note Csv Rfc spec defines escaping of quotes to be done using double quotes `""`
174
     *
175
     * @return string
176
     */
177 24
    public function getQuoteCharacter()
178
    {
179 24
        return $this->quoteCharacter;
180
    }
181
182
    /**
183
     * @param string $quoteCharacter
184
     *
185
     * @return static
186
     */
187 2
    public function setQuoteCharacter($quoteCharacter)
188
    {
189 2
        $this->quoteCharacter = $quoteCharacter;
190 2
        return $this;
191
    }
192
193
    /**
194
     * Type type of file format (defined in FileFormatType::)
195
     *
196
     * @return string
197
     */
198 3
    public function getType()
199
    {
200 3
        return 'csv';
201
    }
202
203
    /**
204
     * @return string
205
     */
206 22
    public function getEscapeCharacter()
207
    {
208 22
        return $this->escape;
209
    }
210
211
    /**
212
     * @param string $escape
213
     *
214
     * @return static
215
     */
216 1
    public function setEscapeCharacter($escape)
217
    {
218 1
        $this->escape = $escape;
219 1
        return $this;
220
    }
221
222
    /**
223
     * @return bool
224
     */
225 3
    public function hasEscapeCharacter()
226
    {
227 3
        return $this->escape !== '';
228
    }
229
230
    /**
231
     * Get the limit that should be returned (-1 for no limit)
232
     *
233
     * @return int
234
     */
235 8
    public function getLimit()
236
    {
237 8
        return $this->limit;
238
    }
239
240
    /**
241
     * Set the limit of the number of items to be returned (-1 for not limit)
242
     *
243
     * @param int $limit
244
     *
245
     * @return static
246
     */
247 1
    public function setLimit($limit)
248
    {
249 1
        $this->limit = $limit;
250 1
        return $this;
251
    }
252
253
    /**
254
     * @return bool
255
     */
256 19
    public function isDoubleQuote()
257
    {
258 19
        return $this->doubleQuote;
259
    }
260
261
    /**
262
     * @param bool $doubleQuote
263
     *
264
     * @return static
265
     */
266 1
    public function setDoubleQuote($doubleQuote)
267
    {
268 1
        $this->doubleQuote = $doubleQuote;
269 1
        return $this;
270
    }
271
}
272