|
18 | 18 | * Represents filtered table result. |
19 | 19 | * Selection is based on the great library NotORM http://www.notorm.com written by Jakub Vrana. |
20 | 20 | * @template T of ActiveRow |
21 | | - * @implements \Iterator<T> |
| 21 | + * @implements \IteratorAggregate<T> |
22 | 22 | * @implements \ArrayAccess<T> |
23 | 23 | */ |
24 | | -class Selection implements \Iterator, \ArrayAccess, \Countable |
| 24 | +class Selection implements \IteratorAggregate, \ArrayAccess, \Countable |
25 | 25 | { |
26 | 26 | protected readonly Explorer $explorer; |
27 | 27 | protected readonly ?Nette\Caching\Cache $cache; |
@@ -59,9 +59,6 @@ class Selection implements \Iterator, \ArrayAccess, \Countable |
59 | 59 | /** should instance observe accessed columns caching */ |
60 | 60 | protected ?self $observeCache = null; |
61 | 61 |
|
62 | | - /** of primary key values */ |
63 | | - protected array $keys = []; |
64 | | - |
65 | 62 |
|
66 | 63 | /** |
67 | 64 | * Creates filtered table representation. |
@@ -971,43 +968,18 @@ public function getReferencingTable( |
971 | 968 | } |
972 | 969 |
|
973 | 970 |
|
974 | | - /********************* interface Iterator ****************d*g**/ |
| 971 | + /********************* interface IteratorAggregate ****************d*g**/ |
975 | 972 |
|
976 | 973 |
|
977 | | - public function rewind(): void |
| 974 | + /** @return \Generator<T> */ |
| 975 | + public function getIterator(): \Generator |
978 | 976 | { |
979 | 977 | $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 | + } |
1011 | 983 | } |
1012 | 984 |
|
1013 | 985 |
|
|
0 commit comments