|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pim\Bundle\ExcelConnectorBundle\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use Pim\Bundle\BaseConnectorBundle\Writer\File\FileWriter; |
|
6
|
|
|
use Pim\Component\Connector\Writer\File\ArchivableWriterInterface; |
|
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Writes normalized array in an homogeneous CSV file |
|
11
|
|
|
* |
|
12
|
|
|
* @author Antoine Guigan <[email protected]> |
|
13
|
|
|
* @copyright 2014 Akeneo SAS (http://www.akeneo.com) |
|
14
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
15
|
|
|
*/ |
|
16
|
|
|
class HomogeneousCSVWriter extends FileWriter implements ArchivableWriterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @Assert\NotBlank |
|
20
|
|
|
* @Assert\Choice(choices={",", ";", "|"}, message="The value must be one of , or ; or |") |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $delimiter = ';'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @Assert\NotBlank |
|
27
|
|
|
* @Assert\Choice(choices={"""", "'"}, message="The value must be one of "" or '") |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $enclosure = '"'; |
|
31
|
|
|
|
|
32
|
|
|
/** @var boolean */ |
|
33
|
|
|
protected $withHeader = true; |
|
34
|
|
|
|
|
35
|
|
|
/** @var array */ |
|
36
|
|
|
private $writtenFiles = []; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array */ |
|
39
|
|
|
private $headers; |
|
40
|
|
|
|
|
41
|
|
|
/** @var resource */ |
|
42
|
|
|
private $file; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sets the csv delimiter character |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $delimiter |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setDelimiter($delimiter) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->delimiter = $delimiter; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets the csv delimiter character |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDelimiter() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->delimiter; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the csv enclosure character |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $enclosure |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setEnclosure($enclosure) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->enclosure = $enclosure; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the csv enclosure character |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getEnclosure() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->enclosure; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets whether or not to print a header row into the csv |
|
86
|
|
|
* |
|
87
|
|
|
* @param boolean $withHeader |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setWithHeader($withHeader) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->withHeader = $withHeader; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Gets whether or not to print a header row into the csv |
|
96
|
|
|
* |
|
97
|
|
|
* @return boolean |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isWithHeader() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->withHeader; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getWrittenFiles() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->writtenFiles; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
|
|
public function initialize() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->writtenFiles[$this->getPath()] = basename($this->getPath()); |
|
118
|
|
|
$this->file = fopen($this->getPath(), 'w'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function flush() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->headers = null; |
|
127
|
|
|
fclose($this->file); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getConfigurationFields() |
|
134
|
|
|
{ |
|
135
|
|
|
return |
|
136
|
|
|
array_merge( |
|
137
|
|
|
parent::getConfigurationFields(), |
|
138
|
|
|
array( |
|
139
|
|
|
'delimiter' => array( |
|
140
|
|
|
'options' => array( |
|
141
|
|
|
'label' => 'pim_base_connector.export.delimiter.label', |
|
142
|
|
|
'help' => 'pim_base_connector.export.delimiter.help' |
|
143
|
|
|
) |
|
144
|
|
|
), |
|
145
|
|
|
'enclosure' => array( |
|
146
|
|
|
'options' => array( |
|
147
|
|
|
'label' => 'pim_base_connector.export.enclosure.label', |
|
148
|
|
|
'help' => 'pim_base_connector.export.enclosure.help' |
|
149
|
|
|
) |
|
150
|
|
|
), |
|
151
|
|
|
'withHeader' => array( |
|
152
|
|
|
'type' => 'switch', |
|
153
|
|
|
'options' => array( |
|
154
|
|
|
'label' => 'pim_base_connector.export.withHeader.label', |
|
155
|
|
|
'help' => 'pim_base_connector.export.withHeader.help' |
|
156
|
|
|
) |
|
157
|
|
|
) |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function write(array $items) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!count($items)) { |
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (null === $this->headers) { |
|
172
|
|
|
$this->headers = array_keys(current($items)); |
|
173
|
|
|
$this->writeHeaders(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
foreach ($items as $item) { |
|
177
|
|
|
$this->writeItem($item); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Writes an item in the file |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $item |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function writeItem(array $item) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->writeLine($this->getItemRow($item)); |
|
189
|
|
|
|
|
190
|
|
|
if ($this->stepExecution) { |
|
191
|
|
|
$this->stepExecution->incrementSummaryInfo('write'); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Returns a CSV row for an item |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $item |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function getItemRow(array $item) |
|
203
|
|
|
{ |
|
204
|
|
|
return array_map( |
|
205
|
|
|
function ($key) use ($item) { |
|
206
|
|
|
return isset($item[$key]) ? $item[$key] : ''; |
|
207
|
|
|
}, |
|
208
|
|
|
$this->headers |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Writes the headers |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $csv |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function writeHeaders() |
|
218
|
|
|
{ |
|
219
|
|
|
if ($this->withHeader) { |
|
220
|
|
|
$this->writeLine($this->headers); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Writes a CSV line in the file |
|
226
|
|
|
* |
|
227
|
|
|
* @param array $csv |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function writeLine(array $csv) |
|
230
|
|
|
{ |
|
231
|
|
|
fputcsv($this->file, $csv, $this->delimiter, $this->enclosure); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|