goodby / csv

Goodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8. 3. Ready to Use for Enterprise Applications Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.
MIT License
953 stars 147 forks source link

Proper way to abort export() inside callback? #86

Closed mikyk10 closed 1 year ago

mikyk10 commented 4 years ago

Hi guys,

I want to cap the rows of CSV to certain number and I am not sure if I could safely stop the export process from a callback function, not using exit. Then I came up with throwing an exception to stop the function.

How would you think about this?

Here's example code.

$collection = new CallbackCollection($data, function($row) {
    // apply custom format to the row
    $row[1] = $row[1] . '!';

    // if the data size exceeds a limit
    if (...) {
        throw new \Exception('ABORTED');
    }

    return $row;
});

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('php://stdout', $collection);