1 | <?php |
||
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 | * -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 | * file) |
||
70 | * -lineTerminator <string> (Default: \n) |
||
71 | * -escape <string> (Default: \\) Character to use for escaping |
||
72 | * -limit <int> Total number of data rows to return |
||
73 | * -doubleQuote <bool> instances of quote in fields are indicated by a double quote |
||
74 | */ |
||
75 | 14 | public function __construct(array $options = []) |
|
76 | { |
||
77 | 14 | $this->options = $options; |
|
78 | 14 | $this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER); |
|
79 | 14 | $this->quoteCharacter = $this->getOption(static::OPTION_QUOTE_CHARACTER, static::DEFAULT_QUOTE_CHARACTER); |
|
80 | 14 | $this->nullOutput = $this->getOption(static::OPTION_NULL_OUTPUT, static::DEFAULT_NULL_OUTPUT); |
|
81 | 14 | $this->headerRow = $this->getOption(static::OPTION_HEADER_ROW, static::DEFAULT_HEADER_ROW); |
|
82 | 14 | $this->dataStart = $this->getOption(static::OPTION_DATA_START, static::DEFAULT_DATA_START); |
|
83 | 14 | $this->lineTerminator = $this->getOption(static::OPTION_LINE_TERMINATOR, static::DEFAULT_LINE_TERMINATOR); |
|
84 | 14 | $this->escape = $this->getOption(static::OPTION_ESCAPE, static::DEFAULT_ESCAPE); |
|
85 | 14 | $this->limit = $this->getOption(static::OPTION_LIMIT, static::DEFAULT_LIMIT); |
|
86 | 14 | $this->doubleQuote = $this->getOption(static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE); |
|
87 | 14 | } |
|
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | 25 | public function getDelimiter() |
|
96 | |||
97 | /** |
||
98 | * @param string $delimiter |
||
99 | * |
||
100 | * @return static |
||
101 | */ |
||
102 | 2 | public function setDelimiter($delimiter) |
|
103 | { |
||
104 | 2 | $this->delimiter = $delimiter; |
|
105 | 2 | return $this; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return bool |
||
110 | */ |
||
111 | 17 | public function hasQuotes() |
|
112 | { |
||
113 | 17 | return $this->quoteCharacter <> ''; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 15 | public function getNullOutput() |
|
123 | |||
124 | /** |
||
125 | * @param string $nullOutput |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | 1 | public function setNullOutput($nullOutput) |
|
130 | { |
||
131 | 1 | $this->nullOutput = $nullOutput; |
|
132 | 1 | return $this; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return bool |
||
137 | */ |
||
138 | 12 | public function hasHeaderRow() |
|
142 | |||
143 | /** |
||
144 | * @param int $headerRow |
||
145 | * |
||
146 | * @return static |
||
147 | */ |
||
148 | 2 | public function setHeaderRow($headerRow) |
|
149 | { |
||
150 | 2 | $this->headerRow = $headerRow; |
|
151 | 2 | return $this; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return int |
||
156 | */ |
||
157 | 7 | public function getHeaderRow() |
|
158 | { |
||
159 | 7 | return $this->headerRow; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param int $row |
||
164 | * |
||
165 | * @return static |
||
166 | */ |
||
167 | 2 | public function setDataStart($row) |
|
168 | { |
||
169 | 2 | $this->dataStart = $row; |
|
170 | 2 | return $this; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return int |
||
175 | */ |
||
176 | 12 | public function getDataStart() |
|
177 | { |
||
178 | 12 | if ($this->hasHeaderRow() && $this->getHeaderRow() >= $this->dataStart) { |
|
179 | 4 | return max(1, $this->getHeaderRow() + 1); |
|
180 | } |
||
181 | 9 | return max(1, $this->dataStart); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | 15 | public function getLineTerminator() |
|
188 | { |
||
189 | 15 | return $this->lineTerminator; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $lineTerminator |
||
194 | * |
||
195 | * @return static |
||
196 | */ |
||
197 | 1 | public function setLineTerminator($lineTerminator) |
|
198 | { |
||
199 | 1 | $this->lineTerminator = $lineTerminator; |
|
200 | 1 | return $this; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @note Csv Rfc spec defines escaping of quotes to be done using double quotes `""` |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 24 | public function getQuoteCharacter() |
|
209 | { |
||
210 | 24 | return $this->quoteCharacter; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param string $quoteCharacter |
||
215 | * |
||
216 | * @return static |
||
217 | */ |
||
218 | 1 | public function setQuoteCharacter($quoteCharacter) |
|
219 | { |
||
220 | 1 | $this->quoteCharacter = $quoteCharacter; |
|
221 | 1 | return $this; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Type type of file format (defined in FileFormatType::) |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 3 | public function getType() |
|
230 | { |
||
231 | 3 | return 'csv'; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | 25 | public function getEscapeCharacter() |
|
238 | { |
||
239 | 25 | return $this->escape; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string $escape |
||
244 | * |
||
245 | * @return static |
||
246 | */ |
||
247 | 1 | public function setEscapeCharacter($escape) |
|
252 | |||
253 | /** |
||
254 | * @return bool |
||
255 | */ |
||
256 | 3 | public function hasEscapeCharacter() |
|
260 | |||
261 | /** |
||
262 | * Get the limit that should be returned (-1 for no limit) |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | 11 | public function getLimit() |
|
267 | { |
||
268 | 11 | return $this->limit; |
|
269 | } |
||
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 | 1 | public function setLimit($limit) |
|
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | 22 | public function isDoubleQuote() |
|
291 | |||
292 | /** |
||
293 | * @param bool $doubleQuote |
||
294 | * |
||
295 | * @return static |
||
296 | */ |
||
297 | 1 | public function setDoubleQuote($doubleQuote) |
|
302 | } |
||
303 |