Skip to content

Commit d7a4e68

Browse files
committed
Selection: uses yield for iteration
1 parent 55890af commit d7a4e68

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.
@@ -970,43 +967,18 @@ public function getReferencingTable(
970967
}
971968

972969

973-
/********************* interface Iterator ****************d*g**/
970+
/********************* interface IteratorAggregate ****************d*g**/
974971

975972

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

1012984

0 commit comments

Comments
 (0)