Skip to content

Commit dfe53f5

Browse files
committed
Adding TabularDataReader::mapHeader method
1 parent 60b0062 commit dfe53f5

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All Notable changes to `Csv` will be documented in this file
99
- `Statement::select`
1010
- `TabularDataReader::getRecordsAsObject`
1111
- `TabularDataReader::chunkBy`
12+
- `TabularDataReader::mapHeader`
1213

1314
### Deprecated
1415

docs/9.0/reader/tabular-data-reader.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,40 @@ $reader = Reader::createFromPath('/path/to/my/file.csv')
452452
<p class="message-notice">Added in version <code>9.12.0</code> for <code>Reader</code> and <code>ResultSet</code>.</p>
453453
<p class="message-info"> Wraps the functionality of <code>Statement::select</code>.</p>
454454

455+
### mapHeader
456+
457+
<p class="message-notice">Added in version <code>9.15.0</code> for <code>Reader</code> and <code>ResultSet</code>.</p>
458+
<p class="message-info"> Wraps the functionality of <code>Statement::process</code>.</p>
459+
460+
Complementary to the `select` method, the `mapHeader` method allows you to redefine the column header
461+
names and order and returns a new `TabularDataReader`. The submitted array argument act like the
462+
array from the `Statement::process` method but instead of returning a iterable structure of
463+
records it returns a new `TabularDataReader` with a new header.
464+
465+
```php
466+
$tabularData = $reader
467+
->slice(0, 10)
468+
->mapHeader([
469+
3 => 'Year',
470+
2 => 'Gender',
471+
0 => 'Firstname',
472+
1 => 'Count',
473+
]);
474+
475+
//is equivalent to
476+
477+
$tabularData = Statement::create()
478+
->offset(0)
479+
->limit(10)
480+
->process($reader, [
481+
3 => 'Year',
482+
2 => 'Gender',
483+
0 => 'Firstname',
484+
1 => 'Count',
485+
]);
486+
$tabularData->getHeader(); // returns ['Year', 'Gender', 'Firstname', 'Count'];
487+
```
488+
455489
### matching, matchingFirst, matchingFirstOrFail
456490

457491
The `matching` method allows selecting records, columns or cells from the tabular data reader that match the
@@ -487,6 +521,7 @@ use League\Csv\Reader;
487521
$reader = Reader::createFromString($csv);
488522

489523
foreach ($reader->chunkBy(4) as $chunk) {
524+
// $chunk is a TabularDataReader instance
490525
foreach ($chunk as $record) {
491526
//the actual record will be found here.
492527
}

src/Reader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ public function chunkBy(int $length): Iterator
331331
return ResultSet::createFromTabularDataReader($this)->chunkBy($length);
332332
}
333333

334+
/**
335+
* @param array<string> $headers
336+
*/
337+
public function mapHeader(array $headers): TabularDataReader
338+
{
339+
return Statement::create()->process($this, $headers);
340+
}
341+
334342
/**
335343
* @param Closure(array<mixed>, array-key): bool $closure
336344
*

src/ResultSet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ public function chunkBy(int $length): Iterator
190190
}
191191
}
192192

193+
/**
194+
* @param array<string> $headers
195+
*/
196+
public function mapHeader(array $headers): TabularDataReader
197+
{
198+
return Statement::create()->process($this, $headers);
199+
}
200+
193201
public function filter(Closure $closure): TabularDataReader
194202
{
195203
return Statement::create()->where($closure)->process($this);

src/TabularDataReader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @method TabularDataReader|null matchingFirst(string $expression) extract the first found fragment identifier of the tabular data or return null if none is found
4242
* @method iterable<int, TabularDataReader> matching(string $expression) extract all found fragment identifiers for the tabular data
4343
* @method iterable<int, TabularDataReader> chunkBy(int $length) Chunk the TabulaDataReader into smaller TabularDataReader instance of the given size or less.
44+
* @method TabularDataReader mapHeader(array $headers) Returns a new TabulaDataReader with a new set of headers.
4445
*/
4546
interface TabularDataReader extends Countable, IteratorAggregate
4647
{

0 commit comments

Comments
 (0)