Skip to content

Commit e71999e

Browse files
committed
Have StatementByGuidMap implement IteratorAggregate
1 parent bb3200d commit e71999e

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/Statement/StatementByGuidMap.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Wikibase\DataModel\Statement;
44

5+
use ArrayIterator;
6+
use Countable;
57
use InvalidArgumentException;
8+
use IteratorAggregate;
9+
use Traversable;
610

711
/**
812
* Ordered and unique collection of Statement objects.
@@ -14,7 +18,7 @@
1418
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1519
* @author Kai Nissen < kai.nissen@wikimedia.de >
1620
*/
17-
class StatementByGuidMap implements \Countable {
21+
class StatementByGuidMap implements IteratorAggregate, Countable {
1822

1923
private $statements = array();
2024

@@ -86,4 +90,12 @@ public function count() {
8690
return count( $this->statements );
8791
}
8892

93+
/**
94+
* @see IteratorAggregate::getIterator
95+
* @return Traversable
96+
*/
97+
public function getIterator() {
98+
return new ArrayIterator( $this->statements );
99+
}
100+
89101
}

tests/unit/Statement/StatementByGuidMapTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,24 @@ public function testMapCanBePassedToCount() {
141141
$this->assertSame( 2, count( $statements ) );
142142
}
143143

144+
public function testMapCanBeIteratedOver() {
145+
$statement1 = $this->newStatement( 1, 'some guid' );
146+
$statement2 = $this->newStatement( 2, 'other guid' );
147+
148+
$statementMap = new StatementByGuidMap( array( $statement1, $statement2 ) );
149+
150+
$iteratedStatements = array();
151+
152+
foreach ( $statementMap as $guid => $statement ) {
153+
$iteratedStatements[$guid] = $statement;
154+
}
155+
156+
$expectedStatements = array(
157+
'some guid' => $statement1,
158+
'other guid' => $statement2
159+
);
160+
161+
$this->assertEquals( $expectedStatements, $iteratedStatements );
162+
}
163+
144164
}

0 commit comments

Comments
 (0)