Skip to content

Commit

Permalink
fix issue #26 and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed May 28, 2018
1 parent 5ccfe0b commit 8f49bfa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
31 changes: 22 additions & 9 deletions src/Exportable.php
Expand Up @@ -28,7 +28,7 @@ abstract protected function getType($path);
abstract protected function setOptions(&$reader_or_writer);

/**
* @param string $path
* @param string $path
* @param callable|null $callback
*
* @throws \Box\Spout\Common\Exception\IOException
Expand Down Expand Up @@ -65,7 +65,7 @@ public function download($path, callable $callback = null)

/**
* @param $path
* @param string $function
* @param string $function
* @param callable|null $callback
*
* @throws \Box\Spout\Common\Exception\IOException
Expand Down Expand Up @@ -106,19 +106,32 @@ protected function prepareCollection()
{
$need_conversion = false;
$first_row = $this->data->first();

if (!$first_row) {
return;
}

foreach ($first_row as $item) {
if (!is_string($item)) {
$need_conversion = true;
}
}
if ($need_conversion) {
$this->data->transform(function ($data) {
return collect($data)->map(function ($value) {
return is_int($value) || is_float($value) ? (string) $value : $value;
})->filter(function ($value) {
return is_string($value);
});
});
$this->transform();
}
}

/**
* Transform the collection
*/
private function transform()
{
$this->data->transform(function ($data) {
return collect($data)->map(function ($value) {
return is_int($value) || is_float($value) ? (string)$value : $value;
})->filter(function ($value) {
return is_string($value);
});
});
}
}
55 changes: 34 additions & 21 deletions src/Importable.php
Expand Up @@ -3,6 +3,7 @@
namespace Rap2hpoutre\FastExcel;

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

/**
Expand Down Expand Up @@ -43,10 +44,6 @@ abstract protected function setOptions(&$reader_or_writer);
*/
public function import($path, callable $callback = null)
{
$headers = [];
$collection = [];
$count_header = 0;

$reader = ReaderFactory::create($this->getType($path));
$this->setOptions($reader);
/* @var \Box\Spout\Reader\ReaderInterface $reader */
Expand All @@ -56,26 +53,42 @@ public function import($path, callable $callback = null)
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));
}
$collection[] = $callback ? $callback(array_combine($headers, $row)) : array_combine($headers, $row);
$collection = $this->importSheet($sheet, $callback);
}
$reader->close();

return collect($collection ?? []);
}

/**
* @param SheetInterface $sheet
* @param callable $callback
*
* @return array
*/
private function importSheet(SheetInterface $sheet, $callback)
{
$headers = [];
$collection = [];
$count_header = 0;

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

return collect($collection);
return $collection;
}
}
18 changes: 18 additions & 0 deletions tests/IssuesTest.php
Expand Up @@ -68,4 +68,22 @@ public function testIssue19()
$this->assertEquals(collect([['a' => '1', 'b' => 'n', 'c' => '1.32']]), (new FastExcel())->import(__DIR__.'/test2.xlsx'));
unlink($path);
}

/**
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
*/
public function testIssue26()
{
chdir(__DIR__);
foreach([ [[]], null, [null] ] as $value) {
$path = (new FastExcel($value))->export('test2.xlsx');
$this->assertEquals(collect([]), (new FastExcel())->import(__DIR__.'/test2.xlsx'));
unlink($path);
}

}
}

0 comments on commit 8f49bfa

Please sign in to comment.