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

Refactoring & Return nested Sheet Collection instead of flat Rows #24

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
139 changes: 107 additions & 32 deletions src/Importable.php
Expand Up @@ -3,6 +3,7 @@
namespace Rap2hpoutre\FastExcel;

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Reader\ReaderInterface;
use Illuminate\Support\Collection;

/**
Expand Down Expand Up @@ -32,50 +33,124 @@ abstract protected function getType($path);
abstract protected function setOptions(&$reader_or_writer);

/**
* @param string $path
* @param callable|null $callback
* @param string $path
* @param callable|null $rowCallback
* @param callable|null $filterSheetsCallback: boolean
* @param callable|null $sheetsCallback
*
* @return Collection of Collections (= Sheets) of their respective Row objects
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
*
* @return Collection
*/
public function import($path, callable $callback = null)
public function import($path, callable $rowCallback = null, callable $filterSheetsCallback = null, callable $sheetsCallback = null)
{
$headers = [];
$collection = [];
$count_header = 0;

$reader = ReaderFactory::create($this->getType($path));
$this->setOptions($reader);
/* @var \Box\Spout\Reader\ReaderInterface $reader */
$reader->open($path);
$reader = $this->openReader($path);
/**
* TODO: consider dropping the hardcoded $sheet_number variable.
*/
$sheets = $this->importSheets($path, $sheetsCallback, $reader)
->filter(function($sheet, $key) use ($filterSheetsCallback)
{
if(!is_null($filterSheetsCallback))
{
return $filterSheetsCallback($sheet, $key);
}
return $this->sheet_number == $key;
})
->map(function($sheet, $key) use ($rowCallback)
{
$headers = [];
$collection = [];
$count_header = 0;

foreach ($reader->getSheetIterator() as $key => $sheet) {
if ($this->sheet_number != $key) {
continue;
}
if ($this->with_header) {
foreach ($sheet->getRowIterator() as $k => $row) {
if ($k == 1) {
$headers = $row;
$count_header = count($headers);
continue;
}
if ($count_header > $count_row = count($row)) {
$row = array_merge($row, array_fill(0, $count_header - $count_row, null));
if($this->with_header)
{
foreach($sheet->getRowIterator() as $k => $row)
{
if($k == 1)
{
$headers = $row;
$count_header = count($headers);
continue;
}
if($count_header > $count_row = count($row))
{
$row = array_merge($row, array_fill(0, $count_header - $count_row, null));
}
$collection[] = $rowCallback ? $rowCallback(array_combine($headers, $row)) : array_combine($headers, $row);
}
$collection[] = $callback ? $callback(array_combine($headers, $row)) : array_combine($headers, $row);
}
} else {
foreach ($sheet->getRowIterator() as $row) {
$collection[] = $row;
else
{
foreach($sheet->getRowIterator() as $row)
{
$collection[] = $row;
}
}

$sheet->sheetName = $sheet->getName();
$sheet->sheetData = collect($collection);

return $sheet;
});

$reader->close();

return $sheets;
}

/**
* @param string $path
* @param callable|null $callback
* @param ReaderInterface $reader
*
* @return Collection of \Box\Spout\Reader\SheetInterface instances
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
*/
public function importSheets($path, callable $callback = null, ReaderInterface $reader = null)
{
$sheets = collect();

$tempReader = $reader ?: $this->openReader($path);

foreach ($tempReader->getSheetIterator() as $key => $sheet) {

if($callback)
{
$sheets->put($key, $callback($key, $sheet));
continue;
}
$sheets->put($key, $sheet);
}
$reader->close();

return collect($collection);
/**
* If the following condition is met, the reader was not passed but instantiated locally and needs to be closed.
*/
if(is_null($reader))
{
$tempReader->close();
}

return $sheets;
}


/**
* @param $path
* @return \Box\Spout\Reader\ReaderInterface $reader
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
*/
private function openReader($path)
{
$reader = ReaderFactory::create($this->getType($path));
$this->setOptions($reader);

$reader->open($path);

return $reader;
}
}