Skip to content

Commit

Permalink
Feature/is empty when in model importer (#3828)
Browse files Browse the repository at this point in the history
* Add isEmptyWhen check to ModelImporter

* Formatting

* Test added

* Test added

* StyleCI

* fix: test

* fix: test
  • Loading branch information
quantumwebco authored Jun 19, 2023
1 parent 9bb6a56 commit 9e5b7ca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Imports/ModelImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function import(Worksheet $worksheet, ToModel $import, int $startRow = 1)
if (!$import instanceof SkipsEmptyRows || ($import instanceof SkipsEmptyRows && !$row->isEmpty($withCalcFormulas))) {
$rowArray = $row->toArray(null, $withCalcFormulas, $formatData, $endColumn);

if ($import instanceof SkipsEmptyRows && method_exists($import, 'isEmptyWhen') && $import->isEmptyWhen($rowArray)) {
continue;
}

if ($withValidation) {
$rowArray = $import->prepareForValidation($rowArray, $row->getIndex());
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Concerns/SkipsEmptyRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,35 @@ public function isEmptyWhen(array $row)
$import->import('import-empty-rows.xlsx');
$this->assertTrue($import->called);
}

/**
* @test
*/
public function custom_skips_rows_when_importing_to_model()
{
$import = new class implements SkipsEmptyRows, ToModel
{
use Importable;

public $called = false;

/**
* @param array $row
*/
public function model(array $row)
{
Assert::assertEquals('Not empty', $row[0]);
}

public function isEmptyWhen(array $row): bool
{
$this->called = true;

return $row[0] === 'Empty';
}
};

$import->import('skip-empty-rows-with-is-empty-when.xlsx');
$this->assertTrue($import->called);
}
}
Binary file not shown.

0 comments on commit 9e5b7ca

Please sign in to comment.