Skip to content

Commit 01cc85e

Browse files
committed
Selection: uses yield for iteration
1 parent 509bc53 commit 01cc85e

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
@@ -18,10 +18,10 @@
1818
* Represents filtered table result.
1919
* Selection is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
2020
* @template T of ActiveRow
21-
* @implements \Iterator<T>
21+
* @implements \IteratorAggregate<T>
2222
* @implements \ArrayAccess<T>
2323
*/
24-
class Selection implements \Iterator, \ArrayAccess, \Countable
24+
class Selection implements \IteratorAggregate, \ArrayAccess, \Countable
2525
{
2626
protected readonly Explorer $explorer;
2727
protected readonly ?Nette\Caching\Cache $cache;
@@ -59,9 +59,6 @@ class Selection implements \Iterator, \ArrayAccess, \Countable
5959
/** should instance observe accessed columns caching */
6060
protected ?self $observeCache = null;
6161

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

6663
/**
6764
* Creates filtered table representation.
@@ -971,43 +968,18 @@ public function getReferencingTable(
971968
}
972969

973970

974-
/********************* interface Iterator ****************d*g**/
971+
/********************* interface IteratorAggregate ****************d*g**/
975972

976973

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

1013985

0 commit comments

Comments
 (0)