1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Format; |
4
|
|
|
|
5
|
|
|
use Graze\DataFile\Helper\GetOptionTrait; |
6
|
|
|
|
7
|
|
|
class CsvFormat implements CsvFormatInterface |
8
|
|
|
{ |
9
|
|
|
use GetOptionTrait; |
10
|
|
|
|
11
|
|
|
const DEFAULT_DELIMITER = ','; |
12
|
|
|
const DEFAULT_NULL_OUTPUT = '\\N'; |
13
|
|
|
const DEFAULT_INCLUDE_HEADERS = true; |
14
|
|
|
const DEFAULT_LINE_TERMINATOR = "\n"; |
15
|
|
|
const DEFAULT_QUOTE_CHARACTER = '"'; |
16
|
|
|
|
17
|
|
|
const OPTION_DELIMITER = 'delimiter'; |
18
|
|
|
const OPTION_NULL_OUTPUT = 'nullOutput'; |
19
|
|
|
const OPTION_INCLUDE_HEADERS = 'includeHeaders'; |
20
|
|
|
const OPTION_LINE_TERMINATOR = 'lineTerminator'; |
21
|
|
|
const OPTION_QUOTE_CHARACTER = 'quoteCharacter'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $delimiter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $quoteCharacter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $nullOutput; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $includeHeaders; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $lineTerminator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $options -delimiter <string> (Default: ,) Character to use between fields |
50
|
|
|
* -quoteCharacter <string> (Default: ") |
51
|
|
|
* -nullOutput <string> (Default: \N) |
52
|
|
|
* -includeHeaders <bool> (Default: true) |
53
|
|
|
* -lineTerminator <string> (Default: \n) [Not current implemented] |
54
|
|
|
*/ |
55
|
10 |
|
public function __construct($options = []) |
56
|
|
|
{ |
57
|
10 |
|
$this->options = $options; |
58
|
10 |
|
$this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER); |
59
|
10 |
|
$this->quoteCharacter = $this->getOption(static::OPTION_QUOTE_CHARACTER, static::DEFAULT_QUOTE_CHARACTER); |
60
|
10 |
|
$this->nullOutput = $this->getOption(static::OPTION_NULL_OUTPUT, static::DEFAULT_NULL_OUTPUT); |
61
|
10 |
|
$this->includeHeaders = $this->getOption(static::OPTION_INCLUDE_HEADERS, static::DEFAULT_INCLUDE_HEADERS); |
62
|
10 |
|
$this->lineTerminator = $this->getOption(static::OPTION_LINE_TERMINATOR, static::DEFAULT_LINE_TERMINATOR); |
63
|
10 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
7 |
|
public function getDelimiter() |
69
|
|
|
{ |
70
|
7 |
|
return $this->delimiter; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $delimiter |
75
|
|
|
* |
76
|
|
|
* @return CsvFormat |
77
|
|
|
*/ |
78
|
3 |
|
public function setDelimiter($delimiter) |
79
|
|
|
{ |
80
|
3 |
|
$this->delimiter = $delimiter; |
81
|
3 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
6 |
|
public function hasQuotes() |
88
|
|
|
{ |
89
|
6 |
|
return $this->quoteCharacter <> ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
6 |
|
public function getNullOutput() |
96
|
|
|
{ |
97
|
6 |
|
return $this->nullOutput; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $nullOutput |
102
|
|
|
* |
103
|
|
|
* @return CsvFormat |
104
|
|
|
*/ |
105
|
2 |
|
public function setNullOutput($nullOutput) |
106
|
|
|
{ |
107
|
2 |
|
$this->nullOutput = $nullOutput; |
108
|
2 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
6 |
|
public function hasHeaders() |
115
|
|
|
{ |
116
|
6 |
|
return $this->includeHeaders; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param bool $includeHeaders |
121
|
|
|
* |
122
|
|
|
* @return CsvFormat |
123
|
|
|
*/ |
124
|
2 |
|
public function setHeaders($includeHeaders) |
125
|
|
|
{ |
126
|
2 |
|
$this->includeHeaders = $includeHeaders; |
127
|
2 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
6 |
|
public function getLineTerminator() |
134
|
|
|
{ |
135
|
6 |
|
return $this->lineTerminator; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $lineTerminator |
140
|
|
|
* |
141
|
|
|
* @return CsvFormat |
142
|
|
|
*/ |
143
|
2 |
|
public function setLineTerminator($lineTerminator) |
144
|
|
|
{ |
145
|
2 |
|
$this->lineTerminator = $lineTerminator; |
146
|
2 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
6 |
|
public function getQuoteCharacter() |
153
|
|
|
{ |
154
|
6 |
|
return $this->quoteCharacter; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $quoteCharacter |
159
|
|
|
* |
160
|
|
|
* @return CsvFormat |
161
|
|
|
*/ |
162
|
2 |
|
public function setQuoteCharacter($quoteCharacter) |
163
|
|
|
{ |
164
|
2 |
|
$this->quoteCharacter = $quoteCharacter; |
165
|
2 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Type type of file format (defined in FileFormatType::) |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public function getType() |
174
|
|
|
{ |
175
|
1 |
|
return 'csv'; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|