Skip to content

Commit abafba0

Browse files
committed
Fix array export
1 parent 4a4e873 commit abafba0

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

src/Migration/Destinations/CSV.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,27 @@ protected function convertArrayToCSV(array $value): string
275275
if (empty($value)) {
276276
return '';
277277
}
278+
278279
if (isset($value['$id'])) {
279280
return $value['$id'];
280281
}
281-
return \json_encode($value);
282+
283+
$isStringArray = \array_reduce(
284+
$value,
285+
fn($carry, $item) => $carry && is_string($item),
286+
true
287+
);
288+
289+
if ($isStringArray) {
290+
// For string arrays, escape quotes and join with commas
291+
return \implode(',', \array_map(
292+
fn($item) => \str_replace('"', '""', $item),
293+
$value
294+
));
295+
}
296+
297+
// For complex arrays, use JSON with escaped quotes
298+
return \str_replace('"', '""', \json_encode($value));
282299
}
283300

284301
/**
@@ -289,7 +306,8 @@ protected function convertObjectToCSV($value): string
289306
if ($value instanceof Row) {
290307
return $value->getId();
291308
}
292-
return \json_encode($value);
309+
310+
// Escape quotes for CSV by doubling them
311+
return \str_replace('"', '""', \json_encode($value));
293312
}
294-
295313
}

0 commit comments

Comments
 (0)