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')

Added in version 9.12.0 for Reader and ResultSet.

Wraps the functionality of Statement::select.

+### mapHeader + +

Added in version 9.15.0 for Reader and ResultSet.

+

Wraps the functionality of Statement::process.

+ +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 $header + */ + public function mapHeader(array $headers): TabularDataReader + { + return Statement::create()->process($this, $headers); + } + /** * @param Closure(array, array-key): bool $closure * diff --git a/src/ResultSet.php b/src/ResultSet.php index c90564a8..cda984a1 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -190,6 +190,14 @@ public function chunkBy(int $length): Iterator } } + /** + * @param array $headers + */ + public function mapHeader(array $headers): TabularDataReader + { + return Statement::create()->process($this, $headers); + } + public function filter(Closure $closure): TabularDataReader { return Statement::create()->where($closure)->process($this); diff --git a/src/TabularDataReader.php b/src/TabularDataReader.php index 48f0062b..ec130d04 100644 --- a/src/TabularDataReader.php +++ b/src/TabularDataReader.php @@ -41,6 +41,7 @@ * @method TabularDataReader|null matchingFirst(string $expression) extract the first found fragment identifier of the tabular data or return null if none is found * @method iterable matching(string $expression) extract all found fragment identifiers for the tabular data * @method iterable chunkBy(int $length) Chunk the TabulaDataReader into smaller TabularDataReader instance of the given size or less. + * @method TabularDataReader mapHeader(array $headers) Returns a new TabulaDataReader with a new set of headers. */ interface TabularDataReader extends Countable, IteratorAggregate {