Skip to content

Commit 62503d3

Browse files
committed
adding ResultSet::createFromReader
1 parent c90a0c9 commit 62503d3

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ All Notable changes to `Csv` will be documented in this file
99
- More return types and type parameters as supported in PHP7.2+
1010
- `League\Csv\Statement::create` named constructor to ease constraint builder instantiation
1111
- `League\Csv\Statement` can now also process `League\Csv\ResultSet` instances.
12-
- `League\Csv\ResultSet::getRecords` has an optional `$header` second argument to make the method works like `League\Csv\Reader::getRecords`
1312
- `League\Csv\TabularDataReader` interface to represent how to read tabular data
13+
- `League\Csv\ResultSet::getRecords` has an optional `$header` second argument to make the method works like `League\Csv\Reader::getRecords`
14+
- `League\Csv\ResultSet::createFromReader` create a new instance from `League\Csv\Reader`
1415

1516
### Deprecated
1617

src/Reader.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,23 @@ protected function removeBOM(array $record, int $bom_length, string $enclosure):
199199
*/
200200
public function fetchColumn($index = 0): Iterator
201201
{
202-
$tabular_data = new ResultSet($this->getRecords(), $this->getHeader());
203-
204-
return $tabular_data->fetchColumn($index);
202+
return ResultSet::createFromReader($this)->fetchColumn($index);
205203
}
206204

207205
/**
208206
* {@inheritdoc}
209207
*/
210208
public function fetchOne(int $nth_record = 0): array
211209
{
212-
$tabular_data = new ResultSet($this->getRecords(), $this->getHeader());
213-
214-
return $tabular_data->fetchOne($nth_record);
210+
return ResultSet::createFromReader($this)->fetchOne($nth_record);
215211
}
216212

217213
/**
218214
* {@inheritdoc}
219215
*/
220216
public function fetchPairs($offset_index = 0, $value_index = 1): Iterator
221217
{
222-
$tabular_data = new ResultSet($this->getRecords(), $this->getHeader());
223-
224-
return $tabular_data->fetchPairs($offset_index, $value_index);
218+
return ResultSet::createFromReader($this)->fetchPairs($offset_index, $value_index);
225219
}
226220

227221
/**
@@ -253,18 +247,7 @@ public function jsonSerialize(): array
253247
}
254248

255249
/**
256-
* Returns the CSV records as an iterator object.
257-
*
258-
* Each CSV record is represented as a simple array containing strings or null values.
259-
*
260-
* If the CSV document has a header record then each record is combined
261-
* to the header record and the header record is removed from the iterator.
262-
*
263-
* If the CSV document is inconsistent. Missing record fields are
264-
* filled with null values while extra record fields are strip from
265-
* the returned object.
266-
*
267-
* @param string[] $header an optional header to use instead of the CSV document header
250+
* {@inheritDoc}
268251
*/
269252
public function getRecords(array $header = []): Iterator
270253
{

src/ResultSet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function __destruct()
7272
unset($this->records);
7373
}
7474

75+
/**
76+
* Returns a new instance from a League\Csv\Reader object.
77+
*/
78+
public static function createFromReader(Reader $reader): self
79+
{
80+
return new self($reader->getRecords(), $reader->getHeader());
81+
}
82+
7583
/**
7684
* Returns the header associated with the result set.
7785
*

src/TabularDataReader.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
interface TabularDataReader extends Countable, IteratorAggregate
2424
{
2525
/**
26-
* Returns the header associated with the tabular data.
27-
*
28-
* The header must contains unique string or is an empty array
29-
* if no header was specified.
30-
*
31-
* @return string[]
26+
* Returns the number of records contained in the tabular data structure
27+
* excluding the header record.
3228
*/
33-
public function getHeader(): array;
29+
public function count(): int;
3430

3531
/**
3632
* Returns the tabular data records as an iterator object.
@@ -43,29 +39,34 @@ public function getHeader(): array;
4339
* If the CSV document is inconsistent. Missing record fields are
4440
* filled with null values while extra record fields are strip from
4541
* the returned object.
42+
*/
43+
public function getIterator(): Iterator;
44+
45+
/**
46+
* Returns the header associated with the tabular data.
4647
*
47-
* @param string[] $header an optional header to use instead of the CSV document header
48+
* The header must contains unique string or is an empty array
49+
* if no header was specified.
50+
*
51+
* @return string[]
4852
*/
49-
public function getRecords(array $header = []): Iterator;
53+
public function getHeader(): array;
5054

5155
/**
5256
* Returns the tabular data records as an iterator object.
5357
*
5458
* Each record is represented as a simple array containing strings or null values.
5559
*
56-
* If the CSV document has a header record then each record is combined
60+
* If the tabular data has a header record then each record is combined
5761
* to the header record and the header record is removed from the iterator.
5862
*
59-
* If the CSV document is inconsistent. Missing record fields are
63+
* If the tabular data is inconsistent. Missing record fields are
6064
* filled with null values while extra record fields are strip from
6165
* the returned object.
66+
*
67+
* @param string[] $header an optional header to use instead of the CSV document header
6268
*/
63-
public function getIterator(): Iterator;
64-
65-
/**
66-
* Returns the number of records contained in the tabular data structure.
67-
*/
68-
public function count(): int;
69+
public function getRecords(array $header = []): Iterator;
6970

7071
/**
7172
* Returns the nth record from the tabular data.

0 commit comments

Comments
 (0)