git init
This commit is contained in:
30
app/Services/CsvReader.php
Normal file
30
app/Services/CsvReader.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class CsvReader {
|
||||
private $file;
|
||||
private array $headers;
|
||||
|
||||
public array $lines = [];
|
||||
|
||||
public function __construct(
|
||||
public string $filename,
|
||||
)
|
||||
{
|
||||
$this->file = fopen(public_path($filename), "r");
|
||||
if (!$this->file) {
|
||||
throw new \RuntimeException("Failed to open file: " . $filename);
|
||||
}
|
||||
|
||||
$this->headers = $this->readNextLine();
|
||||
}
|
||||
|
||||
public function readNextLine(): ?array
|
||||
{
|
||||
if (($data = fgetcsv($this->file, 1000, ",")) !== FALSE) {
|
||||
return $data;
|
||||
}
|
||||
return null; // End of file or error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user