Skip to content

Commit

Permalink
Implement Parser::setSeparator()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 27, 2023
1 parent 5b4c43c commit 3da52d8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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 `,`.
7 changes: 7 additions & 0 deletions src/Parser.php
Expand Up @@ -19,6 +19,7 @@
*/
final class Parser
{
private string $separator = ',';
private bool $ignoreFirstLine = false;

/**
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/fixture/fixture_non_default_separator.csv
@@ -0,0 +1 @@
1;2;3;1;0;2023-03-24
32 changes: 31 additions & 1 deletion tests/unit/ParserTest.php
Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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();
}
Expand Down

0 comments on commit 3da52d8

Please sign in to comment.