1717/**
1818 * Represents a database result set.
1919 */
20- class Result implements \Iterator
20+ class Result implements \IteratorAggregate
2121{
22- private Row |false |null $ lastRow = null ;
23- private int $ lastRowKey = -1 ;
22+ private bool $ fetched = false ;
2423
2524 /** @var Row[] */
2625 private array $ rows ;
@@ -94,42 +93,20 @@ public function dump(): void
9493 }
9594
9695
97- /********************* interface Iterator ****************d*g**/
96+ /********************* interface IteratorAggregate ****************d*g**/
9897
9998
100- public function rewind (): void
99+ /** @return \Generator<Row> */
100+ public function getIterator (): \Generator
101101 {
102- if ($ this ->lastRow === false ) {
102+ if ($ this ->fetched ) {
103103 throw new Nette \InvalidStateException (self ::class . ' implements only one way iterator. ' );
104104 }
105- }
106-
107-
108- public function current (): Row |false |null
109- {
110- return $ this ->lastRow ;
111- }
112-
113-
114- public function key (): int
115- {
116- return $ this ->lastRowKey ;
117- }
118-
119105
120- public function next (): void
121- {
122- $ this ->lastRow = false ;
123- }
124-
125-
126- public function valid (): bool
127- {
128- if ($ this ->lastRow ) {
129- return true ;
106+ $ counter = 0 ;
107+ while (($ row = $ this ->fetch ()) !== null ) {
108+ yield $ counter ++ => $ row ;
130109 }
131-
132- return $ this ->fetch () !== null ;
133110 }
134111
135112
@@ -147,13 +124,15 @@ public function fetchAssoc(?string $path = null): ?array
147124
148125 $ data = $ this ->result ?->fetch();
149126 if ($ data === null ) {
127+ $ this ->fetched = true ;
150128 return null ;
151129
152- } elseif ($ this ->lastRow === null && count ($ data ) !== $ this ->result ->getColumnCount ()) {
130+ } elseif (! $ this ->fetched && count ($ data ) !== $ this ->result ->getColumnCount ()) {
153131 $ duplicates = array_filter (array_count_values (array_column ($ this ->result ->getColumnsInfo (), 'name ' )), fn ($ val ) => $ val > 1 );
154132 trigger_error ("Found duplicate columns in database result set: ' " . implode ("', ' " , array_keys ($ duplicates )) . "'. " );
155133 }
156134
135+ $ this ->fetched = true ;
157136 return $ this ->normalizeRow ($ data );
158137 }
159138
@@ -164,12 +143,7 @@ public function fetchAssoc(?string $path = null): ?array
164143 public function fetch (): ?Row
165144 {
166145 $ data = $ this ->fetchAssoc ();
167- if ($ data === null ) {
168- return null ;
169- }
170-
171- $ this ->lastRowKey ++;
172- return $ this ->lastRow = Arrays::toObject ($ data , new Row );
146+ return $ data === null ? null : Arrays::toObject ($ data , new Row );
173147 }
174148
175149
@@ -219,8 +193,7 @@ public function fetchPairs(string|int|\Closure|null $keyOrCallback = null, strin
219193 */
220194 public function fetchAll (): array
221195 {
222- $ this ->rows ??= iterator_to_array ($ this );
223- return $ this ->rows ;
196+ return $ this ->rows ??= iterator_to_array ($ this );
224197 }
225198
226199
0 commit comments