Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getHeaders() #52

Merged
merged 7 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ $rows = SimpleExcelReader::create($pathToCsv)
});
```

#### Retrieving Header Row values

If you would like to retrieve the header row as an array, you can use the `getHeaders()` method.

```php
$headers = SimpleExcelReader::create($pathToCsv)->getHeaders();

// $headers will contain
// [ 'email', 'first_name' ]
```

#### Trimming Header Row values

If the file you are reading contains a title row, but you need to trim additional characters on the title values, then you should use the `trimHeaderRow()` method.
Expand Down
32 changes: 32 additions & 0 deletions src/SimpleExcelReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class SimpleExcelReader
{
protected string $path;

protected string $type;

protected ReaderInterface $reader;

protected IteratorInterface $rowIterator;
Expand All @@ -28,6 +30,8 @@ class SimpleExcelReader

protected ?Closure $formatHeadersUsing = null;

protected ?array $headers = null;

protected int $skip = 0;

protected int $limit = 0;
Expand All @@ -43,6 +47,8 @@ public function __construct(string $path, string $type = '')
{
$this->path = $path;

$this->type = $type;

$this->reader = $type ?
ReaderFactory::createFromType($type) :
ReaderEntityFactory::createReaderFromFile($this->path);
Expand Down Expand Up @@ -137,6 +143,7 @@ public function getRows(): LazyCollection

if ($this->processHeader) {
$this->headers = $this->processHeaderRow($firstRow->toArray());

$this->rowIterator->next();
}

Expand All @@ -154,6 +161,31 @@ public function getRows(): LazyCollection
});
}

public function getHeaders(): ?array
{
if ($this->processHeader && ! $this->headers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a big piece of indented code here. I can be simplified by first checking if headers is set to something. If it is, we can already return the headers.

$reader = $this->type ?
ReaderFactory::createFromType($this->type) :
ReaderEntityFactory::createReaderFromFile($this->path);

$reader->open($this->path);

$reader->getSheetIterator()->rewind();

$sheet = $reader->getSheetIterator()->current();

$this->rowIterator = $sheet->getRowIterator();

$this->rowIterator->rewind();

/** @var \Box\Spout\Common\Entity\Row $firstRow */
$firstRow = $this->rowIterator->current();

$this->headers = $this->processHeaderRow($firstRow->toArray());
}
return $this->headers;
}

public function close()
{
$this->reader->close();
Expand Down
97 changes: 97 additions & 0 deletions tests/SimpleExcelReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public function it_can_work_with_a_file_where_the_row_is_too_short()
], $rows);
}

/** @test */
public function it_can_retrieve_the_headers()
{
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv'))
->getHeaders();

$this->assertEquals([
0 => 'email',
1 => 'first_name',
2 => 'last_name'
], $headers);
}

/** @test */
public function it_can_ignore_the_headers()
{
Expand Down Expand Up @@ -105,6 +118,16 @@ public function it_can_ignore_the_headers()
], $rows);
}

/** @test */
public function it_doesnt_return_headers_when_headers_are_ignored()
{
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv'))
->noHeaderRow()
->getHeaders();

$this->assertEquals(null, $headers);
}

/** @test */
public function it_can_use_an_alternative_delimiter()
{
Expand Down Expand Up @@ -197,6 +220,35 @@ public function it_can_call_getRows_twice()
$this->assertNotNull($firstRowAgain);
}

/** @test */
public function it_can_call_getRows_after_getHeaders()
{
$reader = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv'));

$headers = $reader->getHeaders();

$this->assertEquals([
0 => 'email',
1 => 'first_name',
2 => 'last_name'
], $headers);

$rows = $reader->getRows()->toArray();

$this->assertEquals([
[
'email' => '[email protected]',
'first_name' => 'john',
'last_name' => 'doe',
],
[
'email' => '[email protected]',
'first_name' => 'mary jane',
'last_name' => 'doe',
],
], $rows);
}

/** @test */
public function it_can_call_first_on_the_collection_twice()
{
Expand Down Expand Up @@ -239,6 +291,20 @@ public function it_can_trim_the_header_row_names()
], $rows);
}

/** @test */
public function it_can_retrieve_trimmed_header_row_names()
{
$headers = SimpleExcelReader::create($this->getStubPath('header-with-spaces.csv'))
->trimHeaderRow()
->getHeaders();

$this->assertEquals([
0 => 'email',
1 => 'first_name',
2 => 'last_name',
], $headers);
}

/** @test */
public function it_can_trim_the_header_row_names_with_alternate_characters()
{
Expand Down Expand Up @@ -285,6 +351,21 @@ public function it_can_convert_headers_to_snake_case()
], $rows);
}

/** @test */
public function it_can_retrieve_headers_converted_to_snake_case()
{
$headers = SimpleExcelReader::create($this->getStubPath('headers-not-snake-case.csv'))
->headersToSnakeCase()
->getHeaders();

$this->assertEquals([
0 => 'email',
1 => 'first_name',
2 => 'last_name',
3 => 'job_title',
], $headers);
}

/** @test */
public function it_can_use_custom_header_row_formatter()
{
Expand All @@ -308,4 +389,20 @@ public function it_can_use_custom_header_row_formatter()
],
], $rows);
}

/** @test */
public function it_can_retrieve_headers_with_a_custom_formatter()
{
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv'))
->formatHeadersUsing(function ($header) {
return $header . '_suffix';
})
->getHeaders();

$this->assertEquals([
0 => 'email_suffix',
1 => 'first_name_suffix',
2 => 'last_name_suffix',
], $headers);
}
}