{"id":2368,"date":"2015-02-19T14:21:00","date_gmt":"2015-02-19T12:21:00","guid":{"rendered":"http:\/\/nyamsprod.com\/blog\/?p=2368"},"modified":"2015-02-19T15:22:31","modified_gmt":"2015-02-19T13:22:31","slug":"league-csv-7","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/league-csv-7\/","title":{"rendered":"League\\Csv 7 is out"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p><a title=\"League\\Csv changelog\" href=\"https:\/\/github.com\/thephpleague\/csv\/tree\/7.0.0\/CHANGELOG.md\" target=\"_blank\">League\\Csv 7\u00a0<\/a> has been officially released! The new version is as simple and as powerful as before but adds more speed and flexibility to the package. Features introduced during the version 6.0 series are now stabilized and care was taken to improved the package manipulation of huge CSV files.<!--more--><\/p>\n<p>Installing <code>League\\Csv 7<\/code> using composer is as simple as typing the code below in your composer installed box.<\/p>\n<pre id=\"snippet_csv_composer\"><code class=\"language-javascript\">$ composer require league\\csv<\/code><\/pre>\n<p>This should install\/update your composer powered project to the latest stable version of the library.<\/p>\n<ul>\n<li>View the documentation at <a title=\"League\\Csv documentation\" href=\"http:\/\/csv.thephpleague.com\">http:\/\/csv.thephpleague.com<\/a><\/li>\n<li>View the source and contribute at <a title=\"League\\Csv source code on Github\" href=\"https:\/\/github.com\/thephpleague\/csv\" target=\"_blank\">https:\/\/github.com\/thephpleague\/csv<\/a><\/li>\n<\/ul>\n<h2>What is League\\Csv ?<\/h2>\n<p>For those non familiar with the library, it&#8217;s a set of two classes <code>League\\Csv\\Reader<\/code> and <code>League\\Csv\\Writer<\/code> that make it painless to interact with CSV documents if your are using a supported version of PHP. Let&#8217;s say you have some CSV data that you want to import into your database. <code>League\\Csv\\Reader<\/code> will help you do that as simple as possible with the following code:<\/p>\n<pre id=\"snippet_csv_each\"><code class=\"language-php\">&lt;?php\r\nuse League\\Csv\\Reader;\r\n\r\n\/\/We are going to insert some data into the users table\r\n$sth = $dbh-&gt;prepare(\r\n     &quot;INSERT INTO users (firstname, lastname, email) VALUES (:firstname, :lastname, :email)&quot;\r\n);\r\n\r\n$csv = Reader::createFromPath(&#039;\/path\/to\/your\/csv\/file.csv&#039;);\r\n$csv-&gt;setOffset(1); \/\/because we don&#039;t want to insert the header\r\n$nbInsert = $csv-&gt;each(function ($row) use (&amp;$sth) {\r\n    \/\/Do not forget to validate your data before inserting it in your database\r\n    $sth-&gt;bindValue(&#039;:firstname&#039;, $row[0], PDO::PARAM_STR);\r\n    $sth-&gt;bindValue(&#039;:lastname&#039;, $row[1], PDO::PARAM_STR);\r\n    $sth-&gt;bindValue(&#039;:email&#039;, $row[2], PDO::PARAM_STR);\r\n\r\n    return $sth-&gt;execute(); \/\/if the function return false then the iteration will stop\r\n});\r\necho $nbInsert; \/\/display the number of successfull inserts done<\/code><\/pre>\n<p>More examples and usage can be found in <a title=\"League\\Csv usage examples\" href=\"https:\/\/github.com\/thephpleague\/csv\/tree\/7.0.0\/examples\" target=\"_blank\">the examples directory<\/a> attached to the library on the github repository.<\/p>\n<h2>What&#8217;s changed ?<\/h2>\n<p><strong>Mac OS X users will now be required to update their PHP ini setting to allow the library to work as intended on their environment<\/strong>.<\/p>\n<p>Since version 5.3 the <code>auto_detecting_line_ending<\/code> PHP ini setting was activated by default by the library. The problem is that the library was messing with the developer environment without informing him which could lead to unexpected side effects without warning. By removing this setting, the developer gains back the full control over his environment.<\/p>\n<pre id=\"snippet_csv_ini_setting\"><code class=\"language-php\">if (! ini_get(&quot;auto_detect_line_endings&quot;)) {\r\n    ini_set(&quot;auto_detect_line_endings&quot;, &#039;1&#039;);\r\n}\r\n\r\n\/\/the rest of the code continue here...<\/code><\/pre>\n<p><strong>The Writer class has been rewritten to be faster and more flexible.<\/strong><\/p>\n<p>Previous version of the <code>League\\Csv\\Writer<\/code> class performed, out of the box, a number of validation and formatting that were slowing down row insertions. To solve this issue while still being able to control\/manipulate the inserted data version 7.0 introduce a new flexible mechanism illustrate below<\/p>\n<pre id=\"snippet_csv_formatter_validatiors\"><code class=\"language-php\">&lt;?php\r\n\r\nuse League\\Csv\\Writer;\r\nuse League\\Csv\\Exception\\InvalidRowException;\r\n\r\ntry {\r\n    $formatter = function (array $row) {\r\n        return array_map(&#039;strtoupper&#039;, $row);   \r\n    };\r\n    $validator = function (array $row) {\r\n        return 10 == count($row);\r\n    };\r\n    $writer = Writer::createFromPath(&#039;\/path\/to\/my\/file.csv&#039;);\r\n    $writer-&gt;addFormatter($formatter);\r\n    $writer-&gt;addValidator($validator, &#039;row_must_contain_10_cells&#039;);\r\n    \/\/$rows is a multidimentional array or a Traversable object\r\n    $writer-&gt;insertAll($rows);  \r\n} catch (InvalidRowException $e) {\r\n    $e-&gt;getName(); \/\/the name of the validator where the row failed &#039;row_must_contain_10_cells&#039;\r\n    $e-&gt;getData(); \/\/the data which failed the validator for instance [&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;]\r\n}<\/code><\/pre>\n<p>You can of course attach as many filters as you want. But keep in mind that more filters means slower data insertion.<\/p>\n<p>This is a major departure from previous versions and the migration guide will provide all the information needed to migrate your code if necessary.<\/p>\n<p><strong>You can no longer use classes default constructor to instantiated them. You are required to use one of the 3 named constructors.<\/strong><\/p>\n<p><a title=\"How to instantiate League\\Csv objects\" href=\"http:\/\/csv.thephpleague.com\/instantiation\/\" target=\"_blank\">Using named constructors<\/a> is the recommended way to instantiate the <code>Writer<\/code> and the <code>Reader<\/code> objects since version 6. We are now taking the next logical step to remove remaining ambiguities when creating a <code>League\\Csv<\/code> object by\u00a0 removing the default constructor from public API.<\/p>\n<p><strong>Conversion methods output can now be modified using the <code>League\\Csv\\Reader<\/code> query options<\/strong><\/p>\n<p>Conversion methods that transform your CSV into <code>JSON<\/code>, <code>XML<\/code> or <code>HTML<\/code> were introduced in the package prior to the <code>League\\Csv\\Reader<\/code> query options. As such they were restricted to work on the full CSV document only. Starting with version 7.0, a backward compatibility break is introduced to enable these methods to be more flexible <a title=\"Csv conversion methods are more flexible\" href=\"http:\/\/csv.thephpleague.com\/upgrading\/7.0\/#csv-conversion-methods\" target=\"_blank\">when used with the Reader<\/a>.<\/p>\n<pre id=\"snippet_csv_output\"><code class=\"language-php\">&lt;?php\r\n\r\nuse League\\Csv\\Reader;\r\n\r\n$reader = Reader::createFromPath(&#039;\/path\/to\/your\/csv\/file.csv&#039;);\r\n$reader-&gt;setOffset(1);\r\n$reader-&gt;setLimit(5);\r\njson_encode($reader); \/\/will only convert the 5 specified rows and not the full CSV like previously<\/code><\/pre>\n<p>Do keep in mind though, that the same restrictions to the use of query options still remain.<\/p>\n<p><strong>The <code>detectDelimiterList<\/code> method now returns the delimiter and its occurrence in the selected rows.<\/strong><\/p>\n<p>This method introduced in the last version returned an <code>array<\/code> containing the founded delimiters ordered by their occurrences but the occurrence information was not given. With this backward compatibility break the information is now available to the developer.<\/p>\n<pre id=\"snippet_csv_delimiterlist\"><code class=\"language-php\">use League\\Csv\\Reader;\r\n\r\n$reader = Reader::createFromPath(&#039;\/path\/to\/your\/csv\/file.csv&#039;);\r\n\r\n$delimiters_list = $reader-&gt;detectDelimiterList(10, [&#039; &#039;, &#039;|&#039;]);\r\nif (count($delimiters_list)) {\r\n\tforeach ($delimiters_list as $occurences =&gt; $delimiter) {\r\n\t\techo &quot;$delimiter appeared $occurences times in 10 CSV row&quot;, PHP_EOL;\r\n\t}\r\n\t\/\/$occurences can not be less than 1\r\n\t\/\/since it would mean that the delimiter is not used\r\n}\r\n\/\/if you are only interested in getting\r\n\/\/ the most use delimiter you can still do as follow\r\nif (count($delimiters_list)) {\r\n\t$delimiter = array_shift($delimiters);\r\n}<\/code><\/pre>\n<h2>Upgrading to League\\Csv 7<\/h2>\n<p>Even thought this package introduces many backward compatibility breaks. I&#8217;m confident that upgrading to <code>League\\Csv<\/code> 7 won&#8217;t require changes for most users. For a full list of the changes please refer to the<a title=\"League\\Csv changelog\" href=\"https:\/\/github.com\/thephpleague\/csv\/tree\/7.0.0\/CHANGELOG.md\" target=\"_blank\"> CHANGELOG.md<\/a> file attached to the library and see <a title=\"Upgrading from 6.0 to 7.0\" href=\"http:\/\/csv.thephpleague.com\/upgrading\/7.0\/\" target=\"_blank\">the upgrading guide<\/a> for more information.<\/p>\n<h2>Last but not least<\/h2>\n<p>The <code>League\\Csv<\/code> is an open source project with a MIT License so contributions are more than welcome and will be fully credited.\u00a0 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>League\\Csv 7 is out with a focus on better flexibility and faster CSV creation with a rewritten Writer class.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[761,784,785],"class_list":["post-2368","post","type-post","status-publish","format-standard","hentry","category-web","tag-csv","tag-speed","tag-thephpleague"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2368"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2368\/revisions"}],"predecessor-version":[{"id":2400,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2368\/revisions\/2400"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}