From 949363377f3492c29adc54acf9363eda2817e144 Mon Sep 17 00:00:00 2001 From: Patrick Brouwers Date: Mon, 28 Feb 2022 11:14:01 +0100 Subject: [PATCH] Autodetect csv delimiter when not explicitly defined on imports --- CHANGELOG.md | 16 +++++++-- composer.json | 3 +- config/excel.php | 2 +- tests/Concerns/WithCustomCsvSettingsTest.php | 36 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8881ee769..0ea5ff4f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,21 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +## [3.1.37] - 2022-02-28 + +### Fixed - Add `@mixin` docblock to all macroable classes to allow for IDE autocompletion of delegate classes +- Fix issue with `Excel::toArray` not allowing nullable reader types for uploaded files + +### Changed +- Change default Csv Import to auto-detect the delimiter when not explicitly defined ## [3.1.36] - 2022-02-03 + +### Fixed - Fix return type of `FromQuery::query()` + +## Changed - Support Laravel 9 - Added a config setting to specify DB connection - Added a config setting to specify CSV output encoding @@ -196,8 +207,9 @@ All notable changes to this project will be documented in this file. - Raw() method now also available on Exportable. - Fix for breaking changes in PhpSpreadsheet with empty enclosures. -[Unreleased]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.36...HEAD -[3.1.35]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.35...3.1.36 +[Unreleased]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.37...HEAD +[3.1.37]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.36...3.1.37 +[3.1.36]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.35...3.1.36 [3.1.35]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.34...3.1.35 [3.1.34]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.33...3.1.34 [3.1.33]: https://github.com/Maatwebsite/Laravel-Excel/compare/3.1.32...3.1.33 diff --git a/composer.json b/composer.json index 21c368ce5..b4d07914c 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ }, "require-dev": { "orchestra/testbench": "^6.0|^7.0", - "predis/predis": "^1.1" + "predis/predis": "^1.1", + "laravel/legacy-factories": "^1.3" }, "autoload": { "psr-4": { diff --git a/config/excel.php b/config/excel.php index f4d1e78d2..1c7b5c998 100644 --- a/config/excel.php +++ b/config/excel.php @@ -122,7 +122,7 @@ | */ 'csv' => [ - 'delimiter' => ',', + 'delimiter' => null, 'enclosure' => '"', 'escape_character' => '\\', 'contiguous' => false, diff --git a/tests/Concerns/WithCustomCsvSettingsTest.php b/tests/Concerns/WithCustomCsvSettingsTest.php index 45b2597db..c8244d229 100644 --- a/tests/Concerns/WithCustomCsvSettingsTest.php +++ b/tests/Concerns/WithCustomCsvSettingsTest.php @@ -117,6 +117,42 @@ public function getCsvSettings(): array $this->assertStringContains('A2;åßàèòìù', $contents); } + /** + * @test + */ + public function can_read_csv_with_auto_detecting_delimiter() + { + $import = new class implements WithCustomCsvSettings, ToArray + { + /** + * @return array + */ + public function getCsvSettings(): array + { + return [ + 'delimiter' => null, + 'enclosure' => '', + 'escape_character' => '\\', + 'contiguous' => true, + 'input_encoding' => 'UTF-8', + ]; + } + + /** + * @param array $array + */ + public function array(array $array) + { + Assert::assertEquals([ + ['A1', 'B1'], + ['A2', 'B2'], + ], $array); + } + }; + + $this->SUT->import($import, 'csv-with-other-delimiter.csv'); + } + /** * @test */