nozavroni / csvelte

🕺🏻 CSV and Tabular Data library for PHP
http://phpcsv.com/
Other
6 stars 0 forks source link

Add __invoke() method to Reader and other classes #152

Open nozavroni opened 7 years ago

nozavroni commented 7 years ago

Add __invoke() magic method to Reader class and anywhere else you can think of that it would make sense. For the reader method it would make sense to implement it contextually if possible, but if not, then it would be a good "alias" for the "filter()" method. So that instead of...

$reader->addFilter(function($row){
    return $row['foo'] > 10;
});
foreach ($reader->filter() as $line_no => $row) {
    do_something_with($row);
}

You would do this:

$reader->addFilter(function($row){
    return $row['foo'] > 10;
});
foreach ($reader() as $line_no => $row) {
    do_something_with($row);
}

For the Writer class, it would be a good alias for writeRow() writeRows() depending on what you pass it. So you could do $writer($rows) instead of $writer->writeRows($rows);

I'm sure there are other classes that could benefit from this magic method as well. Just don't go overboard.