Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an ability to specify CSV ouput encoding #3507

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.
- Fix return type of `FromQuery::query()`
- Support Laravel 9
- Added a config setting to specify DB connection
- Added a config setting to specify CSV output encoding
- Added an ability to specify CSV ouput encoding through csvSettings

## [3.1.35] - 2022-01-04

Expand Down
1 change: 1 addition & 0 deletions config/excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'use_bom' => false,
'include_separator_line' => false,
'excel_compatibility' => false,
'output_encoding' => '',
],

/*
Expand Down
6 changes: 6 additions & 0 deletions src/Concerns/MapsCsvSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ trait MapsCsvSettings
*/
protected static $inputEncoding = 'UTF-8';

/**
* @var string
*/
protected static $outputEncoding = '';

/**
* @param array $config
*/
Expand All @@ -65,5 +70,6 @@ public static function applyCsvSettings(array $config)
static::$escapeCharacter = Arr::get($config, 'escape_character', static::$escapeCharacter);
static::$contiguous = Arr::get($config, 'contiguous', static::$contiguous);
static::$inputEncoding = Arr::get($config, 'input_encoding', static::$inputEncoding);
static::$outputEncoding = Arr::get($config, 'output_encoding', static::$outputEncoding);
}
}
1 change: 1 addition & 0 deletions src/Factories/WriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function make(string $writerType, Spreadsheet $spreadsheet, $expor
$writer->setUseBOM(static::$useBom);
$writer->setIncludeSeparatorLine(static::$includeSeparatorLine);
$writer->setExcelCompatibility(static::$excelCompatibility);
$writer->setOutputEncoding(static::$outputEncoding);
}

// Calculation settings
Expand Down
50 changes: 50 additions & 0 deletions tests/Concerns/WithCustomCsvSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getCsvSettings(): array
'use_bom' => true,
'include_separator_line' => true,
'excel_compatibility' => false,
'output_encoding' => '',
];
}
};
Expand All @@ -67,6 +68,55 @@ public function getCsvSettings(): array
$this->assertStringContains('A2;B2', $contents);
}

/**
* @test
*/
public function can_store_csv_export_with_custom_encoding()
{
$export = new class implements FromCollection, WithCustomCsvSettings
{
/**
* @return Collection
*/
public function collection()
{
return collect([
['A1', '€ŠšŽžŒœŸ'],
['A2', 'åßàèòìù'],
]);
}

/**
* @return array
*/
public function getCsvSettings(): array
{
return [
'delimiter' => ';',
'enclosure' => '',
'line_ending' => PHP_EOL,
'use_bom' => false,
'include_separator_line' => true,
'excel_compatibility' => false,
'output_encoding' => 'ISO-8859-15',
];
}
};

$this->SUT->store($export, 'custom-csv-iso.csv');

$contents = file_get_contents(__DIR__ . '/../Data/Disks/Local/custom-csv-iso.csv');

Assert::assertEquals('ISO-8859-15', mb_detect_encoding($contents, 'ISO-8859-15', true));
Assert::assertFalse(mb_detect_encoding($contents, 'UTF-8', true));

$contents = mb_convert_encoding($contents, 'UTF-8', 'ISO-8859-15');

$this->assertStringContains('sep=;', $contents);
$this->assertStringContains('A1;€ŠšŽžŒœŸ', $contents);
$this->assertStringContains('A2;åßàèòìù', $contents);
}

/**
* @test
*/
Expand Down