Skip to content

Commit

Permalink
Update the documentation for 9.6.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 11, 2020
1 parent 8b781ba commit 3d1c4f1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ All Notable changes to `Csv` will be documented in this file

### Removed

- polyfill for `is_iterable`
- polyfill for `is_nullable_int`
- Internal polyfill for `is_iterable`
- Internal polyfill for `is_nullable_int`
- Support for PHP7.0 and PHP7.1

## 9.5.0 - 2019-12-15
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"rss": "https://github.com/thephpleague/csv/releases.atom",
"source": "https://github.com/thephpleague/csv"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/nyamsprod"
}
],
"require": {
"php" : "^7.2.5",
"ext-json" : "*",
Expand Down
2 changes: 2 additions & 0 deletions docs/9.0/reader/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The `League\Csv\Reader` class extends the general connections [capabilities](/9.

<p class="message-notice">Prior to <code>9.1.0</code>, by default, the mode for a <code>Reader::createFromPath</code> is <code>r+</code> which looks for write permissions on the file and throws an <code>Exception</code> if the file cannot be opened with the permission set. For sake of clarity, it is strongly suggested to set <code>r</code> mode on the file to ensure it can be opened.</p>

<p class="message-info">Starting with version <code>9.6.0</code>, the class implements the <code>League\Csv\TabularDataReader</code> interface.</p>

## CSV example

Many examples in this reference require an CSV file. We will use the following file `file.csv` containing the following data:
Expand Down
31 changes: 17 additions & 14 deletions docs/9.0/reader/resultset.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ title: Accessing Records from a CSV document

A `League\Csv\ResultSet` object represents the associated result set of processing a [CSV document](/9.0/reader/) with a [constraint builder](/9.0/reader/statement/). This object is returned from [Statement::process](/9.0/reader/statement/#apply-the-constraints-to-a-csv-document) execution.

<p class="message-info">Starting with version <code>9.6.0</code>, the class implements the <code>League\Csv\TabularDataReader</code> interface.</p>

## Informations

### Accessing the result set column names
Expand All @@ -25,7 +27,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
$records->getHeader(); // is empty because no header information was given
~~~

Expand All @@ -37,7 +39,7 @@ use League\Csv\Reader;
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);

$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
$records->getHeader(); // returns ['First Name', 'Last Name', 'E-mail'];
~~~

Expand All @@ -50,7 +52,7 @@ use League\Csv\Statement;
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);

$records = (new Statement())->process($reader, ['Prénom', 'Nom', 'E-mail']);
$records = Statement::create()->process($reader, ['Prénom', 'Nom', 'E-mail']);
$records->getHeader(); // returns ['Prénom', 'Nom', 'E-mail'];
~~~

Expand All @@ -63,7 +65,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
count($records); //return the total number of records found
~~~

Expand All @@ -72,8 +74,9 @@ count($records); //return the total number of records found
### Description

~~~php
public ResultSet::getRecords(void): Iterator
public ResultSet::getRecords(array $header = []): Iterator
~~~
<p class="message-info">Starting with version <code>9.6.0</code>, the class implements the <code>ResultSet::getRecords</code> methods matches the same arguments and the same signature as the <code>Reader::getRecords</code> method.</p>

To iterate over each found records you can call the `ResultSet::getRecords` method which returns a `Generator` of all records found or directly use the `foreach` construct as the class implements the `IteratorAggregate` interface;

Expand All @@ -82,7 +85,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);

