Skip to content

Commit e9173fa

Browse files
authored
Allow to use objects as map keys (#105)
* Allow to use objects as map keys * add more tests * allow cast map to native array
1 parent 046887b commit e9173fa

File tree

4 files changed

+209
-69
lines changed

4 files changed

+209
-69
lines changed

src/Collection/Iterator/MapIterator.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66

77
use Munus\Collection\Iterator;
88
use Munus\Exception\NoSuchElementException;
9-
use Munus\Tuple;
9+
use Munus\Tuple\Tuple2;
1010

11+
/**
12+
* @template K
13+
* @template V
14+
*/
1115
final class MapIterator extends Iterator
1216
{
1317
/**
14-
* @var mixed[]
18+
* @var array<Tuple2<K,V>>
1519
*/
1620
private array $map;
1721

22+
/**
23+
* @param array<Tuple2<K,V>> $map
24+
*/
1825
public function __construct(array $map)
1926
{
2027
$this->map = $map;
@@ -23,25 +30,30 @@ public function __construct(array $map)
2330

2431
public function key(): mixed
2532
{
26-
return key($this->map);
33+
return current($this->map)[0];
2734
}
2835

2936
public function current(): mixed
3037
{
31-
return current($this->map);
38+
return current($this->map)[1];
3239
}
3340

3441
public function rewind(): void
3542
{
3643
reset($this->map);
3744
}
3845

39-
public function next(): Tuple
46+
/**
47+
* @throws NoSuchElementException
48+
*
49+
* @return Tuple2<K, V>
50+
*/
51+
public function next(): Tuple2
4052
{
4153
if (!$this->valid()) {
4254
throw new NoSuchElementException();
4355
}
44-
$next = Tuple::of(key($this->map), current($this->map));
56+
$next = current($this->map);
4557
next($this->map);
4658

4759
return $next;
@@ -57,6 +69,9 @@ public function hasNext(): bool
5769
return $this->valid();
5870
}
5971

72+
/**
73+
* @return array<Tuple2<K, V>>
74+
*/
6075
public function toArray(): array
6176
{
6277
return $this->map;

0 commit comments

Comments
 (0)