Skip to content

Commit a51a778

Browse files
committed
Selection: uses yield for iteration
1 parent e42d0a3 commit a51a778

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.
@@ -969,43 +966,18 @@ public function getReferencingTable(
969966
}
970967

971968

972-
/********************* interface Iterator ****************d*g**/
969+
/********************* interface IteratorAggregate ****************d*g**/
973970

974971

975-
public function rewind(): void
972+
/** @return \Generator<T> */
973+
public function getIterator(): \Generator
976974
{
977975
$this->execute();
978-
$this->keys = array_keys($this->data);
979-
reset($this->keys);
980-
}
981-
982-
983-
/** @return T|false */
984-
public function current(): ActiveRow|false
985-
{
986-
return ($key = current($this->keys)) !== false
987-
? $this->data[$key]
988-
: false;
989-
}
990-
991-
992-
public function key(): string|int
993-
{
994-
return current($this->keys);
995-
}
996-
997-
998-
public function next(): void
999-
{
1000-
do {
1001-
next($this->keys);
1002-
} while (($key = current($this->keys)) !== false && !isset($this->data[$key]));
1003-
}
1004-
1005-
1006-
public function valid(): bool
1007-
{
1008-
return current($this->keys) !== false;
976+
foreach ($this->data as $key => $value) {
977+
if (isset($this->data[$key])) { // may be unset by offsetUnset
978+
yield $key => $value;
979+
}
980+
}
1009981
}
1010982

1011983

0 commit comments

Comments
 (0)