Skip to content

Commit

Permalink
Feat(CSV): Add a skipFirstRow option (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Dec 21, 2023
1 parent 771ead6 commit d7da839
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Iterator/CSVIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@
final readonly class CSVIterator implements IteratorAggregate
{
/**
* @var array{delimiter: string, enclosure: string, escapeString: string, columns: 'auto'|string[]|null, normalizers: ValueNormalizerInterface[]}
* @var array{
* delimiter: string,
* enclosure: string,
* escapeString: string,
* columns: 'auto'|string[]|null,
* normalizers: ValueNormalizerInterface[],
* skipFirstRow: bool,
* }
*/
private array $options;

/**
* @param Traversable<string> $text
* @param array{delimiter?: string, enclosure?: string, escapeString?: string, columns?: 'auto'|string[]|null, normalizers?: ValueNormalizerInterface[]} $options
* @param Traversable<string> $text
* @param array{
* delimiter?: string,
* enclosure?: string,
* escapeString?: string,
* columns?: 'auto'|string[]|null,
* normalizers?: ValueNormalizerInterface[],
* skipFirstRow?: bool,
* } $options
*/
public function __construct(
private Traversable $text,
Expand All @@ -50,6 +64,7 @@ public function __construct(
new NumericStringToNumberNormalizer(),
new EmptyStringToNullNormalizer(),
],
'skipFirstRow' => false,
]);
$resolver->setAllowedTypes('delimiter', 'string');
$resolver->setAllowedTypes('enclosure', 'string');
Expand All @@ -59,6 +74,7 @@ public function __construct(
$resolver->setAllowedValues('columns', function (array|string|null $value) {
return 'auto' === $value || null === $value || is_array($value);
});
$resolver->setAllowedTypes('skipFirstRow', 'bool');
$this->options = $resolver->resolve($options);
}

Expand Down Expand Up @@ -92,6 +108,11 @@ public function getIterator(): Traversable
return $this->iterateFromContent($this->text);
}

private function shouldSkipFirstRow(): bool
{
return $this->options['skipFirstRow'] || 'auto' === $this->options['columns'];
}

/**
* @return Traversable<mixed>
*/
Expand All @@ -112,7 +133,7 @@ private function iterateFromFile(SplFileObject $file): Traversable
if ([null] === $fields) {
continue;
}
if ('auto' === $this->options['columns'] && 0 === $file->key()) {
if (0 === $file->key() && $this->shouldSkipFirstRow()) {
$columns ??= $fields;
continue;
}
Expand All @@ -139,7 +160,7 @@ private function iterateFromContent(Traversable $content): Traversable
$this->options['enclosure'],
$this->options['escapeString'],
);
if ('auto' === $this->options['columns'] && 0 === $r) {
if (0 === $r && $this->shouldSkipFirstRow()) {
$columns ??= $fields;
continue;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Iterator/CSVIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@
yield 'file' => new CSVIterator(new SplFileObject($filename), ['columns' => $columns]);
});

it('skips the 1st row when asked to', function (CSVIterator $iterator) {
$rows = [...$iterator];

expect($rows[0])->toBe([
'cityEnglishName' => 'New York',
'cityLocalName' => 'New York',
'countryIsoCode' => 'US',
'continent' => 'North America',
'population' => 8537673,
])
->and($rows[2])->toBe([
'cityEnglishName' => 'Tokyo',
'cityLocalName' => '東京',
'countryIsoCode' => 'JP',
'continent' => 'Asia',
'population' => 13929286,
]);
})->with(function () {
$columns = [
'cityEnglishName',
'cityLocalName',
'countryIsoCode',
'continent',
'population',
];
$filename = dirname(__DIR__, 2).'/Data/10-biggest-cities.csv';
yield 'string content' => new CSVIterator(new StrTokIterator(file_get_contents($filename)), ['columns' => $columns, 'skipFirstRow' => true]);
yield 'file' => new CSVIterator(new SplFileObject($filename), ['columns' => $columns, 'skipFirstRow' => true]);
});

it('adds fields when the row has not enough columns', function (CSVIterator $iterator) {
$rows = [...$iterator];

Expand Down

0 comments on commit d7da839

Please sign in to comment.