From 3da52d862ada60e6fd668b3bd484f8339b12b0de Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 27 Mar 2023 14:27:51 +0200 Subject: [PATCH] Implement Parser::setSeparator() --- README.md | 2 ++ src/Parser.php | 7 ++++ .../fixture/fixture_non_default_separator.csv | 1 + tests/unit/ParserTest.php | 32 ++++++++++++++++++- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/fixture/fixture_non_default_separator.csv diff --git a/README.md b/README.md index e1a14dc..d9046fe 100644 --- a/README.md +++ b/README.md @@ -107,3 +107,5 @@ array(3) { ``` The `Parser::ignoreFirstLine()` method can be used to configure the `Parser` to ignore the first line of the CSV file. + +The `Parser::setSeparator()` method can be used to configure the `Parser` to use a separator different from the default `,`. diff --git a/src/Parser.php b/src/Parser.php index e35687d..3e72737 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -19,6 +19,7 @@ */ final class Parser { + private string $separator = ','; private bool $ignoreFirstLine = false; /** @@ -36,10 +37,16 @@ public function parse(string $filename, Schema $schema): Generator } $file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE); + $file->setCsvControl($this->separator); return $this->generator($file, $schema); } + public function setSeparator(string $separator): void + { + $this->separator = $separator; + } + public function ignoreFirstLine(): void { $this->ignoreFirstLine = true; diff --git a/tests/fixture/fixture_non_default_separator.csv b/tests/fixture/fixture_non_default_separator.csv new file mode 100644 index 0000000..0c7df80 --- /dev/null +++ b/tests/fixture/fixture_non_default_separator.csv @@ -0,0 +1 @@ +1;2;3;1;0;2023-03-24 diff --git a/tests/unit/ParserTest.php b/tests/unit/ParserTest.php index 45809b4..fcbb301 100644 --- a/tests/unit/ParserTest.php +++ b/tests/unit/ParserTest.php @@ -49,6 +49,7 @@ public static function provider(): array ), __DIR__ . '/../fixture/fixture_with_header.csv', true, + null, ], 'CSV file with header; schema for subset of columns' => [ @@ -62,6 +63,7 @@ public static function provider(): array ), __DIR__ . '/../fixture/fixture_with_header.csv', true, + null, ], 'CSV file without header; schema for all columns' => [ @@ -83,6 +85,7 @@ public static function provider(): array ), __DIR__ . '/../fixture/fixture_without_header.csv', false, + null, ], 'CSV file without header; schema for subset of columns' => [ @@ -96,15 +99,42 @@ public static function provider(): array ), __DIR__ . '/../fixture/fixture_without_header.csv', false, + null, + ], + + 'CSV file with non-default separator' => [ + [ + [ + 'a' => 1, + 'b' => 2.0, + 'c' => '3', + 'd' => true, + 'e' => false, + ], + ], + Schema::from( + ColumnDefinition::from(1, 'a', Type::integer()), + ColumnDefinition::from(2, 'b', Type::float()), + ColumnDefinition::from(3, 'c', Type::string()), + ColumnDefinition::from(4, 'd', Type::boolean()), + ColumnDefinition::from(5, 'e', Type::boolean()), + ), + __DIR__ . '/../fixture/fixture_non_default_separator.csv', + false, + ';', ], ]; } #[DataProvider('provider')] - public function test_Parses_CSV_file_according_to_schema(array $expected, Schema $schema, string $filename, bool $ignoreFirstLine): void + public function test_Parses_CSV_file_according_to_schema(array $expected, Schema $schema, string $filename, bool $ignoreFirstLine, ?string $separator): void { $parser = new Parser; + if ($separator !== null) { + $parser->setSeparator($separator); + } + if ($ignoreFirstLine) { $parser->ignoreFirstLine(); }