@@ -29,6 +29,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
29
29
const HEADERS_KEY = 'csv_headers ' ;
30
30
const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas ' ;
31
31
const AS_COLLECTION_KEY = 'as_collection ' ;
32
+ const NO_HEADERS_KEY = 'no_headers ' ;
32
33
33
34
private $ formulasStartCharacters = array ('= ' , '- ' , '+ ' , '@ ' );
34
35
private $ defaultContext = array (
@@ -38,6 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
38
39
self ::ESCAPE_FORMULAS_KEY => false ,
39
40
self ::HEADERS_KEY => array (),
40
41
self ::KEY_SEPARATOR_KEY => '. ' ,
42
+ self ::NO_HEADERS_KEY => false ,
41
43
);
42
44
43
45
/**
@@ -95,7 +97,9 @@ public function encode($data, $format, array $context = array())
95
97
96
98
$ headers = array_merge (array_values ($ headers ), array_diff ($ this ->extractHeaders ($ data ), $ headers ));
97
99
98
- fputcsv ($ handle , $ headers , $ delimiter , $ enclosure , $ escapeChar );
100
+ if (!($ context [self ::NO_HEADERS_KEY ] ?? false )) {
101
+ fputcsv ($ handle , $ headers , $ delimiter , $ enclosure , $ escapeChar );
102
+ }
99
103
100
104
$ headers = array_fill_keys ($ headers , '' );
101
105
foreach ($ data as $ row ) {
@@ -139,13 +143,20 @@ public function decode($data, $format, array $context = array())
139
143
if (null === $ headers ) {
140
144
$ nbHeaders = $ nbCols ;
141
145
142
- foreach ($ cols as $ col ) {
143
- $ header = explode ($ keySeparator , $ col );
144
- $ headers [] = $ header ;
145
- $ headerCount [] = \count ($ header );
146
- }
146
+ if ($ context [self ::NO_HEADERS_KEY ] ?? false ) {
147
+ for ($ i = 0 ; $ i < $ nbCols ; ++$ i ) {
148
+ $ headers [] = array ($ i );
149
+ }
150
+ $ headerCount = array_fill (0 , $ nbCols , 1 );
151
+ } else {
152
+ foreach ($ cols as $ col ) {
153
+ $ header = explode ($ keySeparator , $ col );
154
+ $ headers [] = $ header ;
155
+ $ headerCount [] = \count ($ header );
156
+ }
147
157
148
- continue ;
158
+ continue ;
159
+ }
149
160
}
150
161
151
162
$ item = array ();
0 commit comments