diff --git a/CHANGELOG.md b/CHANGELOG.md index 58e9eaeb..4b94d674 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All Notable changes to `Csv` will be documented in this file - `Statement::select` - `TabularDataReader::getRecordsAsObject` - `TabularDataReader::chunkBy` +- `TabularDataReader::mapHeader` ### Deprecated diff --git a/docs/9.0/reader/tabular-data-reader.md b/docs/9.0/reader/tabular-data-reader.md index 332fab68..113c893e 100644 --- a/docs/9.0/reader/tabular-data-reader.md +++ b/docs/9.0/reader/tabular-data-reader.md @@ -452,6 +452,40 @@ $reader = Reader::createFromPath('/path/to/my/file.csv')
+### mapHeader + + + + +Complementary to the `select` method, the `mapHeader` method allows you to redefine the column header +names and order and returns a new `TabularDataReader`. The submitted array argument act like the +array from the `Statement::process` method but instead of returning a iterable structure of records +it returns a new `TabularDataReader` with a new header. + +```php +$tabularData = $reader + ->slice(0, 10) + ->mapHeader([ + 3 => 'Year', + 2 => 'Gender', + 0 => 'Firstname', + 1 => 'Count', + ]); + +//is equivalent to + +$tabularData = Statement::create() + ->offset(0) + ->limit(10) + ->process($reader, [ + 3 => 'Year', + 2 => 'Gender', + 0 => 'Firstname', + 1 => 'Count', + ]); +$tabularData->getHeader(); // returns ['Year', 'Gender', 'Firstname', 'Count']; +``` + ### matching, matchingFirst, matchingFirstOrFail 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; $reader = Reader::createFromString($csv); foreach ($reader->chunkBy(4) as $chunk) { + // $chunk is a TabularDataReader instance foreach ($chunk as $record) { //the actual record will be found here. } diff --git a/src/Reader.php b/src/Reader.php index 9ba26c7f..22120775 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -331,6 +331,14 @@ public function chunkBy(int $length): Iterator return ResultSet::createFromTabularDataReader($this)->chunkBy($length); } + /** + * @param array