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