foreach ($records->getRecords() as $record) {
//do something here
Expand All @@ -103,7 +106,7 @@ use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
$records->getHeader(); //returns ['First Name', 'Last Name', 'E-mail']
foreach ($records as $record) {
// $records contains the following data
Expand Down Expand Up @@ -133,7 +136,7 @@ use League\Csv\Statement;
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);

$stmt = (new Statement())
$stmt = Statement::create()
->offset(10)
->limit(12)
;
Expand Down Expand Up @@ -163,7 +166,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
foreach ($records->fetchColumn(2) as $value) {
//$value is a string representing the value
//of a given record for the selected column
Expand All @@ -173,7 +176,7 @@ foreach ($records->fetchColumn(2) as $value) {
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);

$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
foreach ($records->fetchColumn('E-mail') as $value) {
//$value is a string representing the value
//of a given record for the selected column
Expand All @@ -188,7 +191,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
count($records); //returns 10;
count(iterator_to_array($records->fetchColumn(2), false)); //returns 5
//5 records were skipped because the column value is null
Expand All @@ -203,7 +206,7 @@ use League\Csv\Statement;
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$reader->setHeaderOffset(0);

$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);
foreach ($records->fetchColumn('foobar') as $record) {
//throw an Exception exception if
//no `foobar` column name is found
Expand Down Expand Up @@ -240,7 +243,7 @@ use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromString($str);
$records = (new Statement())->process($reader);
$records = Statement::create()->process($reader);

foreach ($records->fetchPairs() as $firstname => $lastname) {
// - first iteration
Expand Down Expand Up @@ -289,7 +292,7 @@ foreach ($records as $record) {
}

$reader = Reader::createFromFileObject($tmp)->setHeaderOffset(0);
$stmt = (new Statement())->offset(1)->limit(1);
$stmt = Statement::create()->offset(1)->limit(1);
$result = $stmt->process($reader);

echo '<pre>', PHP_EOL;
Expand Down
38 changes: 25 additions & 13 deletions docs/9.0/reader/statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ When building a constraint, the methods do not need to be called in any particul

<p class="message-info">Because the <code>Statement</code> object is independent of the <code>Reader</code> object it can be re-use on multiple <code>Reader</code> objects.</p>

<p class="message-info">Starting with version <code>9.6.0</code>, the class exposes the <code>Statement::create</code> named constructor to ease object creation.</p>

## Filtering constraint

The filters attached using the `Statement::where` method **are the first settings applied to the CSV before anything else**. This option follow the *First In First Out* rule.
Expand Down Expand Up @@ -109,23 +111,33 @@ Just like the `Reader:getRecords`, the `Statement::process` method takes an opti
use League\Csv\Reader;
use League\Csv\Statement;

function filterByEmail(array $record): bool
{
return (bool) filter_var($record[2], FILTER_VALIDATE_EMAIL);
}

function sortByLastName(array $recordA, array $recordB): int
{
return strcmp($recordB[1], $recordA[1]);
}

$reader = Reader::createFromPath('/path/to/file.csv', 'r');
$stmt = (new Statement())
$stmt = Statement::create()
->offset(3)
->limit(2)
->where('filterByEmail')
->orderBy('sortByLastName')
->where(fn(array $record) => (bool) filter_var($record[2], FILTER_VALIDATE_EMAIL))
->orderBy(fn(array $recordA, array $recordB) => strcmp($recordB[1], $recordA[1]))
;

$records = $stmt->process($reader, ['firstname', 'lastname', 'email']);
~~~

<p class="message-notice">Starting with version <code>9.6.0</code>, the <code>Statement::process</code> method can also be used on the <code>ResultSet</code> class because it implements the <code>TabularDataReader</code> interface.</p>

~~~php
use League\Csv\Reader;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/file.csv', 'r');
$stmt = Statement::create()
->where(fn(array $record) => (bool) filter_var($record[2], FILTER_VALIDATE_EMAIL))
->orderBy(fn(array $recordA, array $recordB) => strcmp($recordB[1], $recordA[1]))
;

$resultSet = $stmt->process($reader, ['firstname', 'lastname', 'email']);

$stmt2 = Statement::create(null, 3, 2);
$records = $stmt2->process($resultSet);
// the $records and the $resultSet parameters are distinct League\Csv\ResultSet instances.
~~~

0 comments on commit 3d1c4f1

Please sign in to comment.