Skip to content

Commit

Permalink
feature/skip-first-line-in-csv: add option skip_first_line
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Pasqualini committed Sep 29, 2018
1 parent 3c5f198 commit d9c7f50
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
10 changes: 7 additions & 3 deletions doc/step/csv_extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ extraire chaque ligne d'un fichier csv sous forme d'un tableau php

##### Options

| Nom | Description |
|----------|--------------------------------------------|
| filepath | chemin complet du fichier csv à parcourir |
| Nom | Description |
|-----------------|-------------------------------------------------|
| filepath | chemin complet du fichier csv à parcourir |
| delimiter | séparateur de colonnes |
| colums_names | nom des colonnes si différent de première ligne |
| skip_first_line | faut-il ignorer la première ligne ? |

##### Examples

Expand All @@ -18,4 +21,5 @@ options:
filepath: 'file.csv'
delimiter: ';'
colums_names: null # auto use first_line when use null
skip_first_line: false
```
4 changes: 2 additions & 2 deletions src/ImportBundle/Extractor/CsvExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(Slugify $slugify)
$this->slugify = $slugify;
}

public function extract(string $csvFilePath, string $delimiter = ',', array $columsNames = null): \Traversable
public function extract(string $csvFilePath, string $delimiter = ',', array $columsNames = null, bool $skipFirstLine = true): \Traversable
{
$csvFileObjects = new \SplFileObject($csvFilePath);
$csvFileObjects->setFlags((\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE | \ SplFileObject::READ_AHEAD));
Expand All @@ -29,7 +29,7 @@ public function extract(string $csvFilePath, string $delimiter = ',', array $col

foreach ($csvFileObjects as $loopIndex => $csvFileObjectRow) {
$currentData = (array) $csvFileObjectRow;
if ($csvFileObjects->key() > 0 && true === $this->isValidLine($currentData)) {
if ((!$skipFirstLine || $csvFileObjects->key() > 0) && true === $this->isValidLine($currentData)) {
$currentData = array_map('trim', $currentData);
$arrayToYield = array_combine($arrayKeys, $currentData);
yield $loopIndex => $arrayToYield;
Expand Down
12 changes: 7 additions & 5 deletions src/ImportBundle/Step/CsvExtractorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ public function __construct(CsvExtractor $extractor)

public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver
{
$resolver->setRequired(['filepath', 'delimiter', 'colums_names']);
$resolver->setRequired(['filepath', 'delimiter', 'colums_names', 'skip_first_line']);
$resolver->setDefault('skip_first_line', true);

return parent::configureOptionResolver($resolver);
}

public function execute(ProcessState $state)
{
$this->iterator = $this->extractor->extract(
$state->getOptions()['filepath'],
$state->getOptions()['delimiter'],
$state->getOptions()['colums_names']
$state->getOption('filepath'),
$state->getOption('delimiter'),
$state->getOption('colums_names'),
$state->getOption('skip_first_line', true)
);

$state->setContext('filepath', $state->getOptions()['filepath']);
$state->setContext('filepath', $state->getOption('filepath'));
}

public function next(ProcessState $state)
Expand Down
26 changes: 26 additions & 0 deletions tests/ImportBundle/Extractor/CsvExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

class CsvExtractorTest extends TestCase
{
/** @var CsvExtractor */
protected $extractor;

/** @var string */
private $filepath;

/**
Expand Down Expand Up @@ -53,6 +56,29 @@ public function testExtract()
'price' => '1.5',
],
], iterator_to_array($dataRows));
}

public function testExtractWhenNotSkipFirstLine()
{
$this->slugify->expects($this->any())
->method('slugify')
->willReturnArgument(0);
$dataRows = $this->extractor->extract($this->filepath, ';', null, false);

$this->assertEquals([
0 => [
'name' => 'name',
'price' => 'price',
],
1 => [
'name' => 'savon',
'price' => '2.32',
],

2 => [
'name' => 'lait',
'price' => '1.5',
],
], iterator_to_array($dataRows));
}
}
8 changes: 4 additions & 4 deletions tests/ImportBundle/Step/CsvExtractorStepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testExecute()
$this->extractor
->expects($this->once())
->method('extract')
->with('file.csv', ';', null)
->with('file.csv', ';', null, true)
->willReturn(new \ArrayIterator());

$this->step->execute($state);
Expand All @@ -69,7 +69,7 @@ public function testValid()
$this->extractor
->expects($this->once())
->method('extract')
->with('file.csv', ';', null)
->with('file.csv', ';', null, true)
->willReturn($iterator);

$state = new ProcessState(
Expand All @@ -90,7 +90,7 @@ public function testNext()
$this->extractor
->expects($this->once())
->method('extract')
->with('file.csv', ';', null)
->with('file.csv', ';', null, true)
->willReturn($iterator);

$state = new ProcessState(
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testGetProgress()
$this->extractor
->expects($this->once())
->method('extract')
->with('file.csv', ';', null)
->with('file.csv', ';', null, true)
->willReturn($iterator);

$state = new ProcessState(
Expand Down

0 comments on commit d9c7f50

Please sign in to comment.