Installation:
composer require ajgl/csv
No additional configuration is required—just autoload the package.
First Use Case:
use Ajgl\Csv\Reader;
$reader = new Reader('path/to/file.csv');
$rows = $reader->getRows(); // Returns associative array of rows
use Ajgl\Csv\Writer;
$writer = new Writer('path/to/output.csv');
$writer->insertRow(['col1', 'col2', 'col3']);
$writer->insertRow(['data1', 'data2', 'data3']);
$writer->close(); // Flushes remaining data
Where to Look First:
Ajgl\Csv\Reader and Ajgl\Csv\Writer classes for core functionality.Ajgl\Csv\Rfc4180\Reader/Writer for strict RFC 4180 compliance.Reading CSV Data:
getRows() with a callback for memory efficiency:
$reader = new Reader('large_file.csv');
$reader->getRows(function ($row) {
// Process row-by-row (avoids loading entire file into memory)
});
$headers = array_shift($reader->getRows());
Writing CSV Data:
$writer = new Writer('output.csv');
foreach ($data as $row) {
$writer->insertRow($row);
}
$writer->close();
Rfc4180\Writer for strict compliance or override defaults:
$writer = new Writer('output.csv', ',', '"', '\\');
Integration with Laravel:
$this->app->bind('csv.reader', function () {
return new Reader(config('csv.path'));
});
use Ajgl\Csv\Reader;
class ImportCommand extends Command {
protected function handle() {
$reader = new Reader(storage_path('app/import.csv'));
// Process rows...
}
}
Validation & Transformation:
$writer = new Writer('output.csv');
foreach ($data as $row) {
if ($this->isValidRow($row)) {
$writer->insertRow($this->transformRow($row));
}
}
Memory Management:
getRows() with a callback) for files >1MB.Encoding Issues:
$reader = new Reader('file.csv', ',', '"', '\\', 'ISO-8859-1');
Line Endings:
\r\n) vs. Unix (\n) line endings can cause parsing errors. Use Rfc4180\Writer for consistency.Closed Writers:
$writer->close() to flush buffered data. Forgetting this may truncate output files.Deprecated Methods:
array_merge_recursive in older PHP versions).Validate CSV Structure:
Rfc4180\Reader to enforce strict compliance:
$reader = new \Ajgl\Csv\Rfc4180\Reader('file.csv');
try {
$rows = $reader->getRows();
} catch (\Ajgl\Csv\Exception\CsvException $e) {
// Handle malformed CSV (e.g., unescaped delimiters)
}
Log Warnings:
try {
$writer->insertRow($row);
} catch (\Exception $e) {
Log::warning("CSV write failed: " . $e->getMessage());
}
Test Edge Cases:
"field, with, commas").Custom Writers/Readers:
Ajgl\Csv\AbstractReader/AbstractWriter for custom logic:
class MyReader extends \Ajgl\Csv\AbstractReader {
public function getRows() {
$rows = parent::getRows();
return array_map([$this, 'transformRow'], $rows);
}
}
Event Hooks:
insertRow() to add pre/post-processing:
$writer = new Writer('output.csv');
$writer->insertRow = function ($row) {
$row['processed_at'] = now()->toDateTimeString();
parent::insertRow($row);
};
Laravel Service Providers:
Reader::macro('toCollection', function () {
return collect($this->getRows());
});
Fallback for Native Functions:
Rfc4180 implementations if fgetcsv()/fputcsv() behave unexpectedly (e.g., with complex delimiters).How can I help you explore Laravel packages today?