Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error while trying to load a CSV file from String to an \Iterator #545

Closed
6 tasks done
barchard opened this issue Dec 31, 2024 · 3 comments
Closed
6 tasks done

Comments

@barchard
Copy link

barchard commented Dec 31, 2024

Bug Report

I may be holding it wrong, but I imagine we shouldn't throw an exception regardless. I am stuck trying to get a CSV (string) to load and parse. The same string works fine in PHPSpreadsheet. Thanks

Information Description
Version
MAMP PHP 8.2.0
macOS 15
league/csv 9.20

Summary

Trying to load a CSV file from string as an \Iterator

Standalone code, or other way to reproduce the problem

$reader = Reader::createFromString($this->get())
    ->setHeaderOffset(0)
    ->skipEmptyRecords();
return $reader->getIterator();

Expected result

Return a valid iterator.

Actual result

PHP Fatal Exception.

<b>Fatal error</b>
:  Uncaught TypeError: League\Csv\Reader::League\Csv\{closure}(): Argument #1 ($record) must be of type array, null given, called in /path/to/vendor/league/csv/src/MapIterator.php on line 52 and defined in /path/to/vendor/league/csv/src/Reader.php:600
Stack trace:
#0 /path/to/vendor/league/csv/src/MapIterator.php(52): League\Csv\Reader-&gt;League\Csv\{closure}(NULL, NULL)

Checks before submitting

  • Be sure that there isn't already an issue about this. See: Issues list
  • Be sure that there isn't already a pull request about this. See: Pull requests
  • I have added every step to reproduce the bug.
  • If possible I added relevant code examples.
  • This issue is about 1 bug and nothing more.
  • The issue has a descriptive title. For example: "JSON rendering failed on Windows for filenames with space".
@nyamsprod
Copy link
Member

nyamsprod commented Jan 1, 2025

@barchard what is the value returned by $this->get() could you provide a reproducible script. Without that I can not understand or even fix the problem if problem there is 🤔

@barchard
Copy link
Author

barchard commented Jan 1, 2025

Sure, here is a snippet from the network request results (below). I am playing around more. I see that:

// Works:
$rowIterator = ...; // return results from function above 
foreach ($rowIterator as $offset => $record) {
    error_log(__FILE__.' '.__LINE__.print_r($record, true));
    die;
}

// Doesn't work:
$rowIterator = ...; // return results from function above
$firstRow = $rowIterator->current();
error_log(__FILE__.' '.__LINE__.print_r($firstRow, true));

Sample snippet of CSV:

Manufacturer,Model,Color,Year,Engine Size (L),Mileage (KM),Mileage (M),Condition,Price (£),Price ($),More info,Notes
Lincoln,MKT,"<font color=""red"">Red</font>",2013,0.6,127835,79432.96,*Excellent*,14261.89,17848.76,[Manufacturer's website](http://tiny.cc),aliquet maecenas leo odio condimentum id luctus nec molestie sed justo pellentesque viverra pede
GMC,Savana 3500,"<font color=""blue"">Blue</font>",2006,0.4,150155,93301.96,Fair,9901.28,12391.45,[Dealer's website](https://admin.ch),nisi at nibh in hac habitasse platea dictumst aliquam augue quam sollicitudin vitae consectetuer
Pontiac,Firebird,"<font color=""green"">Green</font>",1995,1.6,90060,55960.67,Fair,33875.98,42395.79,[Handbook website](http://answers.com),lacus morbi quis tortor id nulla ultrices aliquet maecenas leo

Thanks

@nyamsprod
Copy link
Member

@barchard thanks for the script. What you are experimenting is expected. This is the same issue as #514 . You are not using PHP IteratorAggregate the right way.

The Reader class is a IteratorAggregate so to access it's data via that interface you are required to use

  • foreach
  • iterator_* functions
  • Other SPL Iterator structure which expects an IteratorAggregate class

Any use of IteratorAggregate::getIterator as an implicit unknown or undocumented behaviour which is why you are getting this error.

To resolve this issue you can use the following methods:

  • Reader::first or Reader::nth
  • Reader::firstAsObject or Reader::nthAsObject (if you want your returned data converted to objects)

All these methods are explained in the documentation.

or simply do as you did in your first example but avoid using the getIterator method directly. There's a more in depth explanation in the pinned issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants