League\Csv 9 is out

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

I’m happy to announce that League\Csv version 9.0 has been released. This is a new major version which requires PHP7 (HHVM support is dropped). This new version focuses on making the code more SOLID while enabling better CSV document manipulation.

What’s new

The library comes bundle with two new stream filters.

League\Csv\CharsetConverter which converts on the fly your CSV document from one charset to another.

League\Csv\RFC4180Field which format on reading or on writing CSV field to be RFC4180 compliant by working around PHP’s bugs.

What’s changed

At the beginning, this library was just a set of 2 classes Reader and Writer that made it painless to interact with CSV documents. But as features were added those classes inherited too many responsabilities making them more difficult to use. By taking advantage of modern PHP features both classes API have been changed and/or improve and their responsabilities restricted:

– The Writer connection primary responsability is to add records to the CSV records;
– The Reader connection primary responsability is to access all CSV records;

Everything else (converting, querying) has been delegated to new classes.

Fetching CSV records

Previously, accessing the document records was done using the Reader object and different methods (ie: Reader::fetchAll, Reader::fetchAssoc, Reader::each, Reader::fetch). In version 9,  you will only need the newly introduced Reader::getRecords method. As such accessing CSV records is easier than before.

Of note, unlike previous version, the Reader now automatically strip the BOM sequence if present.

Querying the CSV document

Searching the CSV document has been decoupled from the Reader class. This means that if you are processing multiple CSV documents you don’t need to repeat your selection on each CSV object or on the same CSV if you need to make multiple queries on it.

Converting the CSV document

Starting with version 9, only the Reader class can be converted or serialized. XML and HTML conversion is done using dedicated classes. For instance, to convert your CSV records to XML you are now required to use the League\Csv\XMLConverter. Not only, This new class improves how the conversion is done by adding more configuration settings but it can be used for any tabular data like the result of a PDO::Statement.

Upgrading to League\Csv 9

As you would expect, this is only a quick view of the new features and changes this new version offers. You should head to the documentation website for a full explanation about the new version capabilities.

Even though this package introduces many backward compatibility breaks. I’m confident that upgrading to League\Csv 9 won’t require changes for most users. If you do encounter an issue the upgrading guide should help you ease the transition path.

Last but not least

The League\Csv is an open source project with a MIT License so contributions are more than welcome and will be fully credited. These contributions can be anything from reporting an issue, requesting or adding missing features or simply improving or correcting some typo on the documentation website.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.