1 | <?php |
||
19 | class CsvWriter extends AbstractWriter |
||
20 | { |
||
21 | /** |
||
22 | * CSV Delimiter |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $delimiter = ';'; |
||
27 | |||
28 | /** |
||
29 | * CSV Enclosure. |
||
30 | * fputcsv only encloses values |
||
31 | * it deems need to be enclosed |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $enclosure = '"'; |
||
36 | |||
37 | /** |
||
38 | * EOL because some people use |
||
39 | * windows for batch processing???! |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $eol = "\n"; |
||
44 | |||
45 | /** |
||
46 | * File pointer |
||
47 | * |
||
48 | * @var null|resource |
||
49 | */ |
||
50 | protected $fp = null; |
||
51 | |||
52 | /** |
||
53 | * Column Headers |
||
54 | * |
||
55 | * @var null|array |
||
56 | */ |
||
57 | protected $columnHeaders = null; |
||
58 | |||
59 | /** |
||
60 | * Count of headers |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $headersCount; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $encloseEmptyFields = false; |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @param \SplFileObject $file CSV file |
||
74 | * @param string $mode See http://php.net/manual/en/function.fopen.php |
||
75 | * @param string $delimiter The delimiter |
||
76 | * @param string $enclosure The enclosure |
||
77 | * @param string $eol The end of line string |
||
78 | * @param bool $encloseEmptyFields Whether to enclose empty fields or not |
||
79 | */ |
||
80 | public function __construct( |
||
94 | |||
95 | /** |
||
96 | * Set column headers |
||
97 | * |
||
98 | * @param array $columnHeaders |
||
99 | * |
||
100 | * @return CsvReader |
||
101 | */ |
||
102 | public function setColumnHeaders(array $columnHeaders) |
||
109 | |||
110 | /** |
||
111 | * Write Column Header |
||
112 | * |
||
113 | * @return \Ddeboer\DataImport\Writer\WriterInterface |
||
114 | */ |
||
115 | public function prepare() |
||
123 | |||
124 | /** |
||
125 | * TODO: Make stripping quotes/enclosure configurable |
||
126 | * TODO: Make enclosing configurable |
||
127 | * TODO: Use fputcsv when ALL_ENCLOSING not necessary |
||
128 | * |
||
129 | * Write a line, removing double quotes and then enclosing every |
||
130 | * piece of data. |
||
131 | * |
||
132 | * @param array $data |
||
133 | */ |
||
134 | public function writeLine(array $data) |
||
149 | |||
150 | /** |
||
151 | * TODO: Make ordering configurable |
||
152 | * |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function writeItem(array $item) |
||
167 | |||
168 | /** |
||
169 | * Order the data using the order of the Header rows |
||
170 | * This assumes that each row has the keys which |
||
171 | * correspond to the header |
||
172 | * |
||
173 | * @param array $data |
||
174 | * @return array |
||
175 | */ |
||
176 | public function orderDataByColumnHeaders(array $data) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function finish() |
||
189 | } |
||
190 |