Skip to content

Commit 00a4f4a

Browse files
committed
Selection: uses yield for iteration
1 parent 03127a6 commit 00a4f4a

1 file changed

Lines changed: 10 additions & 38 deletions

File tree

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.
@@ -965,43 +962,18 @@ public function getReferencingTable(
965962
}
966963

967964

968-
/********************* interface Iterator ****************d*g**/
965+
/********************* interface IteratorAggregate ****************d*g**/
969966

970967

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

1007979

0 commit comments

Comments
 (0)