Skip to content

Commit bcb9989

Browse files
committed
Selection: uses yield for iteration
1 parent 41bfcb8 commit bcb9989

File tree

1 file changed

+10
-38
lines changed

1 file changed

+10
-38
lines changed

src/Database/Table/Selection.php

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* Represents filtered table result.
1818
* Selection is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
1919
* @template T of ActiveRow
20-
* @implements \Iterator<T>
20+
* @implements \IteratorAggregate<T>
2121
* @implements \ArrayAccess<T>
2222
*/
23-
class Selection implements \Iterator, \ArrayAccess, \Countable
23+
class Selection implements \IteratorAggregate, \ArrayAccess, \Countable
2424
{
2525
protected readonly Explorer $explorer;
2626
protected readonly ?Nette\Caching\Cache $cache;
@@ -58,9 +58,6 @@ class Selection implements \Iterator, \ArrayAccess, \Countable
5858
/** should instance observe accessed columns caching */
5959
protected ?self $observeCache = null;
6060

61-
/** of primary key values */
62-
protected array $keys = [];
63-
6461

6562
/**
6663
* Creates filtered table representation.
@@ -953,43 +950,18 @@ public function getReferencingTable(
953950
}
954951

955952

956-
/********************* interface Iterator ****************d*g**/
953+
/********************* interface IteratorAggregate ****************d*g**/
957954

958955

959-
public function rewind(): void
956+
/** @return \Generator<T> */
957+
public function getIterator(): \Generator
960958
{
961959
$this->execute();
962-
$this->keys = array_keys($this->data);
963-
reset($this->keys);
964-
}
965-
966-
967-
/** @return T|false */
968-
public function current(): ActiveRow|false
969-
{
970-
return ($key = current($this->keys)) !== false
971-
? $this->data[$key]
972-
: false;
973-
}
974-
975-
976-
public function key(): string|int
977-
{
978-
return current($this->keys);
979-
}
980-
981-
982-
public function next(): void
983-
{
984-
do {
985-
next($this->keys);
986-
} while (($key = current($this->keys)) !== false && !isset($this->data[$key]));
987-
}
988-
989-
990-
public function valid(): bool
991-
{
992-
return current($this->keys) !== false;
960+
foreach ($this->data as $key => $value) {
961+
if (isset($this->data[$key])) { // may be unset by offsetUnset
962+
yield $key => $value;
963+
}
964+
}
993965
}
994966

995967

0 commit comments

Comments
 (0)