Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Migration/Sources/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,15 @@ private function exportDocuments(int $batchSize): void
return;
}

$this->validateCSVHeaders($headers, $attributeTypes);

$buffer = [];

while (($row = fgetcsv($stream)) !== false) {
if (count($row) !== count($headers)) {
throw new \Exception('CSV row does not match the number of header columns.');
}

$data = array_combine($headers, $row);
if ($data === false) {
continue;
Expand Down Expand Up @@ -277,4 +283,34 @@ private function withCsvStream(callable $fn): void
fclose($stream);
}
}

/**
* @throws \Exception
*/
private function validateCSVHeaders(array $headers, array $attributeTypes): void
{
$expectedAttributes = array_keys($attributeTypes);

// Ignore keys like $id, $permissions, etc.
$filteredHeaders = array_filter($headers, fn ($key) => ! str_starts_with($key, '$'));

$extraAttributes = array_diff($filteredHeaders, $expectedAttributes);
$missingAttributes = array_diff($expectedAttributes, $filteredHeaders);

if (! empty($missingAttributes) || ! empty($extraAttributes)) {
$messages = [];

if (! empty($missingAttributes)) {
$label = count($missingAttributes) === 1 ? 'Missing attribute' : 'Missing attributes';
$messages[] = "{$label}: '".implode("', '", $missingAttributes)."'";
}

if (! empty($extraAttributes)) {
$label = count($extraAttributes) === 1 ? 'Unexpected attribute' : 'Unexpected attributes';
$messages[] = "{$label}: '".implode("', '", $extraAttributes)."'";
}

throw new \Exception('CSV header mismatch. '.implode(' | ', $messages));
}
}
}