Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Csv Converter Laravel Package

agence-dnd-bundle/csv-converter

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require agence-dnd-bundle/csv-converter
    

    Ensure your Laravel project uses Symfony 4.4+ (compatible with Laravel 6+).

  2. First Use Case: Convert CSV to Array

    use AgenceDndBundle\CsvConverter\CsvConverter;
    
    $converter = new CsvConverter();
    $data = $converter->convertToArray('path/to/file.csv');
    
  3. Key Files

    • src/CsvConverter.php: Core converter logic.
    • src/Exception/: Custom exceptions (e.g., InvalidCsvException).

Implementation Patterns

Workflows

  1. CSV to Array (Common Use)

    $converter = new CsvConverter(['delimiter' => ';']);
    $rows = $converter->convertToArray('import.csv');
    
  2. Array to CSV (Export)

    $converter->convertToCsv($rows, 'export.csv');
    
  3. Streaming Large Files

    $converter->setStreaming(true);
    $converter->convertToArray('large_file.csv');
    

Integration Tips

  • Laravel Service Provider Bind the converter to the container for dependency injection:

    $this->app->singleton(CsvConverter::class, function ($app) {
        return new CsvConverter(['delimiter' => ';']);
    });
    
  • Form Request Validation Validate uploaded CSV files:

    use Illuminate\Http\Request;
    
    public function import(Request $request) {
        $request->validate([
            'file' => 'required|file|csv',
        ]);
    }
    
  • Queue Jobs for Heavy Processing Offload conversion to a queue:

    dispatch(new ConvertCsvJob($filePath, $destination));
    

Gotchas and Tips

Pitfalls

  1. Encoding Issues

    • Use iconv() or mb_convert_encoding() if encountering garbled text:
      $converter->setEncoding('UTF-8');
      
  2. Memory Limits

    • Disable streaming for small files to avoid overhead:
      $converter->setStreaming(false);
      
  3. Delimiter Assumptions

    • Defaults to ,; explicitly set for edge cases:
      $converter->setDelimiter(';');
      

Debugging

  • Log Errors Wrap conversions in try-catch:

    try {
        $data = $converter->convertToArray('file.csv');
    } catch (\Exception $e) {
        \Log::error("CSV Conversion Failed: " . $e->getMessage());
    }
    
  • Validate Headers Check for missing columns early:

    if (!isset($data[0]['expected_column'])) {
        throw new \RuntimeException("Missing required column");
    }
    

Extension Points

  1. Custom Parsers Extend CsvConverter to add logic:

    class CustomCsvConverter extends CsvConverter {
        protected function parseRow($row) {
            // Custom logic
            return parent::parseRow($row);
        }
    }
    
  2. Event Listeners Trigger events on conversion:

    $converter->onConvert(function ($data) {
        // Post-processing
    });
    
  3. Configuration Override defaults via constructor:

    $converter = new CsvConverter([
        'delimiter' => '|',
        'enclosure' => '"',
        'escape' => '\\',
    ]);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui