Skip to content

Commit

Permalink
rename preserveOffset to preserveRecordOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 17, 2017
1 parent a547b37 commit f4a55c0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/9.0/converter/html.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ echo $html;
// <td title="lastname">doe</td>
// <td title="email">john.doe@example.com</td>
// </tr>
// <tr data-record-offset="0">
// <tr data-record-offset="1">
// <td title="firstname">jane</td>
// <td title="lastname">doe</td>
// <td title="email">jane.doe@example.com</td>
Expand Down
2 changes: 1 addition & 1 deletion docs/9.0/converter/xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ $converter = (new XMLConverter())
;

$records = $stmt->process($csv);
$records->preserveOffset(true);
$records->preserveRecordOffset(true);

$dom = $converter->toXML($records);
$dom->formatOutput = true;
Expand Down
26 changes: 13 additions & 13 deletions docs/9.0/reader/records.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ title: Accessing Records from a CSV document
~~~php
<?php
public RecordSet::count(): int
public RecordSet::isOffsetPreserved(): bool
public RecordSet::isRecordOffsetPreserved(): bool
public RecordSet::getColumnNames(): array
public RecordSet::getIterator(): Generator
public RecordSet::fetchAll(): array
public RecordSet::fetchOne(int $offset = 0): array
public RecordSet::fetchPairs(string|int $offsetIndex = 0, string|int $valueIndex = 1): Generator
public RecordSet::fetchColumn(string|int $columnIndex = 0): Generator
public RecordSet::preserveOffset(bool $status): RecordSet
public RecordSet::preserveRecordOffset(bool $status): RecordSet
~~~

The `League\Csv\RecordSet` is a class which manipulates a collection of CSV document records. This object is returned from [Reader::select](/9.0/reader/#selecting-csv-records) or [Statement::process](/9.0/reader/statement/#apply-the-constraints-to-a-csv-document) execution.
Expand Down Expand Up @@ -98,13 +98,13 @@ $records->getColumnNames(); // returns ['firstname', 'lastname', 'email'];
~~~php
<?php

public RecordSet::isOffsetPreserved(): bool
public RecordSet::preserveOffset(bool $status): RecordSet
public RecordSet::isRecordOffsetPreserved(): bool
public RecordSet::preserveRecordOffset(bool $status): RecordSet
~~~

`RecordSet::preserveOffset` indicates if the `RecordSet` must keep the original CSV document records offset or can re-index them. When the `$status` is `true`, the original CSV document record offset will be preserve and output in methods where it makes sense.
`RecordSet::preserveRecordOffset` indicates if the `RecordSet` must keep the original CSV document records offset or can re-index them. When the `$status` is `true`, the original CSV document record offset will be preserve and output in methods where it makes sense.

At any given time you can tell whether the CSV document offset is kept by calling `RecordSet::isOffsetPreserved` which returns a boolean.
At any given time you can tell whether the CSV document offset is kept by calling `RecordSet::isRecordOffsetPreserved` which returns a boolean.

<p class="message-notice">By default, the <code>RecordSet</code> object does not preserve the original offset.</p>

Expand Down Expand Up @@ -139,7 +139,7 @@ foreach ($records->fetchAll() as $offset => $record) {

~~~

If the `RecordSet::preserveOffset` is set to `true`, the `$offset` parameter will contains the original CSV document offset index, otherwise it will contain a numerical index starting from `0`.
If the `RecordSet::preserveRecordOffset` is set to `true`, the `$offset` parameter will contains the original CSV document offset index, otherwise it will contain a numerical index starting from `0`.

~~~php
<?php
Expand All @@ -152,13 +152,13 @@ $reader = Reader::createFromPath('/path/to/my/file.csv');
//we will start iterating from the 6th record
$stmt = (new Statement())->offset(5);
$records = $stmt->process($reader);
$records->isOffsetPreserved(); //returns false
$records->isRecordOffsetPreserved(); //returns false
foreach ($records as $offset => $record) {
//the first iteration will give $offset equal to `0`
}

$records->preserveOffset(true); //we are preserving the original offset
$records->isOffsetPreserved(); //returns true
$records->preserveRecordOffset(true); //we are preserving the original offset
$records->isRecordOffsetPreserved(); //returns true
foreach ($records->fetchAll() as $offset => $record) {
//the first iteration will give $offset equal to `5`
}
Expand Down Expand Up @@ -226,7 +226,7 @@ $data = $stmt->proce($reader)->fetchOne(3);
//
~~~

<p class="message-notice">The <code>$offset</code> argument is not affected by <code>RecordSet::preserveOffset</code> status.</p>
<p class="message-notice">The <code>$offset</code> argument is not affected by <code>RecordSet::preserveRecordOffset</code> status.</p>

## Selecting a specific column

Expand Down Expand Up @@ -264,7 +264,7 @@ foreach ($records->fetchColumn('First Name') as $offset => $value) {
}
~~~

If the `RecordSet::preserveOffset` is set to `true`, the `$offset` parameter will contains the original CSV document offset index, otherwise it will contain numerical index starting from`0`.
If the `RecordSet::preserveRecordOffset` is set to `true`, the `$offset` parameter will contains the original CSV document offset index, otherwise it will contain numerical index starting from`0`.

~~~php
<?php
Expand All @@ -277,7 +277,7 @@ $reader = Reader::createFromPath('/path/to/my/file.csv');
//we will start iterating from the 6th record
$stmt = (new Statement())->offset(5);
$records = $stmt->process($reader);
$records->preserveOffset(true);
$records->preserveRecordOffset(true);
foreach ($records->fetchColumn(2) as $offset => $value) {
//$value is a string representing the value
//of a given record for the selected column
Expand Down
6 changes: 3 additions & 3 deletions src/RecordSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getColumnNames(): array
*
* @return bool
*/
public function isOffsetPreserved(): bool
public function isRecordOffsetPreserved(): bool
{
return $this->preserve_offset;
}
Expand All @@ -107,7 +107,7 @@ public function getIterator(): Generator
}

/**
* Return the generator depending on the preserveOffset setting
* Return the generator depending on the preserveRecordOffset setting
*
* @param Iterator $iterator
*
Expand Down Expand Up @@ -265,7 +265,7 @@ public function fetchPairs($offset_index = 0, $value_index = 1): Generator
*
* @return static
*/
public function preserveOffset(bool $status)
public function preserveRecordOffset(bool $status)
{
$this->preserve_offset = $status;

Expand Down
12 changes: 6 additions & 6 deletions tests/RecordSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testCountable()
{
$records = $this->stmt->limit(1)->process($this->csv);
$this->assertCount(1, $records);
$records->preserveOffset(true);
$records->preserveRecordOffset(true);
$this->assertSame(iterator_to_array($records, false), $records->fetchAll());
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public function testFetchColumnWithColumnIndex()
{
$keys = ['firstname', 'lastname', 'email'];
$records = $this->stmt->columns($keys)->process($this->csv);
$records->preserveOffset(true);
$records->preserveRecordOffset(true);
$this->assertSame(['john', 'jane'], iterator_to_array($records->fetchColumn(0), false));
}

Expand Down Expand Up @@ -332,11 +332,11 @@ public function testPreserveOffset()
1 => ['parent name' => 'parentA', 'child name' => 'childA', 'title' => 'titleA'],
];
$records = $this->stmt->process($csv);
$records->preserveOffset(false);
$this->assertFalse($records->isOffsetPreserved());
$records->preserveRecordOffset(false);
$this->assertFalse($records->isRecordOffsetPreserved());
$this->assertSame($expectedNoOffset, $records->fetchAll());
$records->preserveOffset(true);
$this->assertTrue($records->isOffsetPreserved());
$records->preserveRecordOffset(true);
$this->assertTrue($records->isRecordOffsetPreserved());
$this->assertSame($expectedWithOffset, $records->fetchAll());
}

Expand Down

0 comments on commit f4a55c0

Please sign in to comment.