Skip to content

Commit b7aeb45

Browse files
committed
Selection: uses yield for iteration
1 parent 1ef0bd4 commit b7aeb45

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
* Filtered table representation.
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.
@@ -948,43 +945,18 @@ public function getReferencingTable(
948945
}
949946

950947

951-
/********************* interface Iterator ****************d*g**/
948+
/********************* interface IteratorAggregate ****************d*g**/
952949

953950

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

990962

0 commit comments

Comments
 (0)