Skip to content

Commit

Permalink
check empty csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Aug 31, 2022
1 parent 52c8e28 commit d88bad8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/Concerns/HasCsvProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Coderflex\LaravelCsv\Concerns;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\MessageBag;
use League\Csv\Reader;
use League\Csv\Statement;
use League\Csv\TabularDataReader;
Expand Down Expand Up @@ -35,4 +37,27 @@ public function getCsvRecordsProperty(): TabularDataReader
{
return Statement::create()->process($this->readCsv);
}

/**
* Handle CSV Information properties from the given file
*
* @return array|Illuminate\Support\MessageBag
*/
public function handleCsvProperties(): array|MessageBag
{
try {
$fileHeaders = $this->readCsv->getHeader();
$fileRowCount = $this->csvRecords->count();

return [$fileHeaders, $fileRowCount];

} catch(\League\Csv\SyntaxError $exception) {
Log::warning($exception->getMessage());

return $this->addError(
'file_error',
__('Your CSV file has error/errors, or is empty. Please check, and try again')
);
}
}
}
6 changes: 4 additions & 2 deletions src/Concerns/InteractsWithCsvFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coderflex\LaravelCsv\Concerns;

use League\Csv\Reader;
use Throwable;

trait InteractsWithCsvFiles
{
Expand All @@ -16,8 +17,9 @@ protected function readCSV(string $path): Reader
{
$stream = fopen($path, 'r');
$csv = Reader::createFromStream($stream);

$csv->setHeaderOffset(0);

$csv->setHeaderOffset(0)
->skipEmptyRecords();

return $csv;
}
Expand Down
25 changes: 21 additions & 4 deletions src/Http/Livewire/ImportCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Coderflex\LaravelCsv\Concerns;
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;
use Livewire\Component;
use Livewire\WithFileUploads;

Expand Down Expand Up @@ -50,9 +52,8 @@ public function updatedFile()
{
$this->validateOnly('file');

$this->fileHeaders = $this->readCsv->getHeader();
$this->fileRowCount = count($this->csvRecords);

$this->setCsvProperties();

$this->resetValidation();
}

Expand All @@ -62,7 +63,7 @@ public function import()

$import = $this->createNewImport();

$chunks = (new ChunkIterator($this->csvRecords->getRecords(), 10))->get();
$chunks = (new ChunkIterator($this->csvRecords->getIterator(), 10))->get();
}

public function render()
Expand All @@ -82,6 +83,22 @@ protected function rules()
] + $this->requiredColumns;
}

protected function setCsvProperties()
{
$this->withValidator(function (Validator $validator) {
$validator->after(function ($validator) {
if ($this->handleCsvProperties() instanceof MessageBag) {
$validator->errors()->merge(
$this->handleCsvProperties()->getMessages()
);
}
});
})->validate();


[$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties();
}

protected function createNewImport()
{
return (new $this->model)->imports()->create([
Expand Down
2 changes: 1 addition & 1 deletion tests/ChunkIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

$statement = Statement::create()->process($file);

$chunks = (new ChunkIterator($statement->getRecords(), 10))->get();
$chunks = (new ChunkIterator($statement->getIterator(), 10))->get();

$result = collect($chunks);

Expand Down
18 changes: 18 additions & 0 deletions tests/ImportCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
->assertSuccessful();
});

it('throws a validation error if the csv file empty', function () {
$model = Customer::class;

$file = UploadedFile::fake()
->createWithContent(
'customers.csv',
file_get_contents('stubs/empty.csv', true)
);

livewire(ImportCsv::class, [
'model' => $model,
])
->set('file', $file)
->assertSet('model', $model)
->assertHasErrors(['file_error']);
});


it('transfers columnsToMap into an associative array', function () {
$columnsToMap = [
'name',
Expand Down

0 comments on commit d88bad8

Please sign in to comment.