@@ -102,22 +102,24 @@ public function exportEntities(AbstractNamedDBElement|array $entities, array $op
102
102
$ groups [] = 'include_children ' ;
103
103
}
104
104
105
- return $ this ->serializer ->serialize ($ entities , $ options ['format ' ],
105
+ return $ this ->serializer ->serialize (
106
+ $ entities ,
107
+ $ options ['format ' ],
106
108
[
107
109
'groups ' => $ groups ,
108
110
'as_collection ' => true ,
109
111
'csv_delimiter ' => $ options ['csv_delimiter ' ],
110
112
'xml_root_node_name ' => 'PartDBExport ' ,
111
113
'partdb_export ' => true ,
112
- //Skip the item normalizer, so that we dont get IRIs in the output
114
+ //Skip the item normalizer, so that we dont get IRIs in the output
113
115
SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER => true ,
114
- //Handle circular references
116
+ //Handle circular references
115
117
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => $ this ->handleCircularReference (...),
116
118
]
117
119
);
118
120
}
119
121
120
- private function handleCircularReference (object $ object, string $ format , array $ context ): string
122
+ private function handleCircularReference (object $ object ): string
121
123
{
122
124
if ($ object instanceof AbstractStructuralDBElement) {
123
125
return $ object ->getFullPath ("-> " );
@@ -127,7 +129,7 @@ private function handleCircularReference(object $object, string $format, array $
127
129
return $ object ->__toString ();
128
130
}
129
131
130
- throw new CircularReferenceException ('Circular reference detected for object of type ' . get_class ($ object ));
132
+ throw new CircularReferenceException ('Circular reference detected for object of type ' . get_class ($ object ));
131
133
}
132
134
133
135
/**
@@ -148,7 +150,9 @@ protected function exportToExcel(array $entities, array $options): string
148
150
$ groups [] = 'include_children ' ;
149
151
}
150
152
151
- $ csvData = $ this ->serializer ->serialize ($ entities , 'csv ' ,
153
+ $ csvData = $ this ->serializer ->serialize (
154
+ $ entities ,
155
+ 'csv ' ,
152
156
[
153
157
'groups ' => $ groups ,
154
158
'as_collection ' => true ,
@@ -162,18 +166,18 @@ protected function exportToExcel(array $entities, array $options): string
162
166
//Convert CSV to Excel
163
167
$ spreadsheet = new Spreadsheet ();
164
168
$ worksheet = $ spreadsheet ->getActiveSheet ();
165
-
169
+
166
170
$ rows = explode ("\n" , $ csvData );
167
171
$ rowIndex = 1 ;
168
-
172
+
169
173
foreach ($ rows as $ row ) {
170
174
if (trim ($ row ) === '' ) {
171
175
continue ;
172
176
}
173
-
174
- $ columns = str_getcsv ($ row , $ options ['csv_delimiter ' ]);
177
+
178
+ $ columns = str_getcsv ($ row , $ options ['csv_delimiter ' ], ' " ' , '\\' );
175
179
$ colIndex = 1 ;
176
-
180
+
177
181
foreach ($ columns as $ column ) {
178
182
$ cellCoordinate = \PhpOffice \PhpSpreadsheet \Cell \Coordinate::stringFromColumnIndex ($ colIndex ) . $ rowIndex ;
179
183
$ worksheet ->setCellValue ($ cellCoordinate , $ column );
@@ -183,17 +187,13 @@ protected function exportToExcel(array $entities, array $options): string
183
187
}
184
188
185
189
//Save to memory stream
186
- if ($ options ['format ' ] === 'xlsx ' ) {
187
- $ writer = new Xlsx ($ spreadsheet );
188
- } else {
189
- $ writer = new Xls ($ spreadsheet );
190
- }
191
-
190
+ $ writer = $ options ['format ' ] === 'xlsx ' ? new Xlsx ($ spreadsheet ) : new Xls ($ spreadsheet );
191
+
192
192
ob_start ();
193
193
$ writer ->save ('php://output ' );
194
194
$ content = ob_get_contents ();
195
195
ob_end_clean ();
196
-
196
+
197
197
return $ content ;
198
198
}
199
199
@@ -231,25 +231,15 @@ public function exportEntityFromRequest(AbstractNamedDBElement|array $entities,
231
231
232
232
//Determine the content type for the response
233
233
234
- //Plain text should work for all types
235
- $ content_type = 'text/plain ' ;
236
-
237
234
//Try to use better content types based on the format
238
235
$ format = $ options ['format ' ];
239
- switch ($ format ) {
240
- case 'xml ' :
241
- $ content_type = 'application/xml ' ;
242
- break ;
243
- case 'json ' :
244
- $ content_type = 'application/json ' ;
245
- break ;
246
- case 'xlsx ' :
247
- $ content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ' ;
248
- break ;
249
- case 'xls ' :
250
- $ content_type = 'application/vnd.ms-excel ' ;
251
- break ;
252
- }
236
+ $ content_type = match ($ format ) {
237
+ 'xml ' => 'application/xml ' ,
238
+ 'json ' => 'application/json ' ,
239
+ 'xlsx ' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ' ,
240
+ 'xls ' => 'application/vnd.ms-excel ' ,
241
+ default => 'text/plain ' ,
242
+ };
253
243
$ response ->headers ->set ('Content-Type ' , $ content_type );
254
244
255
245
//If view option is not specified, then download the file.
@@ -267,7 +257,7 @@ public function exportEntityFromRequest(AbstractNamedDBElement|array $entities,
267
257
268
258
$ level = $ options ['level ' ];
269
259
270
- $ filename = ' export_ ' . $ entity_name. ' _ ' . $ level. ' . ' . $ format ;
260
+ $ filename = " export_ { $ entity_name} _ { $ level} . { $ format}" ;
271
261
272
262
//Sanitize the filename
273
263
$ filename = FilenameSanatizer::sanitizeFilename ($ filename );
0 commit comments