Skip to content

Commit e3d2bdb

Browse files
committed
wip
1 parent c30e90b commit e3d2bdb

File tree

5 files changed

+144
-4
lines changed

5 files changed

+144
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"require": {
2020
"php": "^8.1",
2121
"illuminate/contracts": "^9.0",
22+
"league/csv": "^9.8",
2223
"livewire/livewire": "^2.10",
2324
"spatie/laravel-package-tools": "^1.9.2"
2425
},

src/Concerns/HasCsvProperties.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Concerns;
4+
5+
use League\Csv\Reader;
6+
use League\Csv\Statement;
7+
use League\Csv\TabularDataReader;
8+
9+
trait HasCsvProperties
10+
{
11+
/**
12+
* Read CSV Property
13+
*
14+
* @param void
15+
* @return Reader
16+
*/
17+
public function getReadCsvProperty(): Reader
18+
{
19+
dd($this->file);
20+
return $this->readCSV($this->file->getRealPath());
21+
}
22+
23+
/**
24+
* Get CSV Records Property
25+
*
26+
* @param void
27+
* @return TabularDataReader
28+
*/
29+
public function getCsvRecordsProperty(): TabularDataReader
30+
{
31+
return Statement::create()->process($this->readCsv);
32+
}
33+
34+
/**
35+
* Update File Headers
36+
*
37+
* @param void
38+
* @return array
39+
*/
40+
public function updateFileHeaders(): array
41+
{
42+
$this->fileHeaders = $this->readCsv->getHeaders();
43+
44+
return $this->fileHeaders;
45+
}
46+
47+
/**
48+
* Update File Row Count
49+
*
50+
* @param void
51+
* @return int
52+
*/
53+
public function updateFileRowcount(): int
54+
{
55+
$this->fileRowCount = count($this->csvRecords);
56+
57+
return $this->fileRowCount;
58+
}
59+
}

src/Concerns/InteractsWithFiles.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Concerns;
4+
5+
use League\Csv\Reader;
6+
7+
trait InteractsWithFiles
8+
{
9+
/**
10+
* Read CSV File.
11+
*
12+
* @param string $path
13+
* @return Reader
14+
*/
15+
protected function readCSV(string $path): Reader
16+
{
17+
$stream = fopen($path, 'r');
18+
$csv = Reader::createFromStream($stream);
19+
20+
$csv->setHeaderOffset(0);
21+
22+
return $csv;
23+
}
24+
25+
}

src/Http/Livewire/ImportCsv.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
namespace Coderflex\LaravelCsv\Http\Livewire;
44

5-
use Coderflex\LaravelCsv\Concerns\InteractsWithColumns;
5+
use Coderflex\LaravelCsv\Concerns;
66
use Livewire\Component;
77
use Livewire\WithFileUploads;
88

99
class ImportCsv extends Component
1010
{
1111
use WithFileUploads;
12-
use InteractsWithColumns;
12+
use Concerns\InteractsWithColumns;
13+
use Concerns\InteractsWithFiles;
14+
use Concerns\HasCsvProperties;
1315

1416
/** @var string */
1517
public string $model;
1618

17-
/** @var string */
18-
public string $file;
19+
/** @var $file */
20+
public $file;
1921

2022
/** @var array */
2123
public array $columnsToMap = [];
@@ -26,15 +28,40 @@ class ImportCsv extends Component
2628
/** @var array */
2729
public array $columnLabels = [];
2830

31+
/** @var array $fileHeaders */
32+
public array $fileHeaders = [];
33+
34+
/** @var int $fileRowcount */
35+
public int $fileRowcount = 0;
36+
2937
public function mount()
3038
{
39+
// map and coverts the columnsToMap property into an associative array
3140
$this->columnsToMap = $this->mapThroughColumns();
3241

42+
// map and coverts the requiredColumns property int key => value array
3343
$this->columnLabels = $this->mapThroughColumnLabels();
3444

45+
// map and coverts the requiredColumns property int key => required value
3546
$this->requiredColumns = $this->mapThroughRequiredColumns();
3647
}
3748

49+
public function updatedFile()
50+
{
51+
$this->validateOnly('file');
52+
53+
$this->updateFileHeaders();
54+
55+
$this->updateFileRowcount();
56+
57+
$this->resetValidation();
58+
}
59+
60+
public function upload()
61+
{
62+
$this->updatedFile();
63+
}
64+
3865
public function render()
3966
{
4067
return view('laravel-csv::livewire.import-csv');

tests/ImportCsvTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
use Coderflex\LaravelCsv\Http\Livewire\ImportCsv;
44
use Coderflex\LaravelCsv\Tests\Models\Customer;
5+
use Illuminate\Http\UploadedFile;
6+
use Illuminate\Support\Facades\Storage;
7+
58
use function Pest\Livewire\livewire;
69

710
it('renders import CSV component', function () {
@@ -118,3 +121,28 @@
118121
'columnsToMap.email' => 'email',
119122
]);
120123
});
124+
125+
it('returns csv headers & row counts when upload a file', function () {
126+
Storage::fake('documents');
127+
128+
$file = UploadedFile::fake()
129+
->createWithContent(
130+
'customers.csv',
131+
file_get_contents('Data/customers.csv', true)
132+
);
133+
134+
$model = Customer::class;
135+
136+
livewire(ImportCsv::class, [
137+
'model' => $model,
138+
'file' => $file,
139+
])
140+
->call('upload')
141+
// ->call('updateFileRowcount')
142+
->assertSet('model', $model)
143+
->assertSet('file', $file)
144+
->assertSet('fileHeaders', [
145+
"id", "first_name", "last_name", "email", "company", "vip", "birthday", "created_at", "updated_at"
146+
])
147+
->assertSet('csvRowCount', 3);
148+
});

0 commit comments

Comments
 (0)