1414namespace League \Csv ;
1515
1616use CallbackFilterIterator ;
17- use Countable ;
18- use Generator ;
1917use Iterator ;
20- use IteratorAggregate ;
2118use JsonSerializable ;
2219use LimitIterator ;
2320use function array_flip ;
3027/**
3128 * Represents the result set of a {@link Reader} processed by a {@link Statement}.
3229 */
33- class ResultSet implements Countable, IteratorAggregate , JsonSerializable
30+ class ResultSet implements TabularDataReader , JsonSerializable
3431{
3532 /**
3633 * The CSV records collection.
@@ -51,8 +48,9 @@ class ResultSet implements Countable, IteratorAggregate, JsonSerializable
5148 */
5249 public function __construct (Iterator $ records , array $ header )
5350 {
54- $ this ->records = $ records ;
5551 $ this ->validateHeader ($ header );
52+
53+ $ this ->records = $ records ;
5654 $ this ->header = $ header ;
5755 }
5856
@@ -74,6 +72,14 @@ public function __destruct()
7472 unset($ this ->records );
7573 }
7674
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+
7783 /**
7884 * Returns the header associated with the result set.
7985 *
@@ -87,15 +93,15 @@ public function getHeader(): array
8793 /**
8894 * {@inheritdoc}
8995 */
90- public function getIterator (): Generator
96+ public function getIterator (): Iterator
9197 {
9298 return $ this ->getRecords ();
9399 }
94100
95101 /**
96102 * {@inheritdoc}
97103 */
98- public function getRecords (array $ header = []): Generator
104+ public function getRecords (array $ header = []): Iterator
99105 {
100106 $ this ->validateHeader ($ header );
101107 $ records = $ this ->combineHeader ($ header );
@@ -145,13 +151,7 @@ public function jsonSerialize(): array
145151 }
146152
147153 /**
148- * Returns the nth record from the result set.
149- *
150- * By default if no index is provided the first record of the resultet is returned
151- *
152- * @param int $nth_record the CSV record offset
153- *
154- * @throws Exception if argument is lesser than 0
154+ * {@inheritdoc}
155155 */
156156 public function fetchOne (int $ nth_record = 0 ): array
157157 {
@@ -166,13 +166,9 @@ public function fetchOne(int $nth_record = 0): array
166166 }
167167
168168 /**
169- * Returns a single column from the next record of the result set.
170- *
171- * By default if no value is supplied the first column is fetch
172- *
173- * @param string|int $index CSV column index
169+ * {@inheritdoc}
174170 */
175- public function fetchColumn ($ index = 0 ): Generator
171+ public function fetchColumn ($ index = 0 ): Iterator
176172 {
177173 $ offset = $ this ->getColumnIndex ($ index , __METHOD__ .'() expects the column index to be a valid string or integer, `%s` given ' );
178174 $ filter = static function (array $ record ) use ($ offset ): bool {
@@ -246,17 +242,9 @@ protected function getColumnIndexByKey(int $index, string $error_message)
246242 }
247243
248244 /**
249- * Returns the next key-value pairs from a result set (first
250- * column is the key, second column is the value).
251- *
252- * By default if no column index is provided:
253- * - the first column is used to provide the keys
254- * - the second column is used to provide the value
255- *
256- * @param string|int $offset_index The column index to serve as offset
257- * @param string|int $value_index The column index to serve as value
245+ * {@inheritdoc}
258246 */
259- public function fetchPairs ($ offset_index = 0 , $ value_index = 1 ): Generator
247+ public function fetchPairs ($ offset_index = 0 , $ value_index = 1 ): Iterator
260248 {
261249 $ offset = $ this ->getColumnIndex ($ offset_index , __METHOD__ .'() expects the offset index value to be a valid string or integer, `%s` given ' );
262250 $ value = $ this ->getColumnIndex ($ value_index , __METHOD__ .'() expects the value index value to be a valid string or integer, `%s` given ' );
0 commit comments